我正在编写一个可以解析从PHP网页返回的JSON的Android应用程序,并且我一直在Logcat中获得“org.json.JSONException:字符0处的输入结束”。这是代码:
public class HttpConnect {
HttpClient httpClient = new DefaultHttpClient();
final String url = "http://foo/bar/handler.php";
HttpPost httpPost = new HttpPost(url);
HttpParams httpParams = new BasicHttpParams();
HttpResponse httpResponse;
List<NameValuePair> params = new ArrayList<NameValuePair>();
JSONObject resultJson;
boolean status;
public boolean sendJson(String value) {
try {
params.add(new BasicNameValuePair("jsonString", URLEncoder.encode(
value, "utf-8")));
httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
httpResponse = httpClient.execute(httpPost);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
String result = EntityUtils.toString(httpResponse.getEntity());
resultJson = new JSONObject(result);
}
if (resultJson.getString("success") == "true")
status = true;
else
status = false;
} catch (Exception e) {
e.printStackTrace();
}
return status;
}
//...
}
服务器端:
$arr = array('success' => 'true', 'status' => 'RegSuccess', 'userid' => $userid, 'authkey' => $authKey);
$jsonString = json_encode($arr);
echo $jsonString;
邮差获得回复,这是正确的:
{
"success": "true",
"status": "RegSuccess",
"userid": "2",
"authkey": "OvOWwCjIXoYtbjsIdTVI"
}
但Android应用程序弹出错误:
08-07 16:44:34.044:W / System.err(13880):org.json.JSONException:字符0的输入结束 08-07 16:44:34.049:W / System.err(13880):at org.json.JSONTokener.syntaxError(JSONTokener.java:450) 08-07 16:44:34.049:W / System.err(13880):at org.json.JSONTokener.nextValue(JSONTokener.java:97) 08-07 16:44:34.049:W / System.err(13880):at org.json.JSONObject。(JSONObject.java:155) 08-07 16:44:34.049:W / System.err(13880):at org.json.JSONObject。(JSONObject.java:172) 08-07 16:44:34.049:W / System.err(13880):at com.lafickens.rescued.HttpConnect.sendJson(HttpConnect.java:49) 08-07 16:44:34.049:W / System.err(13880):at com.lafickens.rescued.LoginActivity $ UserRegisterTask.sendCredentials(LoginActivity.java:369) 08-07 16:44:34.049:W / System.err(13880):at com.lafickens.rescued.LoginActivity $ UserRegisterTask.doInBackground(LoginActivity.java:325) 08-07 16:44:34.049:W / System.err(13880):at com.lafickens.rescued.LoginActivity $ UserRegisterTask.doInBackground(LoginActivity.java:1) 08-07 16:44:34.049:W / System.err(13880):在android.os.AsyncTask $ 2.call(AsyncTask.java:288) 08-07 16:44:34.054:W / System.err(13880):at java.util.concurrent.FutureTask.run(FutureTask.java:237) 08-07 16:44:34.054:W / System.err(13880):在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231) 08-07 16:44:34.054:W / System.err(13880):at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 08-07 16:44:34.054:W / System.err(13880):at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:587) 08-07 16:44:34.054:W / System.err(13880):at java.lang.Thread.run(Thread.java:841)
调试显示字符串result
为空,contentLength
的{{1}}为0.相同的java代码对另一个Web服务完全正常,但不适用于我的。令人费解:s
感谢任何帮助:)