在我的一个应用程序中,有用PHP开发的web服务调用。我已经使用了常用方法来请求 JSON 格式的 Web 服务响应。我编写parseJson()
的方法在CommonUtiliy文件中,这是可重用的。我在整个应用程序中使用相同的方法。
现在我的问题是请求响应的parseJson()
方法在几天前运行良好,但现在它对某些 Web 服务根本不起作用。我无法获得某些网络服务的响应,并收到以下错误。我得到空响应。
任何人都可以为此提供指导吗?为什么会这样?就像以前一样。
错误:
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)
请求方法:
/*
* 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;
}
编辑:
但是,如果我只在我的 java 文件中使用相同的代码,除了从 commonutils 文件访问它之外,那么它就可以正常工作。如下:
@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);
在这里,我成功得到了响应。不知道为什么会这样。
提前谢谢。
区别在于:
BufferedReader reader = new BufferedReader(
new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8"));
json = reader.readLine();
和
BufferedReader bf = new BufferedReader(new InputStreamReader(in));
while ((line = bf.readLine()) != null) {
sb.append(line);
}
m_response = sb.toString();
第一个只读取响应中的第一行 - bf.readLine()
读取直到找到流的末尾或行尾(第一个换行符);而第二个读取所有行,直到结束流 - 因此您有一个完整的 JSON 响应。在这两种情况下,您都会自己记录响应。你为什么不看看Logcat?
将顶部更改为:
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();
应该做这个技巧