【发布时间】:2013-11-08 04:58:21
【问题描述】:
在我的一个应用程序中,有一个用 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);
在这里我成功地得到了响应。不知道为什么会这样。
提前致谢。
【问题讨论】:
-
你的网址是什么?是http还是https?
-
@GrIsHu 您尝试接收的 json 可能不是完整的 json,因此是错误。检查 chrome 浏览器中的 url,如果你得到完整的结果,那么在如何获得响应方面存在错误。否则打印字符串响应
-
并检查是否在 php Web 服务中突然设置了编码。如果有,请将其更改为 UTF-8 编码。
-
在浏览器中运行流畅,json响应也正常。我已经检查并测试了它。 @DharaShah
标签: android json web-services httpclient httpconnection