在Android中使用json.GetJSONArray(?;;&QUOT&QUOT)问题 [英] Problems with json.GetJSONArray("?") in Android

查看:121
本文介绍了在Android中使用json.GetJSONArray(?;;&QUOT&QUOT)问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

结果
我试图让我的code的工作。结果我想打一个MySQL连接
和JSON数据发送到我的Andr​​oid应用程序。结果
我想这几乎工作,但我的logcat给我这个警告几乎是在最后:
结果的错误解析数据值[{staff_phone:123,staff_name:法比安等。
结果我想我做错了什么事在我的SQL脚本。这是脚本:


I am trying to get my code work.
I want to make a mysql connection and send the data with json to my android app.
I guess it almost works but my logcat gives me this warning almost at the end:
"error parsing data" value [{"staff_phone":"123","staff_name":"fabian" etc.
I guess i did something wrong in my sql script. This is the script:

mysql_connect($db_host,$db_user,$db_pwd);
mysql_select_db($database);
$result = mysql_query("SELECT * FROM contactlijst");
$array = array();
while ($row = mysql_fetch_assoc($result)) 
{ 
array_push($array,  $row); 
}
print json_encode($array);
mysql_close();

结果这是我的java codeS:结果


These are my java codes:

public static JSONObject getJSONfromURL(String url){

    //initialize
    InputStream is = null;
    String result = "";
    JSONObject jArray = null;

    //http post
    try{
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();

    }catch(Exception e){
        Log.e("log_tag", "Error in http connection "+e.toString());
    }

    //convert response to string
    try{
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        result=sb.toString();
    }catch(Exception e){
        Log.e("log_tag", "Error converting result "+e.toString());
    }

    //try parse the string to a JSON object
    try{
            jArray = new JSONObject(result);
    }catch(JSONException e){
        Log.e("log_tag", "Error parsing data "+e.toString());
    }

    return jArray;
} }

和这一个:

ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

  //Get the data (see above)
  JSONObject json =
    Database.getJSONfromURL("http://fabian.nostradamus.nu/Android/getcontactinfo.php");

         try{
    JSONArray contactinfo = json.getJSONArray("contactlijst");

                    //Loop the Array
            for(int i=0;i < contactinfo.length();i++){                      

                HashMap<String, String> map = new HashMap<String, String>();
                JSONObject e = contactinfo.getJSONObject(i);

                map.put("voornaam", e.getString("staff_name"));
                map.put("achternaam", e.getString("staff_lastname"));
                map.put("geboortedatum", e.getString("staff_dateofbirth"));
                map.put("adres", e.getString("staff_address"));
                map.put("postcode", e.getString("staff_address_postal"));
                map.put("woonplaats", e.getString("staff_address_city"));
                map.put("email", e.getString("staff_email"));
                map.put("telefoon", e.getString("staff_phone"));
                mylist.add(map);
        }
           }catch(JSONException e)        {
             Log.e("log_tag", "Error parsing data "+e.toString());
           }

}

那么,我做错了什么?
非常感谢!

So what am I doing wrong? Thanks alot!

推荐答案

getJSONArray()预计给予其值是一个数组的关键。因此,预计的JSON看起来更像是这样的:

getJSONArray() expects to be given a key whose value is an array. So it expects JSON that looks more like this:

{contactlijst:[{staff_phone:123,staff_name:费边...

如果这不起作用,尝试验证您的JSON,以确保其格式正确无误。我经常使用 JSONLint 用于这一目的。

If this doesn't work, try validating your JSON to ensure it is formatted correctly. I often use JSONLint for that purpose.

这篇关于在Android中使用json.GetJSONArray(?;;&QUOT&QUOT)问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆