2011-11-20 9 views
11

Sto cercando di analizzare JSON da Android ma ottengo questa strana eccezione. I miei dati JSON èorg.json.JSON Eccezione: fine dell'input al carattere 0

{ "id": "1", "proprietario": "1", "name": "gravitas", "Descrizione": "è una festa", "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": "tipo", "ownername": "dilip", "noofpolls": 0, "noofquizes": 0, "peopleattending": 0, "risultato": true }

e in Android faccio

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

ottengo:

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

Cosa c'è di sbagliato in questo? Ecco il codice ...

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(); 
     } 

risposta

23

Oops! il mio male dovevo usare il metodo GET.che l'url non rispondeva alle richieste del POST quindi stavo ottenendo l'eccezione org.json.JSON: fine dell'input al carattere 0.its a causa di ciò ho ricevuto una risposta nullo che ha generato quell'eccezione .

+0

Ciao Rakon_188, sto anche ricevendo lo stesso problema .. ma ci sto provando in un modo diverso .. puoi mandarmi il codice completo .. – wolverine

+0

Aveva lo stesso problema qui, ma sembra che dovessi cambiare il mio al POST anziché GET. Sembra che questo errore venga generato se hai semplicemente sbagliato, quindi potresti dover semplicemente cambiare la chiamata. – Silmarilos

+0

grazie uomo: * ... –

-3

Ecco un frammento su come analizzare utilizzando 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); 

... 
+4

@ user3458711, non è bello [vandalizzare altri post] (http://stackoverflow.com/review/suggested-edits/4443671) con cui si sta facendo. – gunr2171

-3

Questo errore è anche causato a volte come la funzione json_encode richiede che tutti i dati in entrata siano codificati UTF-8.

Prova ad aggiungere mysqli_set_charset($con, 'utf8'); nel tuo php.

0

Nel mio caso, mi riferivo a un nome funzione di, che non esisteva nemmeno .. Verificare il nome della funzione del servizio web, dopo aver corretto il nome della funzione, ha funzionato per me!

Problemi correlati