我不断收到这个错误,我知道为什么,但似乎无法修复它,我试图为它实现一个修复程序,但它似乎什么也没做,即使帖子都说这应该解决它,我从PHP脚本获取数据是json编码,然后尝试通过迭代来存储它的数组列表。字符串不能转换为JSONArray,解决方案不能修复它
setlat(),设置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“, ”消息“: ”杰米“}] [{ ”纬度“: ”54.009222844372566“, ”经度“: ” - 2.787822335958481“, ”消息“: ”杰米“},{”纬度“:”54.01182883490973“,”经度“:” - 2.7903684228658676“,”消息“:”freddy“}]
我的HTTP POST
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
发布您的'JSON'数据。 – Yugesh 2015-03-19 09:30:13
[将字符串转换为JSON数组]可能的重复(http://stackoverflow.com/questions/15609306/convert-string-to-json-array) – sudocode 2015-03-19 09:38:48
由于@Yugesh说没有显示json格式我们可以如何帮助您? – 2015-03-19 09:50:56