2011-09-14 4 views
0


코드 작업을 얻으려고합니다.
mysql 연결을 으로 만들고 json으로 데이터를 안드로이드 앱에 보내고 싶습니다.
거의 작품을 생각하지만 내 로그 캣 거의 마지막에 나에게이 경고를 제공합니다
"오류 분석 데이터"값 [{ "staff_phone": "123", "staff_name": "파비안"등
내 SQL 스크립트에서 뭔가 잘못한 것 같아요.Android에서 json.GetJSONArray ("?") 문제

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


이 내 자바 코드입니다 : 이것은 스크립트입니다

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

} 

이 그래서 내가 뭘 잘못은? 감사합니다.

답변

0

getJSONArray()에는 값이 배열 인 키가 있어야합니다. 문제가 해결되지 않으면

{"contactlijst": [{"staff_phone":"123","staff_name":"fabian"...

이 올바르게 포맷 확인하기 위해 JSON을 확인하는 시도 : 그래서 더 다음과 같습니다 JSON을 기대하고있다. 나는 종종 그 목적으로 JSONLint을 사용한다.

관련 문제