0

In one of my application there is webservice calling which is developed in PHP. And i have used the common method for requesting the webservice response which is in JSON format.The method parseJson() i have written is in CommonUtiliy file and this is reusable. I use the same method in all over application.

Now my problem is the parseJson() method for requesting the response was working perfectly few days before but now its not working at all for some webservices. I am not able to get the response for some webservice and i get the below error. I am getting null response.

Can any one guide for this? Why this is happening? As it was working before.

Error:

10-29 06:40:04.381: W/System.err(3404): JSON Response--->
10-29 06:40:04.381: W/System.err(3404): org.json.JSONException: End of input at character 0 of 
10-29 06:40:04.381: W/System.err(3404):     at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
10-29 06:40:04.381: W/System.err(3404):     at org.json.JSONTokener.nextValue(JSONTokener.java:97)
10-29 06:40:04.381: W/System.err(3404):     at org.json.JSONObject.<init>(JSONObject.java:154)
10-29 06:40:04.381: W/System.err(3404):     at org.json.JSONObject.<init>(JSONObject.java:171)
10-29 06:40:04.381: W/System.err(3404):     at com.zeal.peak.adapter.GridAdapter$callLikeWS.doInBackground(GridAdapter.java:381)
10-29 06:40:04.381: W/System.err(3404):     at com.zeal.peak.adapter.GridAdapter$callLikeWS.doInBackground(GridAdapter.java:1)
10-29 06:40:04.381: W/System.err(3404):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-29 06:40:04.381: W/System.err(3404):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
10-29 06:40:04.381: W/System.err(3404):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-29 06:40:04.381: W/System.err(3404):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
10-29 06:40:04.381: W/System.err(3404):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
10-29 06:40:04.393: W/System.err(3404):     at java.lang.Thread.run(Thread.java:856)

Request Method:

  /*
 * Call the Webservice read the Json response and return the response in
 * string.
 */
public static String parseJSON(String p_url) {

    String json = null;
    try {
        // Create a new HTTP Client
        DefaultHttpClient defaultClient = new DefaultHttpClient();

        // Setup the get request
        HttpGet httpGetRequest = new HttpGet(BaseUrl + p_url);
        System.out.println("Request URL--->" + BaseUrl + p_url);
        // Execute the request in the client
        HttpResponse httpResponse = defaultClient.execute(httpGetRequest);
        // Grab the response
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                httpResponse.getEntity().getContent(), "UTF-8"));
        json = reader.readLine();
        System.err.println("JSON Response--->" + json);


    } catch (Exception e) {
        // In your production code handle any errors and catch the
        // individual exceptions
        e.printStackTrace();
    }
    return json;
}

EDITED:

But if i use the same code in my java file only besides accessing it from the commonutils file then its working fine. As below:

         @Override
    protected String doInBackground(String... params) {

        String m_response = null;
        HttpClient client = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(BaseUrl
                + "notification_list.php?uid=" + m_userID);
        HttpResponse response;
        try {
            response = client.execute(httpget);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                InputStream in = response.getEntity().getContent();
                StringBuilder sb = new StringBuilder();
                String line = "";
                BufferedReader bf = new BufferedReader(
                        new InputStreamReader(in));
                while ((line = bf.readLine()) != null) {
                    sb.append(line);
                }
                m_response = sb.toString();
            }
        } catch (ClientProtocolException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

    System.err.println("Response=$^^^^^^^^^^" + m_response);

Here i get the response successfully. Don't know why this is happening.

Thanks in advance.

GrIsHu
  • 29,068
  • 10
  • 64
  • 102

1 Answers1

0

The difference lies here, between:

BufferedReader reader = new BufferedReader(
    new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8"));
json = reader.readLine();

and

BufferedReader bf = new BufferedReader(new InputStreamReader(in));
while ((line = bf.readLine()) != null) {
    sb.append(line);
}
m_response = sb.toString();

The first reads only the first line from response - bf.readLine() reads until end of stream or end of line is found (the first line break); while the second reads all the lines until the end stream - so you have a complete JSON response. You are logging the response yourself in both cases. Why didn't you had a look in Logcat?

Changing top section to:

BufferedReader reader = new BufferedReader(new InputStreamReader(
    httpResponse.getEntity().getContent(), "UTF-8"));
StringBuilder sb = new StringBuilder();
String json = null;
while((json = reader.readLine()) != null) {
    sb.append(json);
}
json = sb.toString();

should do the trick

gunar
  • 14,660
  • 7
  • 56
  • 87
  • may be you are right but actually i am not getting the response at all so the thing for reading the line far from that. Though the response is null already. – GrIsHu Oct 29 '13 at 07:59
  • `actually i am not getting the response at all` you're not mentioning this in your question. How are we supposed to know that??? Anyway, check why you're not getting the answer - make a request in browser with the same URL and if it works check if you have `INTERNET` permission set in your app ... – gunar Oct 29 '13 at 08:30
  • i already checked the things in browser already. And it works fine in browser already and i also provided the internet permission already . As i already mentioned in my question that it works fine when i use the same thing in my doInBackground method. – GrIsHu Oct 29 '13 at 09:27
  • Then what exactly is the problem? If it works in `doInBackground`, the `json` String value is a valid JSON? – gunar Oct 29 '13 at 09:33
  • The issue i do not want to repeat the same code for each webservice calling. So i have kept that in commonutils file from where i just use `parseJson` method but it does not work for all the webservices ,whereas for some webservices i get successfull response. – GrIsHu Oct 29 '13 at 09:49
  • `but it does not work for all the webservices ,whereas for some webservices i get successfull response.` Until you post an understandable question, I doubt someone will be able to answer. – gunar Oct 29 '13 at 09:52