2015-07-10 30 views
0

我試圖從遠程URL中拉出一組JSON數組,但出於某種原因,我一直關閉強制。任何形式的幫助我都能得到,非常感謝!如何將遠程JSON數據放入Android數組中?

我的遙控JSON URL輸出:

[ { "title": "name1", "url": "http:\/\/radio.shoutcast.com\/fm", "location": "usa", "webtitle": "\u0111i kh\u00f4ng em y\u1ec7u?", "weburl": "http:\/\/www.cnn.com" }, { "title": "MSNBC", "url": "http:\/\/radio.cnn.com\/cnn", "location": "usa", "webtitle": "kh\u00f4ng \u0111i \u0111\u00e2u", "weburl": "http:\/\/www.espn.com" } ] 

所以在我的Java代碼,我有這樣的:

public void createRadioListForRadioScreen(TableLayout UIRadioList, ArrayList<String> userRadios, TextView radioListName, TextView radioListLocation) { 
    ArrayList<RadioListElement> radioArray = new ArrayList<RadioListElement>(); 
    MainActivity.getDataManager().loadStoredRadioStations(radioArray, userRadios); 
    radioArray.add(new RadioListElement(context, "106.3 FM", "www.cnn.com", "http://radio.cnn.com/cnn")); 


    UIRadioList.removeAllViews(); 
    RadioList radioList = new RadioList(context, radioArray, UIRadioList); 
    radioList.addRadioStations(radioListName, radioListLocation); 

} 

我只想必要的字段加載到當前數組。我已經聲明這對mainactivity.java

String TAG_TITLE = "title"; 
String TAG_URL = "url"; 
String TAG_LOCATION = "location"; 
String TAG_WEBTITLE = "webtitle"; 
String TAG_WEBURL = "weburl"; 
String title, url, location, webtitle, weburl; 
public ArrayList<HashMap<String, String>> radList = new ArrayList<HashMap<String, String>>();; 

CODE拉JSON

protected Void doInBackground(Void... arg0) { 

     try { 

      JSONArray json = jsonParser.getJSONFromUrl(getResources() 
        .getString(R.string.url)); 
      Log.e("MainActivitty", json.toString()); 

      for (int i = 0; i < json.length(); i++) { 
       JSONObject c = json.getJSONObject(i); 
       title = c.getString(TAG_TITLE); 
       url = c.getString(TAG_URL); 
       location = c.getString(TAG_LOCATION); 
       webtitle = c.getString(TAG_WEBTITLE); 
       weburl = c.getString(TAG_WEBURL); 

       HashMap<String, String> map = new HashMap<String, String>(); 

       // adding each child node to HashMap key => value 
       map.put(TAG_TITLE, title); 
       map.put(TAG_URL, url); 
       map.put(TAG_LOCATION, location); 
       map.put(TAG_WEBTITLE, webtitle); 
       map.put(TAG_WEBURL, weburl); 

       // adding HashList to ArrayList 
       radList.add(map); 
      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 
} 

請幫我把這個網址進入我的代碼,太感謝你了!

+0

不能得到任何你有什麼問你在哪裏得到'json'數據?在哪個變量中? – Pankaj

+0

你能展示拉動json數據的代碼嗎? – zzas11

+0

@ zzas11我能夠拉它就好了......我只是不知道如何把它放在我當前的數組以及靜態字段中......我還添加了拉動JSON的代碼.. – thevoipman

回答

0

更新與此

for (int a =0; a<radList.size();a++) 
    { 
     HashMap<String, String> tmpData = (HashMap<String, String>) radList.get(a); 
     Set<String> key = tmpData.keySet(); 
     Iterator it = key.iterator(); 

     String title; 
     String url; 
     String location; 
     String webtitle; 
     String weburl; 

     while (it.hasNext()) { 
      title = tmpData.get(TAG_TITLE); 
      url = tmpData.get(TAG_URL); 
      location = tmpData.get(TAG_LOCATION); 
      webtitle= tmpData.get(TAG_WEBTITLE); 
      weburl = tmpData.get(TAG_WEBURL); 
      it.remove(); 
     } 

     radioArray.add(new RadioListElement(context, webtitle, url , weburl)); 

} 

代碼我沒有測試代碼,但它應該給一些想法解決您的問題。

+0

感謝您...我有不同的Java文件/類中的靜態數組不是在主要活動中......上面的示例進入了具有1個靜態數組的ios中的stations.java ...但是我的聲明和拉代碼位於main_activity.java中... – thevoipman

+0

要麼您使'radList'可訪問main_activity.java通過公開或將您的拉動代碼移動到stations.java,如果列表不與其他人共享類 – zzas11

+0

對,我有一個巨大的學習曲線......我該如何去做'radList.add(map);'public? – thevoipman

0

您是否檢查過響應? 如果不是,請檢查響應並使用斷點進行調試,以確定光標已到達哪裏,然後按f8鍵,檢查是否有可用的數據。

如果這可以幫助你,請讓我知道

相關問題