JSON为Android 2.2不工作 [英] Json for Android 2.2 not working

查看:103
本文介绍了JSON为Android 2.2不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用我与仿真器和手机的Andorid 4.1和Android 2.2的测试。
在Android 4.2的一切工作正常还对仿真器的API 17。

但在2.2我得到错误:java.lang.ClassCastException:java.lang.String中

我没去调试,并在标有星行发生错误。

错误之前我也得到通知:在BufferedReader类的构造函数使用默认的缓冲区大小。是否需要8K-字符缓冲区这将是更好的是明确的。

任何帮助将是AP preciated。

 公共无效buildData(字符串jsonString,字符串code){
    mProduct =新的HashMap<整数,产品>();
    尝试{
        ** JSONArray阵列=(JSONArray),其中新JSONTokener(jsonString).nextValue(); **
        的for(int i = 0; I< array.length();我++){
            JSONObject的JSON = array.getJSONObject(I)
            如果(json.has(JSON_PRODUCT)){
                buildProduct(json.getString(JSON_PRODUCT),code);
            }
            否则如果(json.has(JSON_NAME)){
                buildUser(json.getString(JSON_NAME));
            }
        }
    }赶上(JSONException E){
        // TODO自动生成catch块
        e.printStackTrace();
    }
}

我用这个code下载数据:

 字节公众[] getUrlBytes(字符串urlSpec)抛出IOException
    网址URL =新的URL(urlSpec);
    HttpURLConnection的连接=(HttpURLConnection类)url.openConnection();    尝试{
        ByteArrayOutputStream出=新ByteArrayOutputStream();
        在的InputStream = connection.getInputStream();        如果(connection.getResponse code()!= HttpURLConnection.HTTP_OK)
            返回null;        INT读取动作= 0;
        字节[]缓冲区=新的字节[1024];
        而((读取动作= in.read(缓冲液))大于0){
            out.write(缓冲液,0,读取动作);
        }
        out.close();
        返回out.toByteArray();    } {最后
        connection.disconnect();
    }
}

我也应该追加例如jsonString那我送。

<$p$p><$c$c>{\"PRODUCT\":{\"0\":\"1\",\"product_id\":\"1\",\"1\":\"sdasd\",\"name\":\"sdasd\",\"2\":\"1\",\"person_id\":\"1\",\"3\":\"image\",\"image\":\"image\",\"4\":\"0\",\"alergen_id\":\"0\",\"5\":\"0\",\"contain\":\"0\",\"6\":\"-1\",\"aditiv_id\":\"-1\",\"7\":\"0\",\"tmpgrade\":\"0\"}}


解决方案

您尝试使用

  JSONArray阵列=新JSONArray(jsonString);

 的JSONObject对象=新的JSONObject(jsonString);

而不是使用

  Object对象=新JSONTokener(jsonString).nextValue()

I my application which I tested with emulator and phone Andorid 4.1 and Android 2.2. On Android 4.2 everything works ok also on emulator with API 17.

But on 2.2 I get error: java.lang.ClassCastException: java.lang.String

I did go debugging and the error occurs at line marked with stars.

Before error I also get notice: Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.

Any help would be appreciated.

public void buildData(String jsonString, String code) {
    mProduct = new HashMap<Integer, Product>();
    try {
        **JSONArray array = (JSONArray) new JSONTokener(jsonString).nextValue();**
        for(int i = 0; i < array.length(); i++){
            JSONObject json = array.getJSONObject(i);
            if(json.has(JSON_PRODUCT)){
                buildProduct(json.getString(JSON_PRODUCT), code);
            }
            else if(json.has(JSON_NAME)){
                buildUser(json.getString(JSON_NAME));
            }
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

I use this code for downloading data:

public byte[] getUrlBytes(String urlSpec) throws IOException{
    URL url = new URL(urlSpec);
    HttpURLConnection connection = (HttpURLConnection)url.openConnection();

    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        InputStream in = connection.getInputStream();

        if(connection.getResponseCode() != HttpURLConnection.HTTP_OK)
            return null;

        int bytesRead = 0;
        byte[] buffer = new byte[1024];
        while((bytesRead = in.read(buffer)) > 0){
            out.write(buffer, 0, bytesRead);
        }
        out.close();
        return out.toByteArray();

    } finally {
        connection.disconnect();
    }
}

I should also append example of jsonString that I send.

{"PRODUCT":{"0":"1","product_id":"1","1":"sdasd","name":"sdasd","2":"1","person_id":"1","3":"image","image":"image","4":"0","alergen_id":"0","5":"0","contain":"0","6":"-1","aditiv_id":"-1","7":"0","tmpgrade":"0"}}

解决方案

You tried to use

JSONArray array = new JSONArray(jsonString);

or

JSONObject object = new JSONObject(jsonString);

instead of using

Object object = new JSONTokener(jsonString).nextValue()

这篇关于JSON为Android 2.2不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆