0

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

Addi.Star
  • 475
  • 2
  • 15
  • 36

3 Answers3

1

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"
/>
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Actually the problem is not with the parsing , The response which i am getting is incomplete in logcat , and when i use str.substring(0,4000); it gives chunks of complete json and json is broken in this way . and unable to be parsed – Addi.Star Jan 22 '17 at 14:44
  • You shouldn't parse from the logcat. You don't even need to use the logcat, so I don't understand the issue – OneCricketeer Jan 22 '17 at 14:46
  • If you're relying on the logcat to debug your API, that's the wrong approach. Use a REST client like Postman to do that, then parse appropriately in your app – OneCricketeer Jan 22 '17 at 14:49
  • i am using the logcat for debugging my response from url , firstly i should check for complete response is receiving and then i start further parsing but but but my response is shown incomplete in logcat , i also tried to parse it but the data is only parsed of the string which is incomplete and hence data shown is incomple – Addi.Star Jan 22 '17 at 14:49
  • my response contains 10 posts arrays , only one is shown on the device listview – Addi.Star Jan 22 '17 at 14:50
  • If you an incomplete data, that's completely unrelated to your logcat. And, like a said, that could very easily be a server issue, and logcat isn't intended to be dumping ground for really long strings, which is why that question you found exists. It's unclear why you think you are meant to parse the chunks of strings. If your listview doesn't show all the data, show the actual parsing code you are using – OneCricketeer Jan 22 '17 at 14:56
  • ok i should edit all the details thats a but messed up code . – Addi.Star Jan 22 '17 at 14:58
  • No this issue is not solved, check my updated question, – Addi.Star Jan 22 '17 at 15:07
  • Yes, it is answered. I told you not to parse in that method – OneCricketeer Jan 22 '17 at 15:09
  • then how can i retrieve the complete json back kindly provide any example i should accept as a answer , i need help – Addi.Star Jan 22 '17 at 15:11
  • I use Volley, not that HttpHandler class that you probably copied from some tutorial online. https://developer.android.com/training/volley/request.html#request-json – OneCricketeer Jan 22 '17 at 15:14
  • check my updated question the problem still persists after applying your solution – Addi.Star Jan 22 '17 at 20:02
  • Suggestion 1: Create Java objects that represent your JSON objects. Don't use a HashMap. Suggestion 2: Use Gson or Jackson to simplify your massive JSON parsing into about only 4 lines. Suggestion 3 since I can't see your actual JSON data, debug your parsing code because once again, the Logcat isn't the main issue here. – OneCricketeer Jan 22 '17 at 20:06
  • https://www.hidoctor.pk/api/get_posts/ this is the link from where i am geting json data . – Addi.Star Jan 22 '17 at 20:11
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/133758/discussion-between-addi-star-and-cricket-007). – Addi.Star Jan 22 '17 at 20:12
1

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

Community
  • 1
  • 1
Addi.Star
  • 475
  • 2
  • 15
  • 36
  • Or you shouldn't use a NestedScrollView at all because ListView already scrolls on its own – OneCricketeer Jan 22 '17 at 21:26
  • *You should never use a ScrollView with a ListView, because ListView takes care of its own vertical scrolling* -- https://developer.android.com/reference/android/widget/ScrollView.html – OneCricketeer Jan 22 '17 at 21:26
  • just one last confusion , if i dont use nestedscroll then it will kill the scroll of appbar widget , which i dont want , i want to scroll listview with scroll behaviour of appbar widget , thats why i used nestedscrollview – Addi.Star Jan 22 '17 at 21:29
  • That's a separate problem, then. The NestedScrollView would go around a FrameLayout *in the Activity layout*, not within the Fragment – OneCricketeer Jan 22 '17 at 21:57
0

GoTo Android_Studio_Path\bin\idea.properties

Change the BufferSize and increase it to 2048

idea.cycle.buffer.size=1024
bond007
  • 2,434
  • 1
  • 17
  • 17
  • i am able to get full response only when use substring logic and after geting it is broken unparseable for json Object – Addi.Star Jan 22 '17 at 14:08