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.