我试图从远程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