我一直收到这个错误,我知道为什么但似乎无法修复它,我已经尝试为它实现修复但它似乎没有做任何事情,即使帖子说这应该修复它,我检索来自PHP脚本的数据,该脚本是json编码的,然后尝试迭代它以存储数组列表。
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);
?>
脚本产生的内容
[{&#34;纬度&#34;:&#34; 54.009222844372566&#34;&#34;经度&#34;:&#34; -2.787822335958481&#34;&#34;消息&# 34;:&#34;杰米&#34;}] [{&#34;纬度&#34;:&#34; 54.009222844372566&#34;&#34;经度&#34;:&#34; -2.787822335958481& #34;&#34;消息&#34;:&#34;杰米&#34;},{&#34;纬度&#34;:&#34; 54.01182883490973&#34;&#34;经度&#34 ;:&#34; -2.7903684228658676&#34;&#34;消息&#34;:&#34;弗莱迪&#34;}]
我的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上收到错误 错误
Error parsing data org.json.JSONException: Value invalid of type java.lang.String cannot be converted to JSONArray
答案 0 :(得分:1)
结果的格式与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解析。
答案 1 :(得分:0)
我认为错误发生在这一行:
JSONArray jArray = new JSONArray(result);
假设result
是一个String,我认为你需要首先将String加载到JSONObject中并从中检索数组,例如。
JSONObject jObject = new JSONObject(result);
JSONArray jArray = jObject.getJSONArray("xxx");
...其中xxx
是JSON字符串中Array变量的名称。
答案 2 :(得分:0)
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();
}
试试这个
我希望这可以帮到你