2015-03-19 75 views
0

我不断收到这个错误,我知道为什么,但似乎无法修复它,我试图为它实现一个修复程序,但它似乎什么也没做,即使帖子都说这应该解决它,我从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 
+4

发布您的'JSON'数据。 – Yugesh 2015-03-19 09:30:13

+0

[将字符串转换为JSON数组]可能的重复(http://stackoverflow.com/questions/15609306/convert-string-to-json-array) – sudocode 2015-03-19 09:38:48

+0

由于@Yugesh说没有显示json格式我们可以如何帮助您? – 2015-03-19 09:50:56

回答

0

我假定在这条线中发生错误:

JSONArray jArray = new JSONArray(result); 

假设result是字符串,我想你需要从,例如首先加载字符串到一个JSONObject和检索数组

JSONObject jObject = new JSONObject(result); 
JSONArray jArray = jObject.getJSONArray("xxx"); 

...其中xxx是您的JSON字符串中的数组变量的名称。

+0

出于某种奇怪的原因发生同样的错误 – Harjit 2015-03-19 09:48:18

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(); 
} 

试试这个

我希望这可以帮助您

1

结果的格式不匹配的JsonArray的格式。结果表明只有一个包含两个JsonArray但没有一个的JsonObject。所以,原因是你得到了错误格式的错误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解析。

+0

我明白你的意思,我将如何格式化它?我尝试使用JSON_PRETTY_PRINT,但没有正确格式化它@SilentKnight – Harjit 2015-03-19 10:15:18

+0

你需要修改你的PHP代码。您的PHP没有以正确格式处理结果。 – SilentKnight 2015-03-19 10:20:04

+0

对不起,谢谢你的帮助 – Harjit 2015-03-19 10:20:27