Muthukumaaran
Muthukumaaran

Reputation: 17

Getting data from json api but not able to view in listview

I want to show the data in list view from rest api. At first i did the post request to the given api using async task

 public class SendRequest extends AsyncTask<String, Void, String> {

        protected void onPreExecute(){}

        protected String doInBackground(String... arg0) {

            try{

                URL url = new URL("https://staging.ineyetech.com/inweigh_dev/webservice/itinerary_list\n" +
                        "\n");

                JSONObject postDataParams = new JSONObject();

                postDataParams.put("device_type", "1");
                postDataParams.put("module_type", "2");
                postDataParams.put("user_time_zone", "Asia/Calcutta");
                postDataParams.put("unit_type", "2");
                postDataParams.put("page_no", "1");
                postDataParams.put("bluetooth_battery_percentage", "25");
                postDataParams.put("app_version", "Android 1.3");
                postDataParams.put("device_id", "18fd7cb2716cb0e4");
                postDataParams.put("device_name", "Moto G (4)");
                postDataParams.put("os_version", "7.0");
                postDataParams.put("device_token", "cNy2KZpIFwA");
                postDataParams.put("bluetooth_device_id", "885");
                postDataParams.put("itinerary_type", "2");
                postDataParams.put("oauth_token", "aa0f15f302fe1c0ffe7e3655a58f56bc");
                postDataParams.put("user_id", "205");

                Log.e("params",postDataParams.toString());

                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setReadTimeout(15000 /* milliseconds */);
                conn.setConnectTimeout(15000 /* milliseconds */);
                conn.setRequestMethod("POST");
                conn.setDoInput(true);
                conn.setDoOutput(true);

                OutputStream os = conn.getOutputStream();
                BufferedWriter writer = new BufferedWriter(
                        new OutputStreamWriter(os, "UTF-8"));
                writer.write(getPostDataString(postDataParams));

                writer.flush();
                writer.close();
                os.close();

                int responseCode=conn.getResponseCode();

                if (responseCode == HttpsURLConnection.HTTP_OK) {

                    BufferedReader in=new BufferedReader(new InputStreamReader(conn.getInputStream()));
                    StringBuffer sb = new StringBuffer("");
                    String line="";

                    while((line = in.readLine()) != null) {

                        sb.append(line);
                        break;
                    }

                    in.close();
                    return sb.toString();

                }
                else {
                    return new String("false : "+responseCode);
                }
            }
            catch(Exception e){
                return new String("Exception: " + e.getMessage());
            }
        }

I completed the post request using Async task and getting response from api. But I want that response to show in list view. So I did this.

        @Override
        protected void onPostExecute(String result) {

            super.onPostExecute(result);
            //text.setText(result);

            ListAdapter adapter = new SimpleAdapter(
                    MainActivity.this, airlines,
                    R.layout.details, new String[]{"flightname", "route",
                    "flightnum","time","date"}, new int[]{R.id.flightname,
                    R.id.route, R.id.flightnum,R.id.time,R.id.date});

            lv.setAdapter(adapter);
        }
    }

Even though I am getting response from api but I am not able to see that in list view. Need help. Thanks in advance

Upvotes: 0

Views: 62

Answers (2)

nuevo
nuevo

Reputation: 324

If I follow your code, I can't see any line to add data in airlines arraylist. Before adding adapter you need to add into that.

Inside onPostExecute method put below code-

try {

JSONObject obj = new JSONObject(result);

JsonArray airlinesArray = obj.getJSONArray("data");
for(int i = 0; i < airlinesArray.size(); i++){
    JSONObject object = airlinesArray.get(i);
    // do code to add data into airlines arraylist
}

} catch (Exception e) {

}
 //here put your adapter setting code
ListAdapter adapter = new SimpleAdapter(
                MainActivity.this, airlines,
                R.layout.details, new String[]{"flightname", "route",
                "flightnum","time","date"}, new int[]{R.id.flightname,
                R.id.route, R.id.flightnum,R.id.time,R.id.date});

        lv.setAdapter(adapter);

Upvotes: 1

Exigente05
Exigente05

Reputation: 2211

Inside protected void onPostExecute(String result) you need to create JsonObject then JsonArray of airlines then add into airlines arraylist.

Need to do something like,

try {

    JSONObject obj = new JSONObject(result);

    JsonArray airlinesArray = obj.getJSONArray("data");
    for(int i = 0; i < airlinesArray.size(); i++){
        JSONObject object = airlinesArray.get(i);
        //create airlines arraylist data type object with data and initialize variables using object.getString("s_name");
        // Then insert that data into arraylist
        // airlines.add(data);
    }

} catch (Exception e) {

}

After this create and add adapter into listView.

Upvotes: 0

Related Questions