11

I'm trying to parse json from android but I get this strange exception. My json data is

{"id":"1","owner":"1","name":"gravitas","description":"is a fest","start_time":"0000-00-00 00:00:00","end_time":"0000-00-00 00:00:00","venue":"vellore","radius":"10","lat":"11","lng":"11","type":"type","ownername":"dilip","noofpolls":0,"noofquizes":0,"peopleattending":0,"result":true}

and in android I do

JSONObject j =new JSONObject(response);
Event pst = gson.fromJson(j.toString(),  Event.class);

I get:

org.json.JSONException: end of input at character 0 of

What's wrong with it? Here is the code...

RestClient client = new RestClient("http://192.168.1.3/services/events/"+eve.getName());         
        try {
             Log.i("MY INFO", "calling boston");
            client.Execute(RequestMethod.POST);
        } catch (Exception e) {
            e.printStackTrace();
        }

        String response = client.getResponse();
         Log.i("MY INFO", response);
         GsonBuilder gsonb = new GsonBuilder();
         Gson gson = gsonb.create();
         Event pst = null;

        try {
            JSONObject j =new JSONObject(response);
            pst = gson.fromJson(j.toString(),  Event.class);

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
user3678528
  • 1,741
  • 2
  • 18
  • 24
Rakon_188
  • 573
  • 2
  • 7
  • 15

4 Answers4

23

Oops! my bad I was supposed to use GET method.that url doesn't respond to POST requests so I was getting the org.json.JSON Exception : End of input at character 0.its because of this I got null response which generated that exception.

user3678528
  • 1,741
  • 2
  • 18
  • 24
Rakon_188
  • 573
  • 2
  • 7
  • 15
  • Hi Rakon_188, am also getting the same problem.. but i'm trying it in a different way.. can you please send me the full code.. – wolverine Apr 19 '12 at 07:11
  • Had the same problem here, but it looks like I had to switch mine to POST instead of GET. It seems like this error gets thrown if you simply have the wrong one so you may need to simply switch your call. – PGMacDesign Feb 03 '15 at 06:19
0

In my case, i was referring to a function name, that didn't even exist.. Check the function name of your web service, after correcting the function name, it worked for me !

mayur saini
  • 209
  • 7
  • 17
-3

This error is also caused sometimes as json_encode function requires all incoming data to be UTF-8 encoded.

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

V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50
truespan
  • 189
  • 1
  • 2
  • 11
-3

here is a snippet on how to parse using GSON

    Gson gson = new GsonBuilder().create();
    JsonParser jsonParser = new JsonParser();

     String response = client.getResponse();
    JsonObject jsonResp = jsonParser.parse(response).getAsJsonObject();
    // Important note: JsonObject is the one from GSON lib!
    // now you build as you like e.g.
    Event mEvent = gson.fromJson(jsonResp, Event.class);

...
Yilmaz Guleryuz
  • 9,313
  • 3
  • 32
  • 43
  • 4
    @user3458711, it's not nice to [vandalize other posts](http://stackoverflow.com/review/suggested-edits/4443671) that you are compeating with. – gunr2171 Mar 28 '14 at 02:19