【问题标题】:org.json.JSON Exception : End of input at character 0 atorg.json.JSON 异常:输入在字符 0 处结束
【发布时间】:2016-10-04 06:45:31
【问题描述】:

收到的错误是“org.json.JSON Exception : End of input at character 0 at”

我的 GET JSON 解析器

// request method is GET
            DefaultHttpClient httpClient = new DefaultHttpClient();
            String paramString = URLEncodedUtils.format(params, "utf-8");
            url += "?" + paramString;
            HttpGet httpGet = new HttpGet(url);
            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();

//IS 在这里已经被截断了。为什么?

            is = httpEntity.getContent(); 
            String jsonString=convertStreamToString(is);
            try{
                response = new JSONObject((jsonString.substring(jsonString.indexOf("{"), jsonString.lastIndexOf("}")+1)));
            }catch (Exception e){e.printStackTrace();}

我收到 json (这是正确的,在此处检查 http://jsonlint.com/

php 给出以下输出(在浏览器中),所以这意味着 php 工作正常

{"profile":[{"image":"iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHLAAAABHNCSVQICAgIfAhkiAAAIABJREFU\neJzcvWuMZdl13\/e71ftWr9NzbnOfYRV5j9RN1mVmCNaYDKfbppFuhBE1BBNoAskhmcSxCU8cKYAB\nOxYcGwnyJbDz+pAPVvxIPkq2DMgOYFiyZXgIROCQAp0e2GN3j0Bm7sg90W17WjpXmjLPnumjPqu7\nd1flw9r7nFM9MxQp8SHnzDSq6j7OY++11+O\/\/mvtGf8\/PP78j\/8HHz95sOOLM7NPAcxi\/6mgPb+x\nDYgXts3247OHVFIWaNfThsDJCe3h4dO\/QoTN7VcpRJjBV+5GmM35iszPhy\/\/03\/xK9\/nR\/uOH7Pv\n9w38Xo8\/\/Afe9\/EHcfZDZ+c8vd3+5qcKkZX3nkoKVqsl9V5Nc9SwubkBKWhDS1FWiECzbQCh3qtR\nVTa3NogIvvRswxZfCiJCVVa0ETT2HK4ONk2z\/YqXsy\/\/2f\/qz\/zyM\/\/ZT\/4bLRT\/xgnAYS0fPJ7v\n\/pHiXPEp3OxTs0ilndKqUohABBEgKgLUezV17ekVtneCTbL31L6iVygctKEFJxCVEALeeypf......T0AAAAASUVORK5CYII=\n"}],"success":1,"message":"profile pic found"} 

(.....意味着我删除了其中的一部分以显示在这里)

这是正确的。 但是当它通过所有'is'=>(InputStream) 被截断为

{"profile":[{"image":"iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHLAAAABHNCSVQICAgIfAhkiAAAIABJREFU\neJzcvWuMZdl13\/e71ftWr9NzbnOfYRV5j9RN1mVmCNaYDKfbppFuhBE1BBNoAskhmcSxCU8cKYAB\nOxYcGwnyJbDz+pAPVvxIPkq2DMgOYFiyZXgIROCQAp0e2GN3j0Bm7sg90W17WjpXmjLPnumjPqu7\nd1flw9r7nFM9MxQp8SHnzDSq6j7OY++11+O\/\/mvtGf8\/PP78j\/8HHz95sOOLM7NPAcxi\/6mgPb+x\nDYgXts3247OHVFIWaNfThsDJCe3h4dO\/QoTN7VcpRJjBV+5GmM35iszPhy\/\/03\/xK9\/nR\/uOH7Pv\n9w38Xo8\/\/Afe9\/EHcfZDZ+c8vd3+5qcKkZX3nkoKVqsl9V5Nc9SwubkBKWhDS1FWiECzbQCh3qtR\nVTa3NogIvvRs...EHoF0

(.....再次意味着我删除了其中的一部分以显示在这里) 但它以 EHoF0 结束(介于真正的完整图像字符串之间

使用

在 android 端接收
responseFromServer = JSONParser.makeHttpRequestReturnJson(url, "GET",param);

在这里检查了各种其他问题的答案,但没有一个有效。我什至尝试过 POST

请帮忙!

【问题讨论】:

标签: android json parsing exception get


【解决方案1】:

我认为你应该准确地学习 Android 中的 JSON 解析。我建议你去观看任何视频教程!

要解决您面临的问题,请更新您的代码:

String jsonString=convertStreamToString(is);
try{
    response = new JSONObject(jsonString)
}catch (Exception e) {
    e.printStackTrace();
}

来自

String jsonString=convertStreamToString(is);
try{
     response = new JSONObject((jsonString.substring(jsonString.indexOf("{"), jsonString.lastIndexOf("}")+1)));
}catch (Exception e) {
      e.printStackTrace();
}

【讨论】:

  • 我做了更改。现在我在保存位图时碰巧看到了这个错误D/skia: --- SkImageDecoder::Factory returned null,让我提一下位图(作为字符串)被截断。它是位图的完整字符串
【解决方案2】:

喜欢就好,

JSONObject jsonObject = new JSONObject(jsonString);

返回字符0处的输入结束,因为字符串结果是空的,也许你可以尝试用一些jsonString替换它。

【讨论】:

    【解决方案3】:

    按照我的@Paresh 的建议进行更改

     String jsonString=convertStreamToString(is); try{
         response = new JSONObject(jsonString) }catch (Exception e) {
         e.printStackTrace(); }
    

    来自

    String jsonString=convertStreamToString(is);
    try{
         response = new JSONObject((jsonString.substring(jsonString.indexOf("{"), jsonString.lastIndexOf("}")+1)));
    }catch (Exception e) {
          e.printStackTrace();
    }
    

    然后我进一步收到错误

    D/skia: --- SkImageDecoder::Factory 返回 null

    解决了 更换 这个

    public Bitmap StringToBitMap(String getContent) {
            try {
                byte[] encodeByte = getContent.getBytes(Charset.forName("UTF-8"));
                Bitmap bitmap = BitmapFactory.decodeByteArray(encodeByte, 0,encodeByte.length);
                return bitmap;
            }
            catch (Exception e)
            {
                e.getMessage();
                return null;
            }
    
        }
    

    有了这个

    public Bitmap StringToBitMap(String encodedString){
            try {
                byte [] encodeByte=Base64.decode(encodedString, Base64.DEFAULT);
                Bitmap bitmap= BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
                return bitmap;
            } catch(Exception e) {
                e.getMessage();
                return null;
            }
        }
    

    希望对别人有帮助

    如果有帮助请投票:)

    【讨论】:

      猜你喜欢
      • 2012-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 2015-11-13
      • 1970-01-01
      • 2014-08-09
      相关资源
      最近更新 更多