我是android dev的新手,我从URL
获取数据,并希望在微调器中设置名称并将其ID发送到服务器
MY JSON
{
"data": [{
"name": "RELIANCE CDMA",
"ID": "1"
}, {
"name": "IDEA",
"ID": "5"
}, {
"name": "AIRTEL",
"ID": "7"
}, {
"name": "MTNL DELHI",
"ID": "8"
}, {
"name": "MTNL MUMBAI",
"ID": "8"
}, {
"name": "VODAFONE",
"ID": "12"
}, {
"name": "LOOP",
"ID": "13"
}, {
"name": "BSNL",
"ID": "14"
}, {
"name": "AIRCEL",
"ID": "19"
}, {
"name": "TATA INDICOM",
"ID": "23"
}, {
"name": "VIRGIN CDMA",
"ID": "27"
}, {
"name": "RELIANCE GSM",
"ID": "32"
}, {
"name": "TATA DOCOMO",
"ID": "35"
}, {
"name": "VIRGIN GSM",
"ID": "36"
}, {
"name": "MTS",
"ID": "38"
}, {
"name": "UNINOR",
"ID": "41"
}, {
"name": "VIDEOCON",
"ID": "43"
}, {
"name": "T24",
"ID": "52"
}]
}
我正在使用此代码 在android中
CODE
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... params) {
json_data = new ArrayList<Json_Data>();
datalist = new ArrayList<String>();
jsonobject = JSONfunctions
.getJSONfromURL("http://www.myurl.com/index.php"); // example only
Log.d("Response: ", "> " + jsonobject);
try {
jsonarray = jsonobject.getJSONArray("data");
for (int i = 0; i < jsonarray.length(); i++) {
jsonobject = jsonarray.getJSONObject(i);
Json_Data worldpop = new Json_Data();
worldpop.setName(jsonobject.optString("name"));
worldpop.setId(jsonobject.optString("ID"));
json_data.add(worldpop);
datalist.add(jsonobject.optString("name"));
}
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void args) {
Spinner mySpinner = (Spinner)getView().findViewById(R.id.operator_spinner);
mySpinner
.setAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_dropdown_item,
datalist));
mySpinner
.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0,
View arg1, int position, long arg3) {
String opt_code = datalist.get(position);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}
但是此代码仅在微调器中显示名称,但未选择任何id形式我的json
答案 0 :(得分:2)
应该是:
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... params) {
json_data = new ArrayList<Json_Data>();
datalist = new ArrayList<Pair<String, String>>();
jsonobject = JSONfunctions
.getJSONfromURL("http://www.myurl.com/index.php"); // example only
Log.d("Response: ", "> " + jsonobject);
try {
jsonarray = jsonobject.getJSONArray("data");
for (int i = 0; i < jsonarray.length(); i++) {
jsonobject = jsonarray.getJSONObject(i);
Json_Data worldpop = new Json_Data();
worldpop.setName(jsonobject.optString("name"));
worldpop.setId(jsonobject.optString("ID"));
json_data.add(worldpop);
datalist.add(new Pair<String, String>(jsonobject.optString("ID"), jsonobject.optString("name"));
}
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void args) {
Spinner mySpinner = (Spinner) getView().findViewById(R.id.operator_spinner);
mySpinner
.setAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_dropdown_item,
datalist));
mySpinner
.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0,
View arg1, int position, long arg3) {
String opt_code = datalist.get(position).first;
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}
但在我看来,你不明白该代码中发生了什么,所以强烈建议你理解这一点。
还有一条建议 - 不要使用AsyncTask,并查看Retrofit。