我有一个 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"
}
]
}
]