Android错误解析后响应org.json.JSONException: End of input at charac


Android error parsing post response org.json.JSONException: End of input at character 0 of

我正在编写一个Android应用程序,可以解析从PHP网页返回的JSON,我一直得到"org.json。JSONException: Logcat中字符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;

Postman得到响应,这是正确的:

{
    "success": "true",
    "status": "RegSuccess",
    "userid": "2",
    "authkey": "OvOWwCjIXoYtbjsIdTVI"
}

但是android应用程序弹出错误:

08-07 16:44:34.044: W/System.err(13880): org.json.JSONException: End of input at character 0 of08-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. rescuedhttpconnect . sendjson (HttpConnect.java:49)08-07 16:44:34.049: W/System.err(13880): at com.lafickens.rescue . loginactivity $UserRegisterTask.sendCredentials(LoginActivity.java:369)08-07 16:44:34.049: W/System.err(13880): at com.lafickens.rescue . loginactivity $UserRegisterTask.doInBackground(LoginActivity.java:325)08-07 16:44:34.049: W/System.err(13880): at com.lafickens.rescue . loginactivity $UserRegisterTask.doInBackground(LoginActivity.java:1)08-07 16:44:34.049: W/System.err(13880): at android.os.AsyncTask$2.call(AsyncTask.java:288)08-07 16:44:34.054: W/System.err(13880): at java.util.concurrent. futurettask .run(futurettask .java:237)08-07 16:44:34.054: W/System.err(13880): at 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为空,httpResponsecontentLength为0。相同的java代码在另一个web服务中工作得很好,但不适合我的web服务。令人费解:s

任何帮助都是感激的:)

我的建议是使用Volley进行网络操作。这是为了更快更好的联网。

它可以很好地处理各种Json请求和字符串请求