服务器正确发送json数据。客户端解析错误

时间:2012-10-11 21:25:25

标签: android json

我有一个PHP代码,可以生成一个JSON字符串以发送到我的Android应用程序。这部分有效。 问题是当我的应用程序捕获该字符串并尝试将其转换为JSONArray对象时。 主要思想是将数据存储在SQLite数据库中。

以下是捕获JSON字符串的代码:

public void getPostResponse(){
    try{
        br = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
        sb = new StringBuilder();
        line = null;
        while((line = br.readLine()) != null){
            sb.append(line+"\n");
        }
        is.close();
        result = sb.toString();
        Log.e("getPostResponse","result= "+sb.toString());
    }catch(Exception e){
        Log.e("log_tag","Error converting result "+e.toString());
    }
}

这是LogCat中的结果:

10-11 16:27:01.171: E/getPostResponse(9155): result= [{ "establecimientos":[]},{ "rutas":[]},{ "formularios":[]},{ "clientes":[]},{ "mediciones":[]}]

这就是为什么我认为存在错误,结果变量应该包含整个JSON字符串而不是。有什么想法吗?

变量br,sb,line和result全局声明。 从服务器发送的JSON已经是JSONArray(以'['开头并以']'结尾),这是导致问题的原因吗?

这是json字符串:

[
    {
        "establecimientos": [
            {
                "idestablecimiento": "108",
                "nombre": "Establecimiento 123",
                "direccion": "632-8165 Non Road",
                "idruta": "104",
                "idempresa": "1004"
            },
            {
                "idestablecimiento": "102",
                "nombre": "Establecimiento XYZ",
                "direccion": "Ap #124-9882 Risus, Street",
                "idruta": "106",
                "idempresa": "1006"
            },
            {
                "idestablecimiento": "106",
                "nombre": "Unicasa La Candelaria",
                "direccion": "P.O. Box 898, 6831 Morbi Rd.",
                "idruta": "106",
                "idempresa": "1001"
            }
        ]
    },
    {
        "rutas": [
            {
                "idruta": "106",
                "nombre": "Petare"
            },
            {
                "idruta": "104",
                "nombre": "La Castellana"
            }
        ]
    },
    {
        "formularios": [
            {
                "idformulario": "100",
                "nombre": "Encuesta Libre",
                "idestablecimiento": "108"
            },
            {
                "idformulario": "100",
                "nombre": "Encuesta Libre",
                "idestablecimiento": "102"
            },
            {
                "idformulario": "100",
                "nombre": "Encuesta Libre",
                "idestablecimiento": "106"
            }
        ]
    },
    {
        "clientes": [
            {
                "idcliente": "1002",
                "nombre": "Sibelius",
                "idempresa": "1006"
            },
            {
                "idcliente": "1009",
                "nombre": "Lavasoft",
                "idempresa": "1004"
            },
            {
                "idcliente": "1000",
                "nombre": "Cakewalk",
                "idempresa": "1001"
            }
        ]
    },
    {
        "mediciones": [
            {
                "idformulario": "100",
                "idmedicion": "100",
                "texto": "1) FALTA DE PRODUCTO",
                "tipopregunta": "Seleccion Simple",
                "tipodato": "String"
            },
            {
                "idformulario": "100",
                "idmedicion": "101",
                "texto": "2) PRE VENDEDOR NO VISITA",
                "tipopregunta": "Si o No",
                "tipodato": "String"
            },
            {
                "idformulario": "100",
                "idmedicion": "102",
                "texto": "3) ENTREGADOR NO DESPACHA",
                "tipopregunta": "Si o No",
                "tipodato": "String"
            },
            {
                "idformulario": "100",
                "idmedicion": "103",
                "texto": "4) NEVERA FUERA DE SERVICIO",
                "tipopregunta": "Seleccion Simple",
                "tipodato": "String"
            },
            {
                "idformulario": "100",
                "idmedicion": "104",
                "texto": "5) NEVERA CONTAMINADA",
                "tipopregunta": "Seleccion Simple",
                "tipodato": "String"
            },
            {
                "idformulario": "100",
                "idmedicion": "105",
                "texto": "6) CLIENTE EXCLUSIVO DE LA COMPETENCIA",
                "tipopregunta": "Si o No",
                "tipodato": "String"
            },
            {
                "idformulario": "100",
                "idmedicion": "106",
                "texto": "7) FALTA DE MATERIAL POP EN PDV",
                "tipopregunta": "Si o No",
                "tipodato": "String"
            },
            {
                "idformulario": "100",
                "idmedicion": "107",
                "texto": "8) CLIENTE CERRADO",
                "tipopregunta": "Si o No",
                "tipodato": "String"
            },
            {
                "idformulario": "100",
                "idmedicion": "108",
                "texto": "9) NEGACIÓN POR PARTE DEL CLIENTE",
                "tipopregunta": "Si o No",
                "tipodato": "String"
            },
            {
                "idformulario": "100",
                "idmedicion": "109",
                "texto": "10) PRODUCTO VENCIDO",
                "tipopregunta": "Seleccion Simple",
                "tipodato": "String"
            },
            {
                "idformulario": "100",
                "idmedicion": "110",
                "texto": "11) PROMOCIONES ESPECIALES",
                "tipopregunta": "Si o No",
                "tipodato": "String"
            }
        ]
    }
]

1 个答案:

答案 0 :(得分:0)

更容易获得你的post调用的HttpResponse,获取实体字符串,因此,创建一个新的JSONObject:

//create an Uri
URI uri = new URI(string);
//or using URIUtils

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(uri);
HttpResponse response = client.execute(post);
String jsonString = EntityUtils.toString(response.getEntity());
JSONObject json = new JSONObject(jsonString);

因此,您可以操纵和检查json对象。示例,如何获取JSONArray:

JSONArray array = json.getJSONArray("estabelecimentos");

[]中 内托

相关问题