GET call in Android Studio with Java

Tiempo de lectura: 2 minutos

To create a list of elements obtained from a GET call and display them, you need to follow the steps outlined in the example below.

First, create an XML file to define the view with the list. To display a list, we will use the “ListView” element.

Photo by Bri Schneiter from Pexels

  


To create a list of elements obtained from a GET call and display them, you need to follow the steps outlined in the example below.

First, create an XML file to define the view with the list. To display a list, we will use the "ListView" element.



    


Next, initialize the ListView and the Adapter, and initialize

Initialize GetDataTask that extends AsyncTask to perform a GET request in the background doInBackground that creates a connection with the URL, reads the data from that connection, and stores it in a list, which we call data as an example.

When the background task is complete, we call onPostExecute and receive the JSON data.

Finally, we add to the ListView Adapter- And to update the view, we use adapter.notifyDataSetChanged().

public class MainActivity extends AppCompatActivity {

    private ListView listView;
    private ArrayAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = findViewById(R.id.listView);
        adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);

        // Method to perform the GET request
        new GetDataTask().execute();

        listView.setAdapter(adapter);
    }

    private class GetDataTask extends AsyncTask<Void, Void, List> {

        @Override
        protected List doInBackground(Void... voids) {
            // URL of the corresponding endpoint on your server
            String apiUrl = "url_api";

            List data = new ArrayList<>();

            try {
                URL url = new URL(apiUrl);
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setRequestMethod("GET");

                BufferedReader bufferedReader = new BufferedReader(
                        new InputStreamReader(urlConnection.getInputStream()));
                String line;
                while ((line = bufferedReader.readLine()) != null) {
                    data.add(line); // Add the obtained data
                }

                bufferedReader.close();
                urlConnection.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return data;
        }

        @Override
        protechtml
Copy code
                    JSONArray jsonArray = new JSONArray(jsonData);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        String centerName = jsonObject.getString("name");
                        centerNames.add(centerName);
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
                Log.e("JSON_PARSE_ERROR", "Error parsing JSON: " + e.getMessage());
                // Log.e("JSON_PARSE_ERROR", "JSON Data: " + jsonData);
            }

            adapter.addAll(centerNames);
            adapter.notifyDataSetChanged();
        }
    }
}

Finally, I show the result of the list on the mobile view.

Hope it helps! Have a great day.

Leave a Comment