1

i am trying to get JSON values from server. I don't know what is wrong with my code. I am using AsyncTask and here is my code in doInBackground

Code:

@Override
    protected Void doInBackground(List<String>... articlesRec) {
        for (List<String> articlesPostValue : articlesRec) {
            event = articlesPostValue.get(0);
            category = articlesPostValue.get(1);
            paged = articlesPostValue.get(2);
            deviceID = articlesPostValue.get(3);
        }

        List<NameValuePair> articlesPostValues = new ArrayList<NameValuePair>();
        articlesPostValues.add(new BasicNameValuePair("_event", event));
        articlesPostValues.add(new BasicNameValuePair("cat", category));
        articlesPostValues.add(new BasicNameValuePair("paged", paged));
        articlesPostValues.add(new BasicNameValuePair("_deviceID", deviceID));

        HttpClient hc = new DefaultHttpClient();
        HttpPost hp = new HttpPost(AutoLifeConstants.BASE_URL);

        try {
            hp.setEntity(new UrlEncodedFormEntity(articlesPostValues));

            // Execute HTTP Post Request
            HttpResponse response = hc.execute(hp);

            String result = EntityUtils.toString(response.getEntity());
            String jsontext = new String(result);

            JSONObject entries = new JSONObject(jsontext);

            this.response = entries.getString("response_code");

            try {
                String totalPageStr = entries.getString("total_pages");
                totalPage = Integer.valueOf(totalPageStr);
            } catch (Exception e) {

            }

            JSONArray postListArray = entries.getJSONArray("posts_list");

            for (int i = 0; i < postListArray.length(); i++) {
                JSONObject postListObj = postListArray.getJSONObject(i);

                String articlesTitle = postListObj.getString("title");
                String dateTime = postListObj.getString("date");
                String articlesImage = postListObj.getString("feature_image");
                String descrition = postListObj.getString("content");
                String fullDescription = postListObj.getString("full_content");

                articlesNewesttitle.add(articlesTitle);
                articlesNewestPostTime.add(dateTime);
                articlesNewestPostImage.add(articlesImage);
                articlesNewestPostDescription.add(descrition);
                postFullDescription.add(fullDescription);

                Log.d("Title",articlesTitle);
            }

        } catch (Exception e) {
            Log.e("catched error","Exceptin occured");
            e.printStackTrace();
        }

        return null;
    }

And here is catch(Exception e) on logcat

    06-19 13:22:32.229: W/System.err(19647): org.json.JSONException: End of input at character 0 of 
06-19 13:22:32.264: W/System.err(19647):    at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
06-19 13:22:32.265: W/System.err(19647):    at org.json.JSONTokener.nextValue(JSONTokener.java:97)
06-19 13:22:32.268: W/System.err(19647):    at org.json.JSONObject.<init>(JSONObject.java:154)
06-19 13:22:32.269: W/System.err(19647):    at org.json.JSONObject.<init>(JSONObject.java:171)
06-19 13:22:32.270: W/System.err(19647):    at np.com.autolife.adapters.NewsActivity$Get_postlist.doInBackground(NewsActivity.java:169)
06-19 13:22:32.272: W/System.err(19647):    at np.com.autolife.adapters.NewsActivity$Get_postlist.doInBackground(NewsActivity.java:1)
06-19 13:22:32.272: W/System.err(19647):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
06-19 13:22:32.273: W/System.err(19647):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
06-19 13:22:32.273: W/System.err(19647):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
06-19 13:22:32.274: W/System.err(19647):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
06-19 13:22:32.274: W/System.err(19647):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
06-19 13:22:32.281: W/System.err(19647):    at java.lang.Thread.run(Thread.java:838)

Here is JSON format

    {
    "get": [],
    "post": {
        "_event": "get_posts",
        "_deviceID": "490154203237518",
        "cat": "2",
        "paged": "1"
    },
    "response_code": "success",
    "posts_total": 8,
    "total_pages": 2,
    "paged": 1,
    "posts_list": [
        {
            "ID": 9168,
            "title": "MAXXIS Introduce Trepador Tires",
            "content": "",
            "full_content": "http:xxxxxx",
            "date": "June 13, 2014",
            "category": "All News & Events, Local News",
            "feature_image": "http:xxxxx.jpg",
            "feature_image_thumb": "http:xxxx.jpg",
            "author": "AutoLife Team"
        },
        {
            "ID": 9162,
            "title": "5 Year Warranty On All Hero Motorcycles",
            "content": "",
            "full_content": "http://xxxxx",
            "date": "June 13, 2014",
            "category": "All News & Events, Local News",
            "feature_image": "http://xxxxxx.jpg",
            "feature_image_thumb": "http://xxxx.jpg",
            "author": "AutoLife Team"
        },
        {
            "ID": 8130,
            "title": "All new 11th Generation Toyota Corolla Launched",
            "content": "",
            "full_content": "http://xxxxxx",
            "date": "May 1, 2014",
            "category": "Events & Schemes",
            "feature_image": "http://xxxxxx.png",
            "feature_image_thumb": "http://xxxxxx.png",
            "author": "supervisor"
        },
        {
            "ID": 9178,
            "title": "New Launches From TATA Motors : TATA Nano Twist",
            "content": "",
            "full_content": "http://xxxxxx",
            "date": "February 15, 2014",
            "category": "All News & Events, International News",
            "feature_image": "http://xxxxx.jpg",
            "feature_image_thumb": "xxxxxxx.jpg",
            "author": "AutoLife Team"
        },
        {
            "ID": 9175,
            "title": "New Launches From TATA Motors : Revotron Engine",
            "content": "",
            "full_content": "xxxxxxx",
            "date": "February 15, 2014",
            "category": "All News & Events, International News",
            "feature_image": "http://xxxxx.jpg",
            "feature_image_thumb": "http://xxxxx.jpg",
            "author": "AutoLife Team"
        }
    ]
}
10
  • i have update the question with adding JSON Commented Jun 19, 2014 at 8:06
  • 1
    what is line number 169? Commented Jun 19, 2014 at 8:11
  • This is not the problem, but why are you doing this: String jsontext = new String(result); Commented Jun 19, 2014 at 8:13
  • @user3751280 check jsontext having some values. Commented Jun 19, 2014 at 8:20
  • @GiruBhai line 169 is ' JSONObject entries = new JSONObject(jsontext); ' . Commented Jun 19, 2014 at 8:21

3 Answers 3

6

The reason why you get this error is because you attempt to access the response more than once. For example lets lets say you do a log with the response, then create a json object with the same response object, it will trigger this one time acees error.

Sign up to request clarification or add additional context in comments.

2 Comments

This is an Actual Hidden point !
correct Answer!
2

You are probably getting a blank response. Its not null but the jsontext is empty. So you are getting this error and not a Nullpointer exception

Are you sending right parameters to server.Also Check url respond to POST requests or not.

3 Comments

That would be unbelievable as the OP is posting the response one would think.
@user3751280 imeplement it using GET
@GiruBhai i have same problem..and i am sending right parameters yet getting error
0

Check if the response is not empty before process the JSON:

if (response.success()) {
    if (response.getData() == null) {
        return null;
    } else if (response.getData().length() <= 0){
        return null;
    }

    try {
        // Logic

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.