org.json.JSON异常:

时间:2016-06-04 06:47:51

标签: android json parsing exception get

收到的错误是“org.json.JSON例外:字符0处的输入结束”

MY GET JSON Parser

// 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

请帮忙!

3 个答案:

答案 0 :(得分: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();
}

答案 1 :(得分:1)

简单地说,

JSONObject jsonObject = new JSONObject(jsonString);

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

答案 2 :(得分:0)

根据我的@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;
        }
    }

我希望它可以帮助别人

如果有帮助,请投票:)