本文介绍了如何从URL中获取价值和微调器中设置名称和发送选择的值id到服务器中的片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的Andr​​oid开发人员我是从网​​址获取数据并希望在微调设置名称,并发送其ID到服务器

我的JSON

  {
数据:[{
    名:倚CDMA
    ID为1
},{
    名:IDEA
    ID:5
},{
    名:AIRTEL
    ID:7
},{
    名:MTNL DELHI
    ID:8
},{
    名:MTNL孟买
    ID:8
},{
    名:VODAFONE
    ID:12
},{
    名:循环,
    ID:13
},{
    名:BSNL
    ID:14
},{
    名:商Aircel
    ID:19
},{
    名:TATA Indicom的
    ID:23
},{
    名:VIRGIN CDMA
    ID:27
},{
    名:倚GSM
    ID:32
},{
    名:塔塔Docomo
    ID:35
},{
    名:VIRGIN GSM
    ID:36
},{
    名:MTS
    ID:38
},{
    名:UNINOR
    ID:41
},{
    名:VIDEOCON
    ID:43
},{
    名:T24
    ID:52
}]
}

和我使用这个code
在android系统

code

 私有类DownloadJSON扩展的AsyncTask<太虚,太虚,太虚> {
    保护无效doInBackground(虚空...... PARAMS){
        json_data =新的ArrayList< Json_Data>();
        DataList控件=新的ArrayList<串GT;();
        的JSONObject = JSONfunctions
                .getJSONfromURL(http://www.myurl.com/index.php);只有//例子
        Log.d(回应:,>中+的JSONObject);
        尝试{
            jsonarray = jsonobject.getJSONArray(数据);
            的for(int i = 0; I< jsonarray.length();我++){
                的JSONObject = jsonarray.getJSONObject(ⅰ);
                Json_Data worldpop =新Json_Data();
                worldpop.setName(jsonobject.optString(名字));
                worldpop.setId(jsonobject.optString(ID));
                json_data.add(worldpop);
                datalist.add(jsonobject.optString(名字));
            }
        }赶上(例外五){
            Log.e(错误,e.getMessage());
            e.printStackTrace();
        }
        返回null;
    }
    保护无效onPostExecute(无效参数){        微调mySpinner =(微调)getView()findViewById(R.id.operator_spinner)。
        mySpinner
                .setAdapter(新ArrayAdapter<串GT;(getActivity()
                        android.R.layout.simple_spinner_dropdown_item,
                        DataList控件));
        mySpinner
                .setOnItemSelectedListener(新AdapterView.OnItemSelectedListener(){
                    公共无效onItemSelected(适配器视图<>为arg0,
                                               查看ARG1,INT位置,长ARG3){
                        字符串opt_ code = datalist.get(位置);
                    }                    公共无效onNothingSelected(适配器视图<>为arg0){
                        // TODO自动生成方法存根
                    }
                });
    }
}

但这种code只显示微调的名字,但没有选择任何ID形成的json


解决方案

它应该是:

 私有类DownloadJSON扩展的AsyncTask<太虚,太虚,太虚> {
    保护无效doInBackground(虚空...... PARAMS){
        json_data =新的ArrayList< Json_Data>();
        DataList控件=新的ArrayList<对<字符串,字符串>>();
        的JSONObject = JSONfunctions
                .getJSONfromURL(http://www.myurl.com/index.php);只有//例子
        Log.d(回应:,>中+的JSONObject);
        尝试{
            jsonarray = jsonobject.getJSONArray(数据);
            的for(int i = 0; I< jsonarray.length();我++){
                的JSONObject = jsonarray.getJSONObject(ⅰ);
                Json_Data worldpop =新Json_Data();
                worldpop.setName(jsonobject.optString(名字));
                worldpop.setId(jsonobject.optString(ID));
                json_data.add(worldpop);
                datalist.add(新配对<字符串,字符串>(jsonobject.optString(ID),jsonobject.optString(名字));
            }
        }赶上(例外五){
            Log.e(错误,e.getMessage());
            e.printStackTrace();
        }
        返回null;
    }    保护无效onPostExecute(无效参数){
        微调mySpinner =(微调)getView()findViewById(R.id.operator_spinner)。
        mySpinner
                .setAdapter(新ArrayAdapter<串GT;(getActivity()
                        android.R.layout.simple_spinner_dropdown_item,
                        DataList控件));
        mySpinner
                .setOnItemSelectedListener(新AdapterView.OnItemSelectedListener(){
                    公共无效onItemSelected(适配器视图<>为arg0,
                                               查看ARG1,INT位置,长ARG3){
                        字符串opt_ code = datalist.get(位置)。首先,
                    }                    公共无效onNothingSelected(适配器视图<>为arg0){
                        // TODO自动生成方法存根
                    }
                });
    }
}

但在我看来,你不明白这是怎么回事在code,所以强烈建议在理清。

和多了一个建议 - don'y使用的AsyncTask,并采取

I am new in android dev i am fetch data from URL and want to set name in spinner and send their id to server

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"
}]
}

and i am using this code in 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
                    }
                });
    }


}

but this code only show name in spinner but not select any id form my json

解决方案

It should be:

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
                    }
                });
    }
}

But it seems to me that you don't understand what's going on in that code, so highly suggest to sort out in that.

And one more advice - don'y use AsyncTask, and take a look on Retrofit.

这篇关于如何从URL中获取价值和微调器中设置名称和发送选择的值id到服务器中的片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 00:49