2011-11-20 103 views
11

我想从android解析json,但我得到这个奇怪的异常。我的JSON数据是org.json.JSON异常:字符0输入结束

{ “ID”: “1”, “主人”: “1”, “名”: “庄严”, “说明”: “是一个巨星”, “START_TIME”:” “00:00:00”,“end_time”:“0000-00-00 00:00:00”,“venue”:“vellore”,“radius”:“10”,“lat”:“ 11" , “LNG”: “11”, “类型”: “类型”, “OWNERNAME”: “迪利普”, “noofpolls”:0 “noofquizes”:0 “peopleattending”:0 “结果是”:真}

和Android的我做

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

我得到:

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

它有什么问题?下面是代码...

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

回答

23

哎呀解析一个片段!我的坏我本来应该使用GET方法。该网址不响应POST请求,所以我得到了org.json.JSON异常:在字符0.its输入结束,因为这我得到空响应,它产生的异常。

+0

嗨Rakon_188,我也得到了同样的问题..但我试着用不同的方式..你可以请给我完整的代码.. – wolverine

+0

在这里有同样的问题,但它看起来像我不得不切换我的POST而不是GET。看起来像这个错误被抛出,如果你只是错了,所以你可能需要简单地切换你的电话。 – Silmarilos

+0

谢谢男人:* ... –

-3

这里是如何使用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,这是不好[你破坏其他帖子](http://stackoverflow.com/review/suggested-edits/4443671),你正在与之竞争。 – gunr2171

-3

此错误也有时会导致json_encode函数要求所有传入数据编码为UTF-8

尝试在您的php中添加mysqli_set_charset($con, 'utf8');

0

就我而言,我指的是一个函数名,甚至不存在。检查您的Web服务的函数名,纠正函数名后,它为我工作!

相关问题