我試圖從遠程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;
}
}
請幫我把這個網址進入我的代碼,太感謝你了!
不能得到任何你有什麼問你在哪裏得到'json'數據?在哪個變量中? – Pankaj
你能展示拉動json數據的代碼嗎? – zzas11
@ zzas11我能夠拉它就好了......我只是不知道如何把它放在我當前的數組以及靜態字段中......我還添加了拉動JSON的代碼.. – thevoipman