0

I'm writing an Android app that can parse JSON returned from PHP webpages, and I keep getting "org.json.JSONException: End of input at character 0" in Logcat. Here's the code:

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;
    }
    //...
}

Server side:

$arr = array('success' => 'true', 'status' => 'RegSuccess', 'userid' => $userid, 'authkey' => $authKey);
$jsonString = json_encode($arr);
echo $jsonString;

Postman gets the response, which is correct:

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

But the android app pops error:

08-07 16:44:34.044: W/System.err(13880): org.json.JSONException: End of input at character 0 of 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): at 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): 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)

Debugging shows string result is empty, and contentLength of httpResponse is 0. The same java code works perfectly fine for another web service, but not for mine. Puzzling :s

Any help is appreciated:)

0

1 Answer 1

0

My suggestion is use Volley for your networking operations . It is for faster and better networking.

It nicely plays with all kinds of Json requests and also String request.

Sign up to request clarification or add additional context in comments.

1 Comment

Yep it works. Thank you so much! But the question remains: why can't 'httpPost' and 'httpResponse' get response the way Volley does?... Any clue?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.