如何使用URL处理多个微调器?

时间:2018-04-06 06:34:31

标签: android json http android-spinner

在我的应用程序中,我正在使用两个微调器,第一个微调器包含部门和ID 等数据,第二个微调器包含缺陷和id ,这里我使用的是一些URL如下。

http://182.18.163.39/m/def.php?issue=1

此URL为我提供了第一个微调器值。在从第一个微调器中选择一个值时,与该值相关的数据将出现在第二个微调器中。

这是我的第二个URL,它给出了第二个微调器值。

http://182.18.163.39/m/def.php?issue=2&dept=” +的dept_id

在这个URL中我想从第一个URL传递id,这是我的问题我能够从第一个微调器获取所有值。当我从第一个微调器中选择一个值时,它不会在第二个微调器中给出相关值。 任何人都给我一个解决这个问题的示例代码。

以下是代码:

  //Department spinner
    Spinner spinner;
    String URL="http://182.18.163.39/train/m/def.php?issue=1";
    ArrayList<String> Department;
    String uid;

    //Deficiency category spinner
    Spinner spinner2;
    String URL2 = "http://182.18.163.39/train/m/def.php?issue=2&dept="+dept_id;
    ArrayList<String>Deficiency;
    String defid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.new_deficiency);

        //spinner for department
        Department = new ArrayList<>();
        spinner=(Spinner)findViewById(R.id.select_department);
        loadSpinnerData(URL);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                String department = spinner.getItemAtPosition(spinner.getSelectedItemPosition()).toString();
                Toast.makeText(getApplicationContext(),department,Toast.LENGTH_LONG).show();
            }
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                // DO Nothing here
            }
        });

        //spinner for deficiency
        Deficiency = new ArrayList<>();
        spinner2 = (Spinner)findViewById(R.id.deficiency_category);
        loadSpinnerDeficiency(URL2);

        spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                String deficiency =   spinner2.getItemAtPosition(spinner2.getSelectedItemPosition()).toString();
                Toast.makeText(getApplicationContext(),deficiency,Toast.LENGTH_LONG).show();
            }
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                // DO Nothing here
            }
        });

    }

    private void loadSpinnerData(String url) {

        try {

            HttpClient httpClient = new DefaultHttpClient();
            HttpGet httpPost = new HttpGet(url);
            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();

            String responseString = EntityUtils.toString(entity);

            JSONArray jsonarray = new JSONArray(responseString);
            for (int i = 0; i < jsonarray.length(); i++) {
                JSONObject jsonobject = jsonarray.getJSONObject(i);
                String department = jsonobject.getString("Department");
                dept_id = jsonobject.getString("ID");
                Department.add(department);
            }

            spinner.setAdapter(new ArrayAdapter<String>(NewDeficiency.this, android.R.layout.simple_spinner_dropdown_item, Department));

        } catch (JSONException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private void loadSpinnerDeficiency(String url2) {

        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpGet httpPost = new HttpGet("http://182.18.163.39/train/m/def.php?issue=2&dept="+dept_id);
            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();

            String responseString = EntityUtils.toString(entity);

            JSONArray jsonarray = new JSONArray(responseString);
            for (int i = 0; i < jsonarray.length(); i++) {
                JSONObject jsonobject = jsonarray.getJSONObject(i);
                String defcat = jsonobject.getString("Deficiency");
                defid = jsonobject.getString("ID");

                Deficiency.add(defcat);
            }

            spinner2.setAdapter(new ArrayAdapter<String>(NewDeficiency.this, android.R.layout.simple_spinner_dropdown_item, Deficiency));

        } catch (JSONException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    }

这是我对第一个网址的json回复:

[{
    "Department": "Select Department",
    "ID": ""
}, {
    "Department": "Engg North",
    "ID": "1"
}, {
    "Department": "DIESEL SHED",
    "ID": "2"
}, {
    "Department": "S &amp; T",
    "ID": "3"
}, {
    "Department": "SAFETY",
    "ID": "4"
}, {
    "Department": "Srdenwest",
    "ID": "5"
}, {
    "Department": "enggwest",
    "ID": "6"
}, {
    "Department": "Engg central",
    "ID": "7"
}, {
    "Department": "electmaint",
    "ID": "8"
}, {
    "Department": "Operating",
    "ID": "9"
}, {
    "Department": "Security",
    "ID": "10"
}, {
    "Department": "Diesel  Shed",
    "ID": "11"
}, {
    "Department": "Electric Loco shed",
    "ID": "12"
}, {
    "Department": "C &amp; W",
    "ID": "13"
}, {
    "Department": "trso",
    "ID": "14"
}, {
    "Department": "trdohe",
    "ID": "15"
}, {
    "Department": "engg",
    "ID": "16"
}, {
    "Department": "commercial",
    "ID": "17"
}, {
    "Department": "engg South",
    "ID": "18"
}, {
    "Department": "Mechanical",
    "ID": "19"
}]

这是我对第二个网址的json回复:

[{
    "Deficiency": "Select Deficiency",
    "ID": ""
}, {
    "Deficiency": "Engg",
    "ID": "1"
}, {
    "Deficiency": "Track",
    "ID": "25"
}, {
    "Deficiency": "LCs",
    "ID": "26"
}, {
    "Deficiency": "Misc",
    "ID": "43"
}, {
    "Deficiency": "",
    "ID": "NULL"
}]

1 个答案:

答案 0 :(得分:0)

你shuold创建一个自定义对象,如

 private HashMap<String,String>departmentMap=new HashMap<>();
private void loadSpinnerData(String url) {

    try {

        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpPost = new HttpGet(url);
        HttpResponse response = httpClient.execute(httpPost);
        HttpEntity entity = response.getEntity();

        String responseString = EntityUtils.toString(entity);

        JSONArray jsonarray = new JSONArray(responseString);
        for (int i = 0; i < jsonarray.length(); i++) {
            JSONObject jsonobject = jsonarray.getJSONObject(i);
            String department = jsonobject.getString("Department");
            uid = jsonobject.getString("ID");
            Department.add(department);
            departmentMap.put(department,uid);
        }

        spinner.setAdapter(new ArrayAdapter<String>(NewDeficiency.this, android.R.layout.simple_spinner_dropdown_item, Department));

    } catch (JSONException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

并修改你的firstSpinner onclick,如

 spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l){
            String department=spinner.getItemAtPosition(spinner.getSelectedItemPosition()).toString();
            Toast.makeText(getApplicationContext(),department,Toast.LENGTH_LONG).show();
            String id=departmentMap.get(department);
            loadSpinnerDeficiency(URL2,id);
        }
        @Override
        public void onNothingSelected(AdapterView<?> adapterView){
            // DO Nothing here
        }
    });

现在您可以在loadSpinnerDeficiency方法中使用id来获得正确的响应

 private void loadSpinnerDeficiency(String url2,String uid) {

        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpGet httpPost = new HttpGet("http://182.18.163.39/train/m/def.php?issue=2&dept="+uid);
            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();

            String responseString = EntityUtils.toString(entity);

            JSONArray jsonarray = new JSONArray(responseString);
            for (int i = 0; i < jsonarray.length(); i++) {
                JSONObject jsonobject = jsonarray.getJSONObject(i);
                String defcat = jsonobject.getString("Deficiency");
                defid = jsonobject.getString("ID");

                Deficiency.add(defcat);
            }

            spinner2.setAdapter(new ArrayAdapter<String>(NewDeficiency.this, android.R.layout.simple_spinner_dropdown_item, Deficiency));

        } catch (JSONException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }