我一直收到这个错误,我知道为什么,但似乎无法修复它,我试图实现修复它,但它似乎没有做任何事情,即使帖子说这应该修复它,我从一个json编码的PHP脚本中检索数据,然后尝试迭代它以将其存储为数组列表。
setlat()、set lng()和setmsg()来自构造函数类
PHP脚本
<?php
$hostname_localhost ="**";
$database_localhost ="**";
$username_localhost ="**";
$password_localhost ="**";
$localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost)
or trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db($database_localhost, $localhost);
$sql="SELECT latitude,longitude,message FROM locations";
$result=mysql_query($sql);
while ($row=mysql_fetch_assoc($result) or die(mysql_error()))
{
$output []= $row;
print(json_encode($output));
}
mysql_close($localhost);
?>脚本生成的内容
{“纬度”:“54.009222844372566”,“经度”:“-2.787822335958481”,“消息”:“杰米”}
我的http帖子
public String getdata(){
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("******");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String response = httpclient.execute(httppost, responseHandler);
Toast.makeText(Reminders.this, "response" + response, Toast.LENGTH_LONG).show();
return response.trim();
} catch (Exception e){
System.out.print("Your exception : " + e);
return "invalid";这就是调用方法的地方。
new Thread(new Runnable() {
public void run() {
String result = getdata();
ArrayList<locations> lcd = parseJSON(result);
}
}).start();发生错误
public ArrayList<locations> newdata = new ArrayList<locations>();
try {
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
locations llm = new locations();
llm.setlat(json_data.getString("latitude"));
llm.setlng(json_data.getString("longitude"));
llm.setmsg(json_data.getString("message"));
newdata.add(llm);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return newdata;
}我在JSONObject json_data The error上收到一个错误
Error parsing data org.json.JSONException: Value invalid of type java.lang.String cannot be converted to JSONArray发布于 2015-03-19 18:02:44
结果的格式与JsonArray的格式不匹配。结果表明,只有一个JsonObject包含两个JsonArray,而不是一个。所以,原因是你得到了一个错误格式的错误JsonArray。我认为正确的JsonArray格式应该是这样的:
[{"latitude":"54.009222844372566","longitude":"-2.787822335958481","message":"jamie"},{"latitude":"54.009222844372566","longitude":"-2.787822335958481","message":"jamie"},{"latitude":"54.01182883490973","longitude":"-2.7903684228658676","message":"freddy"}]深入了解字符串和您的字符串之间的区别。然后,您将获得正确的JsonArray解析。
发布于 2015-03-19 17:36:07
我假设错误发生在下面这一行:
JSONArray jArray = new JSONArray(result);假设数组是一个字符串,我认为您需要首先将该字符串加载到JSONObject中并从中检索该数组,例如:
JSONObject jObject = new JSONObject(result);
JSONArray jArray = jObject.getJSONArray("xxx");..。其中xxx是JSON字符串中的数组变量的名称。
发布于 2015-03-19 17:48:41
JSONObject obj1 = new JSONObject(resultSTR);
try {
JSONArray result = obj1.getJSONArray("xxx");
for(int i=0;i<=result.length();i++)
{
locations llm = new locations();
llm.setlat(result.getString("latitude"));
llm.setlng(result.getString("longitude"));
llm.setmsg(result.getString("message"));
newdata.add(llm);
}
} catch (JSONException e) {
e.printStackTrace();
}尝尝这个
我希望这对你有帮助
https://stackoverflow.com/questions/29140921
复制相似问题