简体   繁体   中英

how to parse json data and display in listview using aysnctask

I am parsing data and displaying it in ListView . I am done with my parsing successfully but I can't display that parsed data in ListView . I don't know what error I am getting. It automatically comes to home screen and I cant see even the error. I want to display the datas in ListView , those datas which I added in my HashMap .

     private class Getiingbusdetails extends AsyncTask<Void, Void, Void> {
        private static String url="http://runtowin.in/v2/ap/route_api.php?apicode=runtowin002&dateOfJourney=2017-01-01&originId=8&destinationId=5";

            String response = " ";
            String jsonstr;
            ProgressDialog dialogProgress = new ProgressDialog(Searchbus.this);
            AndroidHttpClient mClient = AndroidHttpClient.newInstance("");

            JSONArray array3;
            protected void onPreExecute(){
                dialogProgress.setCancelable(true);
                dialogProgress.setMessage("Please wait..");
                dialogProgress.setIndeterminate(false);
                dialogProgress.show();

            }

            @Override
            protected Void doInBackground(Void... params) {
                HttpGet request = new HttpGet(url);
                ResponseHandler<String> responseHandler = new BasicResponseHandler();
                try {
                    response = mClient.execute(request, responseHandler);
                } catch (ClientProtocolException exception) {
                    exception.printStackTrace();
                    return null;
                } catch (IOException exception) {
                    exception.printStackTrace();
                    return null;
                }
                Log.i("url", "" + url);
                Log.i("routes", "" + response);
                jsonstr = response;

                if (jsonstr != null) {
                    try {
                        JSONArray array1 = new JSONArray(jsonstr);
                        for (int i = 0; i < array1.length(); i++) {
                            JSONObject obj1 = array1.getJSONObject(i);
                            JSONArray array2 = obj1.getJSONArray("bus_schedule");
                            for (int j = 0; j < array2.length(); j++) {
                                JSONObject obj2 = array2.getJSONObject(j);
                                 array3 = obj2.getJSONArray("bus_route");

                                HashMap<String, String> map = new HashMap<>();

                                    for (int k = 0; k < array3.length(); k++) {
                                    JSONObject obj3 = array3.getJSONObject(k);

                                    String s1 = obj3.getString("serviceDate");
                                    String s2 = obj3.getString("amenities");
                                    String s3 = obj3.getString("startCityName");
                                    String s44 = obj3.getString("departureTime");
                                    String s4=dateformatter(s44);
                                    String s5 = obj3.getString("fare");
                                    String s6 = obj3.getString("endCityName");
                                    String s77 = obj3.getString("arrivalTime");
                                    String s7=dateformatter(s77);


                                    String s8 = obj3.getString("operatorName");
                                    String s9 = obj3.getString("bus_id");
                                    String s10 = obj3.getString("journeyHours");
                                    String s11 = obj3.getString("available_seat");
                                    String s12 = obj3.getString("total_seats");
                                    String s13 = obj3.getString("seat_type");
                                    String s14 = (""+s4+" - "+""+s7);
                                    Log.e("servicedate"+s1,"amenities"+s2+"startcityname"+s3);
                                   map.put(SERVICEDATE,s1);
                                    map.put(AMENITIES,s2);
                                    map.put(DEPARTTIME,s4);
                                    map.put(FARE,s5);
                                    map.put(ARRIVALTIME,s7);
                                    map.put(JOURNEYHOURS,s10);
                                    map.put(SEATSAVAILABLE,s12);
                                    map.put(TOTALTIMING,s14);

                                    busdetails.add(map);
   }

                                JSONArray array4 = obj2.getJSONArray("boardingPoint");

                               for (int l = 0; l < array4.length(); l++) {
                                    JSONObject obj4 = array4.getJSONObject(l);

                                    String s14 = obj4.getString("boardingPointName");
                                    String s15 = obj4.getString("boardingPointContact");
                                    String s16 = obj4.getString("boardingPointTime");
                                    String s17 = obj4.getString("boardingPointId");
                                    String s18 = obj4.getString("BusId");

                                }

                            }

                        }

                        Log.e("response", array1.toString());
                    } catch (JSONException e) {
                        e.printStackTrace();
                        Log.e("log_tag", "Error parsing data "+e.toString());

                    }

                }
                return null;
            }

            protected void onPostExecute(Void result){

                try {
    /*
                    if (array3.equals("Bus not available.")) {
    */
                    dialogProgress.dismiss();
                        ListAdapter adapter = new SimpleAdapter(Searchbus.this, busdetails, R.layout.busdetailslistview,
                                new String[]{TOTALTIMING, FARE, JOURNEYHOURS, SEATSAVAILABLE, AMENITIES, DEPARTTIME},
                                new int[]{R.id.bustiming, R.id.fare_amt, R.id.journey_hours, R.id.seats_avail, R.id.busnametpe, R.id.bustype, R.id.bt_viewseat});
       lv.setAdapter(adapter);
    Toast.makeText(Searchbus.this, "" + journeydatefromjson + ".." + busname, Toast.LENGTH_SHORT).show();
                }
                catch(Exception e){
                    e.printStackTrace();
                }
            }
        }`
my layout is `<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_searchbus"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.example.notebook.runtowin.Searchbus">

    <ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/mytoolbar"
    android:layout_marginTop="3sp">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center"
        android:id="@+id/datelayout"
        android:layout_alignParentTop="true"
        android:layout_marginTop="18sp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/dateview"
            android:hint="date"
            android:textSize="26sp"
            android:textColor="@color/colorPrimaryDark"/>

    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_below="@+id/datelayout"
        android:layout_marginTop="20sp"
        android:id="@+id/relativeLayout1">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Operator &amp; Departure"
    android:textSize="16sp"
    android:id="@+id/departtextview"
   android:padding="5sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Price"
            android:textSize="16sp"
            android:padding="5sp"
            android:layout_marginLeft="15sp"
            android:layout_marginStart="49dp"
            android:id="@+id/textView2"
            android:layout_alignParentTop="true"
            android:layout_toEndOf="@+id/departtextview" />


    </RelativeLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/relativeLayout1"
            android:layout_marginTop="10sp">

            <ListView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/listviewbusdetails"
                android:divider="@color/colorPrimaryDark"
                android:dividerHeight="1sp"/>
        </LinearLayout>


    </RelativeLayout>
</ScrollView>

    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="35sp"
        android:minHeight="?attr/actionBarSize"
        android:id="@+id/mytoolbar"
        android:background="@color/colorPrimaryDark"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/from_to"
            android:textColor="@color/colorwhite"/>

    </android.support.v7.widget.Toolbar>
</RelativeLayout>
` my listview code is `<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/busname"
        android:padding="4sp"
        android:layout_alignParentStart="true">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="gone"
            android:id="@+id/no_bus"
            android:text="no bus is available"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="nddnsadasndman"
            android:id="@+id/bustiming"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="bustype"
            android:padding="4sp"
            android:id="@+id/busnametpe"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="busslepper"
            android:id="@+id/bustype"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/price"
        android:layout_centerHorizontal="true">


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="450"
    android:id="@+id/fare_amt"
    android:gravity="center"
    android:layout_margin="15sp"
    android:textSize="23sp"
    android:layout_gravity="center"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="duration"
            android:id="@+id/journey_hours"

            android:gravity="center"
            android:layout_gravity="center"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/seats"
        android:layout_alignParentEnd="true">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="View seats"
            android:layout_margin="8sp"
            android:id="@+id/bt_viewseat"
            android:background="@color/colorPrimaryDark"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="seats"
            android:id="@+id/seats_avail"
            android:gravity="center"/>
    </LinearLayout>

</RelativeLayout>`

my logcat error is `12-29 14:53:35.435 13421-13421/com.example.notebook.runtowin W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
12-29 14:53:35.647 13421-13421/com.example.notebook.runtowin I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
12-29 14:53:35.651 13421-13421/com.example.notebook.runtowin I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
12-29 14:53:35.666 13421-13421/com.example.notebook.runtowin I/DpmTcmClient: RegisterTcmMonitor from: org.apache.http.impl.conn.TcmIdleTimerMonitor
12-29 14:53:35.692 13421-13459/com.example.notebook.runtowin D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
12-29 14:53:35.708 13421-13460/com.example.notebook.runtowin W/System: ClassLoader referenced unknown path: /system/framework/com.qualcomm.qti.GBAHttpAuthentication.jar
12-29 14:53:35.742 13421-13421/com.example.notebook.runtowin D/ActivityThreadInjector: clearCachedDrawables.
12-29 14:53:35.774 13421-13459/com.example.notebook.runtowin I/Adreno: QUALCOMM build                   : a7823f5, I59a6815413
                                                                       Build Date                       : 09/23/16
                                                                       OpenGL ES Shader Compiler Version: XE031.07.00.00
                                                                       Local Branch                     : mybranch22028469
                                                                       Remote Branch                    : quic/LA.BR.1.3.3_rb2.26
                                                                       Remote Branch                    : NONE
                                                                       Reconstruct Branch               : NOTHING
12-29 14:53:35.784 13421-13459/com.example.notebook.runtowin I/OpenGLRenderer: Initialized EGL, version 1.4
12-29 14:53:35.848 13421-13459/com.example.notebook.runtowin E/HAL: hw_get_module_by_class: module name gralloc
12-29 14:53:35.848 13421-13459/com.example.notebook.runtowin E/HAL: hw_get_module_by_class: module name gralloc
12-29 14:53:37.589 13421-13460/com.example.notebook.runtowin I/city: [{"cityId":"5","cityName":"Coimbatore"},{"cityId":"6","cityName":"Chennai"},{"cityId":"7","cityName":"Madurai"},{"cityId":"8","cityName":"Kanyakumari"},{"cityId":"9","cityName":"Nagercoil"},{"cityId":"12","cityName":"Anjugramam"},{"cityId":"11","cityName":"Erode"},{"cityId":"13","cityName":"Valliyur"},{"cityId":"14","cityName":"Kavalkinaru"},{"cityId":"15","cityName":"Tirunelveli"}]
12-29 14:53:37.606 13421-13421/com.example.notebook.runtowin I/InstituteName: [Coimbatore, 5, Chennai, 6, Madurai, 7, Kanyakumari, 8, Nagercoil, 9, Anjugramam, 12, Erode, 11, Valliyur, 13, Kavalkinaru, 14, Tirunelveli, 15]
`

1) Make sure you parse the json correctly. System.out.pritnln("text") and Log.d("some", "thing") are your friends.

2) In Android Studio there is an easy way to view the log, look for: 6: Android Monitor.

3) Check this excellent post about ListView and it's Adapters. Mock your data &| classes to make sure you get the input you want.

4) Building Android Apps in a hurry is a bad idea. Java / Android is not strong with fast building, Cordova / Javascript / HTML are better for that. But Java & Android have the power and speed when done right...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM