Android 如何从URL获取值,在微调器中设置名称,并在片段中将选择值id发送到服务器
我是android开发人员中的新手,我正在从Android 如何从URL获取值,在微调器中设置名称,并在片段中将选择值id发送到服务器,android,json,spinner,android-spinner,Android,Json,Spinner,Android Spinner,我是android开发人员中的新手,我正在从URL获取数据,并希望在spinner中设置名称并将其id发送到服务器 我的JSON { "data": [{ "name": "RELIANCE CDMA", "ID": "1" }, { "name": "IDEA", "ID": "5" }, { "name": "AIRTEL", "ID": "7" }, { "name": "MTNL DELHI", "ID": "8" }, {
URL
获取数据,并希望在spinner中设置名称并将其id发送到服务器
我的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中
代码
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
}
});
}
}
私有类下载JSON扩展异步任务{
受保护的Void doInBackground(Void…参数){
json_data=new ArrayList();
datalist=newarraylist();
jsonobject=JSONfunctions
.getJSONfromURL(“http://www.myurl.com/index.php“”;//仅示例
Log.d(“响应:”,“>”+jsonobject);
试一试{
jsonarray=jsonobject.getJSONArray(“数据”);
for(int i=0;i
但这段代码只在微调器中显示名称,而不从我的json中选择任何id
应该是:
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
}
});
}
}
私有类下载JSON扩展异步任务{
受保护的Void doInBackground(Void…参数){
json_data=new ArrayList();
datalist=newarraylist();
jsonobject=JSONfunctions
.getJSONfromURL(“http://www.myurl.com/index.php“”;//仅示例
Log.d(“响应:”,“>”+jsonobject);
试一试{
jsonarray=jsonobject.getJSONArray(“数据”);
for(int i=0;i
但在我看来,您似乎不明白代码中发生了什么,所以强烈建议您在代码中进行整理
还有一个建议-不要使用AsyncTask,请看一看。谢谢您的回复,但ArrayList中的给定错误“add(java.lang.String)”无法应用(anndroid.util.Pair)我不理解这一行中的声明
datalist
为ArrayList datalist
,而不是ArrayList datailist
mySpinner.setAdapter(新的ArrayAdapter(getActivity(),android.R.layout.simple_spinner\u dropdown\u item,datalist))