2014-12-29 2 views
0

나는 천천히 여기서 정신 나간다.Android JSON ListView 가져 오기

저는 웹 사이트에서 정보를 가져와 listview에 넣는 NewsActivity를 얻었지만 작동하지 않습니다.

12-29 13:56:39.698: W/System.err(1685): org.json.JSONException: Value [{"hometext":"Welcome to Nuke-Evolution.<br \/><br \/>\r\n\r\nYou must now setup an admin account. You can do this by <a href=\"admin.php\">clicking here<\/a>.<br \/><br \/><br \/><br \/>\r\n\r\n<b>NOTE:<\/b> You can remove this by going into the News Administration or by clicking the delete button below.\r\n","sid":"1","time":"2005-07-02 10:38:28","title":"Welcome To Nuke-Evolution"},{"hometext":"<p>\n\ttest android<\/p>\n<p>\n\tandroid test<\/p>","sid":"2","time":"2014-12-28 23:21:25","title":"android test"}] of type org.json.JSONArray cannot be converted to JSONObject 

이 anybodey 잘못하고 무슨 메신저 알고 있습니까 :

public class NewsActivity extends ListActivity { 
 

 
    private ProgressDialog pDialog; 
 

 
    // URL to get contacts JSON 
 
    private static String NEWS_URL = "http://www.bandofbrothersgaming.nl/android/news.php"; 
 

 
    // JSON Node names 
 
    private static final String TAG_POSTID = "sid"; 
 
    private static final String TAG_POSTSUBJECT = "title"; 
 
    private static final String TAG_POSTTEXT = "hometext"; 
 

 
    // contacts JSONArray 
 
    JSONArray post_id = null; 
 

 
    // Hashmap for ListView 
 
    ArrayList < HashMap < String, String >> NewsList; 
 

 
    @ 
 
    Override 
 
    public void onCreate(Bundle savedInstanceState) { 
 
    super.onCreate(savedInstanceState); 
 
    setContentView(R.layout.activity_news); 
 

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

 
    ListView lv = getListView(); 
 

 
    // Listview on item click listener 
 
    lv.setOnItemClickListener(new OnItemClickListener() { 
 

 
     @ 
 
     Override 
 
     public void onItemClick(AdapterView <? > parent, View view, 
 
     int position, long id) { 
 
     // getting values from selected ListItem 
 
     String name = ((TextView) view.findViewById(R.id.post_id)) 
 
      .getText().toString(); 
 
     String cost = ((TextView) view.findViewById(R.id.post_subject)) 
 
      .getText().toString(); 
 
     String description = ((TextView) view.findViewById(R.id.post_text)) 
 
      .getText().toString(); 
 

 
     // Starting single contact activity 
 
     /* Intent in = new Intent(getApplicationContext(), 
 
        SingleContactActivity.class); 
 
      in.putExtra(TAG_NAME, name); 
 
      in.putExtra(TAG_EMAIL, cost); 
 
      in.putExtra(TAG_PHONE_MOBILE, description); 
 
      startActivity(in);*/ 
 

 
     } 
 
    }); 
 

 
    // Calling async task to get json 
 
    new GetContacts().execute(); 
 
    } 
 

 
    /** 
 
    * Async task class to get json by making HTTP call 
 
    * */ 
 
    private class GetContacts extends AsyncTask < Void, Void, Void > { 
 

 
    @ 
 
    Override 
 
    protected void onPreExecute() { 
 
     super.onPreExecute(); 
 
     // Showing progress dialog 
 
     pDialog = new ProgressDialog(NewsActivity.this); 
 
     pDialog.setMessage("Please wait..."); 
 
     pDialog.setCancelable(false); 
 
     pDialog.show(); 
 

 
    } 
 

 
    @ 
 
    Override 
 
    protected Void doInBackground(Void...arg0) { 
 
     // Creating service handler class instance 
 
     ServiceHandler sh = new ServiceHandler(); 
 

 
     // Making a request to url and getting response 
 
     String jsonStr = sh.makeServiceCall(NEWS_URL, ServiceHandler.GET); 
 

 
     Log.d("Response: ", "> " + jsonStr); 
 

 
     if (jsonStr != null) { 
 
     try { 
 
      JSONObject jsonObj = new JSONObject(jsonStr); 
 

 
      // Getting JSON Array node 
 
      post_id = jsonObj.getJSONArray(TAG_POSTID); 
 

 
      // looping through All Posts 
 
      for (int i = 0; i < post_id.length(); i++) { 
 
      JSONObject c = post_id.getJSONObject(i); 
 

 
      String postid = c.getString(TAG_POSTID); 
 
      String postsubject = c.getString(TAG_POSTSUBJECT); 
 
      String posttext = c.getString(TAG_POSTTEXT); 
 

 

 
      // tmp hashmap for single contact 
 
      HashMap < String, String > contact = new HashMap < String, String >(); 
 

 
      // adding each child node to HashMap key => value 
 
      contact.put(TAG_POSTID, postid); 
 
      contact.put(TAG_POSTSUBJECT, postsubject); 
 
      contact.put(TAG_POSTTEXT, posttext); 
 

 
      // adding contact to contact list 
 
      NewsList.add(contact); 
 
      } 
 
     } catch (JSONException e) { 
 
      e.printStackTrace(); 
 
     } 
 
     } else { 
 
     Log.e("ServiceHandler", "Couldn't get any data from the url"); 
 
     } 
 

 
     return null; 
 
    } 
 

 
    @ 
 
    Override 
 
    protected void onPostExecute(Void result) { 
 
     super.onPostExecute(result); 
 
     // Dismiss the progress dialog 
 
     if (pDialog.isShowing()) 
 
     pDialog.dismiss(); 
 
     /** 
 
     * Updating parsed JSON data into ListView 
 
     * */ 
 
     ListAdapter adapter = new SimpleAdapter(
 
     NewsActivity.this, NewsList, 
 
     R.layout.list_item, new String[] { 
 
      TAG_POSTID, TAG_POSTSUBJECT, 
 
      TAG_POSTTEXT 
 
     }, new int[] { 
 
      R.id.post_id, 
 
      R.id.post_subject, R.id.post_text 
 
     }); 
 

 
     setListAdapter(adapter); 
 
    } 
 

 
    } 
 

 
}

내가 로그 캣 실행 다음 말한다?

답변

0

당신은

JSONArray post_id = new JSONArray(jsonStr); 대신

JSONObject jsonObj = new JSONObject(jsonStr); 
// Getting JSON Array node 
post_id = jsonObj.getJSONArray(TAG_POSTID); 

는 당신이 얻을 jsonStr은 JSONArray

+0

당신을 감사하십시오! 그 트릭을 :) – PatrickStel

0

당신의 PHP 파일의 응답은 PHP 코드를 검토하십시오.

1

jsonStrJSONObject으로 변환하려고 시도했지만 실제로는 LogCat을보고 JSONArray입니다. 따라서 먼저 JSONArray으로 변환 한 다음 반복하여 찾고있는 데이터를 얻으십시오.

0

때문에 사용이

JSONArray jsonArr = new JSONArray(jsonStr); 

     for(int i = 0; i < jsonArr.length(); i++){ 
      JSONObject item = jsonArr.getJSONObject(i); 
     String postid = item.getString(TAG_POSTID); 
        String postsubject = item.getString(TAG_POSTSUBJECT); 
        String posttext = item.getString(TAG_POSTTEXT); 


        // tmp hashmap for single contact 
        HashMap < String, String > contact = new HashMap < String, String >(); 

        // adding each child node to HashMap key => value 
        contact.put(TAG_POSTID, postid); 
        contact.put(TAG_POSTSUBJECT, postsubject); 
        contact.put(TAG_POSTTEXT, posttext); 

        // adding contact to contact list 
        NewsList.add(contact); 
     }