15

I have an API in php, that sends data in JSON format. I made the following code, that works fine when I am on wifi. But when I want to download the data from the API when I am on 3g, I receive the following exception: JSONException: End of input at character 0 of

I have no idea why it does work on wifi, but it doesn't on mobile internet. My code:

        JSONObject json = getJSONfromURL("http://api.myurl.com/users.json");

        JSONArray objects = json.getJSONArray("objects");
        db.setLockingEnabled(false);
        db.beginTransaction();

        for (int i = 0; i < objects.length(); i++) {
            JSONObject e = objects.getJSONObject(i);

            if(e.getString("UID") != "-1"){
                ContentValues values = new ContentValues();
                //DO DATABASE INSERT. REMOVED THIS CODE FOR READABILITY
                alldata_mProgressDialog.incrementProgressBy(1);
            }
        }

Anyone that can help me out?

5
  • where is the API located? Is it available while not connected to the internal network? Commented Jan 5, 2012 at 9:49
  • 3
    First of all, check whether you are getting exact response. Print your json value on console. Commented Jan 5, 2012 at 9:51
  • 1
    @PareshMayani agree, I think JSON response may be missing something. Commented Jan 5, 2012 at 10:01
  • @ mkfinest: The API is is available online, it works on every wifi network (at home, too). @PareshMayani: if I wouldn't get an exact response, it wouldn't work in wifi either? Commented Jan 5, 2012 at 10:02
  • @harmjanr be sure to always null check your response. The mind set should be im lucky if I got a response, with that approach you at least avoid some potential pitfalls. Commented Jan 5, 2012 at 10:09

6 Answers 6

51

You are probably getting a blank response. Its not null but the response is empty. So you are getting this error and not a Nullpointer exception

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

5 Comments

The response is null indeed. But why? Because the data sent from server is fine, it looks like my mobile internet connection is cutting the response or something.. Because, as mentioned before, it works fine on wifi.
check for some proxy issues. Is the url exposed on the internet?
How to block the error? Is there a way to detect it? Thank you
internet issue show it's getting this error, double check your internet connection
The response is Not Empty, but the problem arise. Why ?
5

Check if you are asking for permissions to use the Internet.

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

in the Android Manifest

Check out others at Permissions overview @ https://developer.android.com.

Comments

4

May be you are getting default response with default values. This kind of error comes up when you are not sending your requests properly or sending with wrong parameters. Check This

Comments

2

Sometimes this error is caused as json encode function requires all incoming data to be UTF-8 encoded.

Try adding mysqli_set_charset($con, 'utf8'); into your php.

Comments

0

This kind of error comes up when response is null.Print Json respone in logcat

Log.e ( "response", "" + response ), or you can check response on postman also.

Comments

0

I got this error while debugging an android app on Android Studio via USB. Connection attempts failed with one tablet but not with my regular test tablet. It turned out that the failure was due to the fact that I hadn't enabled WIFI settings on the failing machine. So I enabled WIFI on it and it connected to server OK.

Comments

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.