2014-12-04 41 views
0
package com.example.velichamjson; 

import java.util.ArrayList; 
import java.util.HashMap; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.app.ListActivity; 
import android.app.ProgressDialog; 
import android.content.Intent; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 
import android.widget.TextView; 

public class MainActivity extends ListActivity { 

    private ProgressDialog pDialog; 

    // URL to get contacts JSON 
    private static String url = "http://velicham.co.in/index.php?option=com_obrss&task=feed&id=4:politics&format=json&Itemid=351"; 

    // JSON Node names 
    private static final String TAG_ITEMS= "items"; 
    private static final String TAG_TITLE= "title"; 
    private static final String TAG_LINK = "link"; 
    private static final String TAG_PUBDATE = "pubDate"; 
    private static final String TAG_DESCRIPTION= "description"; 
// private static final String TAG_GENDER = "gender"; 
// private static final String TAG_PHONE = "phone"; 
// private static final String TAG_PHONE_MOBILE = "mobile"; 
// private static final String TAG_PHONE_HOME = "home"; 
// private static final String TAG_PHONE_OFFICE = "office"; 

    // contacts JSONArray 
    JSONArray items = null; 

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

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

     itemList = 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 link = ((TextView) view.findViewById(R.id.link)) 
         .getText().toString(); 
       String pubDate = ((TextView) view.findViewById(R.id.pubdate)) 
         .getText().toString(); 
       String description = ((TextView) view.findViewById(R.id.description)) 
         .getText().toString(); 

       // Starting single contact activity 
       Intent in = new Intent(getApplicationContext(), 
         SingleContactActivity.class); 
       in.putExtra(TAG_LINK, link); 
       in.putExtra(TAG_PUBDATE, pubDate); 
       in.putExtra(TAG_DESCRIPTION, description); 
       startActivity(in); 

      } 
     }); 

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

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

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      // Showing progress dialog 
      pDialog = new ProgressDialog(MainActivity.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(url, ServiceHandler.GET); 

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

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

        // Getting JSON Array node 

        items = jsonObj.getJSONArray(TAG_ITEMS); 
        System.out.println(items); 

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

         String title = c.getString(TAG_TITLE); 
         String link = c.getString(TAG_LINK); 
         String pubDate = c.getString(TAG_PUBDATE); 
         String description = c.getString(TAG_DESCRIPTION); 
         //String gender = c.getString(TAG_GENDER); 

         // Phone node is JSON Object 
//      JSONObject phone = c.getJSONObject(TAG_PHONE); 
//      String mobile = phone.getString(TAG_PHONE_MOBILE); 
//      String home = phone.getString(TAG_PHONE_HOME); 
//      String office = phone.getString(TAG_PHONE_OFFICE); 

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

         // adding each child node to HashMap key => value 
         item.put(TAG_TITLE, title); 
         item.put(TAG_LINK, link); 
         item.put(TAG_PUBDATE, pubDate); 
         item.put(TAG_DESCRIPTION, description); 

         // adding contact to contact list 
         itemList.add(item); 
        } 
       } 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(
        MainActivity.this, itemList, 
        R.layout.list_item, new String[] { TAG_LINK, TAG_PUBDATE, 
          TAG_DESCRIPTION ,TAG_TITLE}, new int[] { R.id.link, 
          R.id.pubdate, R.id.description,R.id.title }); 

      setListAdapter(adapter); 
     } 

    } 

} 

IAM試圖解析JSON數據是一個泰米爾人的消息從我的JSON數據獲取,但它顯示了一個錯誤的項目沒有價值的我不是一個半天的時間試圖更加任何能回答在那裏出了問題,我不知道哪裏出了問題有人REPONSE我在此先感謝獲取數據的JSON我的列表視圖

+0

你能後傳入的JSON數據? – 2014-12-04 08:56:32

+0

http://velicham.co.in/index.php?option=com_obrss&task=feed&id=4:politics&format=json&Itemid=351 – 2014-12-04 09:01:04

+0

上面是我的JSON網址 – 2014-12-04 09:01:33

回答

0

更新 -使用的JSONObject你可以做如下

if (jsonStr != null) { 
      try { 
       JSONObject jsonObj = new JSONObject(jsonStr); 
       JSONArray arr = new JSONArray(jsonObj.get(TAG_ITEMS)); 
       // looping through All data 
       for (int i = 0; i < arr.length(); i++) { 
        JSONObject temp = arr.getJSONObject(i); 
        String title = temp.get(TAG_TITLE); 
        String link = temp.get(TAG_LINK); 
        String pubDate = temp.get(TAG_PUBDATE); 
        String description = temp.get(TAG_DESCRIPTION); 
       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

可以使用JsonReader 等低你的JSON數據,

注 -我沒有測試的代碼,你必須調整它按照您的要求,但多數民衆贊成在如何使用該機器人解析JsonData JsonReader.You可以看一下Android開發者頁面更多信息。

JsonReader reader = new JsonReader(new InputStreamReader(yourResponseStream, Encoding)); 
reader.beginObject(); 
while (reader.hasNext()) { 
     String name = reader.nextName(); 
     if (name.equals("count")) { 
      string count = reader.nextString(); 
     } else if (name.equals("value")) { 
      //Method to process the value object 
      processValueObject(reader); 
     } else { 
      reader.skipValue(); 
     } 
    } 


    private void processValueObject(JsonReader reader) 
    { 
    reader.beginObject(); 
    while (reader.hasNext()) { 
     String name = reader.nextName(); 
     if (name.equals("feedUrl")) { 
      string feedUrl = reader.nextString(); 
     } else if (name.equals("title")) { 
      string title = reader.nextString(); 
     } ....and so on 
     //For array of data 
     }else if (name.equals("items")) 
     { 
     reader.beginArray(); 
     reader.beginObject(); 
     //then use the same methods to get name and value pair. 
      processReader(reader); 
     //at the end dont forget to end your array and object 
     reader.endObject(); 
     reader.endArray(); 
     } 
     else { 
      reader.skipValue(); 
     } 
    } 
    } 

希望它可以幫助...

+0

我檢查... – 2014-12-04 10:04:37

+0

我試過,但它沒有來請你看看我的問題 – 2014-12-04 11:08:55

+0

我不知道在哪裏以及如何編寫上面給出的JSON閱讀器代碼,你可以簡單地解釋一下......在此先感謝 – 2014-12-04 11:16:05