Adnan Quraishi
Adnan Quraishi

Reputation: 533

Cannot retrieve complete json response in Android logcat and Gives error or parsing

I am working on a application and making an http request which works fine ,

this is my Asynch class Doinbackground method:

@Override
    protected String doInBackground(Void... arg0) {


        HttpHandler sh = new HttpHandler();

        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(url);

        Log.d("Response ==>", "Response from url: ");
        longInfo(jsonStr);
        return jsonStr; 
    }

this is the logic i am using

        public void longInfo(String str) {
        if(str.length() > 4000) {
            Log.e("TAG", str.substring(0, 4000));
            longInfo(str.substring(4000));
        } else
            Log.e("TAG", str);
    }

but this gives me chunks of json string which breaks json format and i am unable to parse objects and arrays accordingly. kindly help

here is my parsing method of json

 public void parsejsonp(String jsonp){


        if (jsonp != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonp);
                int count = jsonObj.getInt("count");
                Log.e("count=> ",count+"");

                int count_total = jsonObj.getInt("count_total");
                Log.e("count_total=> ",count_total+"");

                int pages = jsonObj.getInt("pages");
                Log.e("pages=> ",pages+"");


                // Getting JSON Array node
                JSONArray Posts = jsonObj.getJSONArray("posts");

                // looping through All Posts
                for (int i = 0; i < Posts.length(); i++) {
                    JSONObject c = Posts.getJSONObject(i);

                    String id = c.getString("id");
                    String type = c.getString("type");
                    String slug = c.getString("slug");
                    String status = c.getString("status");
                    String title = c.getString("title");

                    String title_plain = c.getString("title_plain");

                    String content = c.getString("content");

                    String date = c.getString("date");

                    String modified = c.getString("modified");




                    // Phone node is JSON Object
                    //                     JSONObject phone = c.getJSONObject("phone");
                    //                   String mobile = phone.getString("mobile");
                    //                 String home = phone.getString("home");
                    //               String office = phone.getString("office");

                    // tmp hash map for single contact
                    HashMap<String, String> posting = new HashMap<>();

                    // adding each child node to HashMap key => value
                    posting.put("id", id);
                    posting.put("type", type);
                    posting.put("slug", slug);
                    posting.put("status",status);
                    posting.put("title", title);
                    posting.put("content", content);
                    posting.put("date", date);
                    posting.put("modified", modified);


                    // adding contact to contact list
                    postList.add(posting);
                }
            } catch (final JSONException e) {
                Log.e("error", "Json parsing error: " + e.getMessage());
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getActivity(),
                                "Json parsing error: " + e.getMessage(),
                                Toast.LENGTH_LONG)
                                .show();
                    }
                });

            }
        } else {
            Log.e("er server", "Couldn't get json from server.");
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getActivity(),
                            "Couldn't get json from server. Check LogCat for possible errors!",
                            Toast.LENGTH_LONG)
                            .show();
                }
            });

        }

    }

this is my onPost method:

@Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);


            Log.e("RESULT",result);

            parsejsonp(result);
            // Dismiss the progress dialog
            if (pDialog.isShowing())
                pDialog.dismiss();
            /**
             * Updating parsed JSON data into ListView
             * */
            ListAdapter adapter = new SimpleAdapter(
                    getActivity(), postList,
                    R.layout.list_item2, new String[]{"type", "slug","status",
                    "title","content","date","modified"}, new int[]{R.id.type,
                    R.id.slug,R.id.state_p,R.id.title_p, R.id.content,R.id.date_p,R.id.moddate_p});

            lv.setAdapter(adapter);
        }

but still i am unable to parse all the data because still in my onPost method in result parameter i am not having complete response

i have also checked the url on browser the response is showing completely there. but if it is not shown completely in logcat due to buffer size it must put all the data in my listview but it is not

here is the screen shot of my listview

enter image description here

as you can see only one object data is inserted but rest of the response is not inserted , i have debugged all the code and checked the arraylist size it is storing complete number of objects , but my screen seems to show only one object data :

here is my layout.xml file may be the problem is here :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragment">



    <android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="fill_vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">


        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context="com.adnan.zwd.hidoctor.Patient.Frag_two">

            <ListView
                android:id="@+id/list2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" />
        </RelativeLayout>




    </android.support.v4.widget.NestedScrollView>

</RelativeLayout>

row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="@dimen/activity_horizontal_margin">

    <TextView
        android:id="@+id/type"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="2dip"
        android:paddingTop="6dip"
        android:text="type"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="16sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/slug"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="slug"
        android:paddingBottom="2dip"
        android:textColor="@color/colorAccent" />
    <TextView
        android:id="@+id/state_p"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="status"
        android:paddingBottom="2dip"
        android:textColor="@color/colorAccent" />

    <TextView
        android:id="@+id/title_p"
        android:text="title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#5d5d5d"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/content"
        android:text="content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#5d5d5d"
        android:textStyle="bold" />
    <TextView
        android:id="@+id/date_p"
        android:text="date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#5d5d5d"
        android:textStyle="bold" />
    <TextView
        android:id="@+id/moddate_p"
        android:text="Mod Date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#5d5d5d"
        android:textStyle="bold" />

</LinearLayout>

Any help regarding this issue , i searched a lot on internet but no success

Upvotes: 0

Views: 2454

Answers (3)

OneCricketeer
OneCricketeer

Reputation: 191728

and it gives exception for parsing.

What exception? Here maybe?

new JSONObject(str.substring(4000));

You can't just cut the middle of a JSONObject. The first { or [ character matters.

You can use the JsonReader class to parse the InputStream instead of reading the stream to a BufferedReader, then a string.


Or...

You aren't meant to parse from that method. The string is already in memory in order to get passed into that method.

@Override
protected String doInBackground(Void... arg0) {
    HttpHandler sh = new HttpHandler();

    // Making a request to url and getting response
    String jsonStr = sh.makeServiceCall(url);

    Log.d("Response ==>", "Response from url: ");
    longInfo(jsonStr);
    return jsonStr;  // change your AsyncTask from Void to String return type 
}

// standalone method 
public static void longInfo(String str) {
    if(str.length() > 4000) {
        Log.i(TAG, str.substring(0, 4000));
        longInfo(str.substring(4000));
    } else
        Log.i(TAG, str);
}

here is my layout.xml file may be the problem is here

Yup, there is an issue there.

You should never use a ScrollView with a ListView, because ListView takes care of its own vertical scrolling

Android | ScrollView

This is all you need for a Fragment layout

<?xml version="1.0" encoding="utf-8"?>
<ListView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/list2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
/>

Upvotes: 1

Adnan Quraishi
Adnan Quraishi

Reputation: 533

Finally i found the problem . It was all happening because of android.support.v4.widget.NestedScrollView in my layout

and i solved it by adding android:fillViewport="true" attribute in nestedscrollview

as mentioned here :

ListView not expanding inside NestedScrollView

Upvotes: 1

bond007
bond007

Reputation: 2494

GoTo Android_Studio_Path\bin\idea.properties

Change the BufferSize and increase it to 2048

idea.cycle.buffer.size=1024

Upvotes: 0

Related Questions