开发者

Problems with json.GetJSONArray("?") in Android

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_encod开发者_如何学Ce($array);
mysql_close();

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

AND this one:

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() 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":"fabian"...

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜