JSON不在Spinner中显示内容

时间:2018-01-30 19:01:00

标签: android json android-volley

我遇到了一个问题。我不明白我的代码错在哪里,我有4个微调器,所有这些都显示相同的JSON文件。这四件事都是一样的。但只有其中一个没有显示任何东西。在您看来,此代码的问题在哪里?

URL_AllUniversity正确工作。但我不明白问题是什么...... 还有更好的给你一个我看到的屏幕: enter image description here 而且,这是我的University网址 enter image description here

[ { "University": "دانشگاه آيت ا... العظمي حائري ميبد" }, { "University": "دانشگاه اراک" }, { "University": "دانشگاه اردکان" }, { "University": "دانشگاه اروميه" }, { "University": "دانشگاه اصفهان" }, { "University": "دانشگاه الزهرا(س)" },...

这是我的Java代码:

    public class FormActivity extends AppCompatActivity {

Spinner spinnerStudy,spinnerUnited,spinnerCity,spinnerUniversity;
ArrayList<String> AllStudy,AllUnited,AllCity, AllUniversity;

String URL_AllStudy = "http://192.168.1.103/android_login_api/helper/AllStudy.json";
String URL_AllUnited = "http://192.168.1.103/android_login_api/helper/AllUnited.json";
String URL_AllCity = "http://192.168.1.103/android_login_api/helper/AllCity.json";
String URL_AllUniversity = "http://192.168.1.103/android_login_api/helper/AllUniversity.json";

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

    AllStudy = new ArrayList<>();
    AllUnited = new ArrayList<>();
    AllUniversity = new ArrayList<>();
    AllCity = new ArrayList<>();


    spinnerStudy = findViewById(R.id.spinner_AllStudy);
    spinnerUnited = findViewById(R.id.spinner_AllUnited);
    spinnerUniversity = findViewById(R.id.spinner_AllUniversity);
    spinnerCity = findViewById(R.id.spinner_AllCity);


    loadSpinnerDataStudy(URL_AllStudy);
    loadSpinnerDataUnited(URL_AllUnited);
    loadSpinnerDataUniversity(URL_AllUniversity);
    loadSpinnerDataCity(URL_AllCity);


    spinnerStudy.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {


        String all_study =  spinnerStudy.getItemAtPosition(spinnerStudy.getSelectedItemPosition()).toString();


        Toast.makeText(getApplicationContext(),all_study, Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {
        // DO Nothing here
    }
    });

    spinnerCity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {


            String all_city =  spinnerCity.getItemAtPosition(spinnerCity.getSelectedItemPosition()).toString();



            Toast.makeText(getApplicationContext(),all_city, Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {
            // DO Nothing here
        }
    });

    spinnerUniversity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {


            String all_university =  spinnerUniversity.getItemAtPosition(spinnerUniversity.getSelectedItemPosition()).toString();

            Toast.makeText(getApplicationContext(),all_university, Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {
            // DO Nothing here
        }
    });

    spinnerUnited.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {


            String all_united =  spinnerUnited.getItemAtPosition(spinnerUnited.getSelectedItemPosition()).toString();

            Toast.makeText(getApplicationContext(),all_united, Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {
            // DO Nothing here
        }
    });


}




private void loadSpinnerDataUnited(String URL_AllUnited) {


    RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
    StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_AllUnited, new Response.Listener<String>() {


        @Override
        public void onResponse(String response) {


            try {

                ArrayList<String> AllUnited = new ArrayList<>();
                JSONArray jsonarray = new JSONArray(response);

                for (int i = 0; i < jsonarray.length(); i++) {
                    JSONObject jsonobject = jsonarray.getJSONObject(i);
                    AllUnited.add(new String(jsonobject.getString("United").getBytes("ISO-8859-1"), "UTF-8"));


                }
                spinnerUnited.setAdapter(new ArrayAdapter<>(FormActivity.this,
                        android.R.layout.simple_spinner_dropdown_item, AllUnited));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }

    });

    int socketTimeout = 30000;
    RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
    stringRequest.setRetryPolicy(policy);
    requestQueue.add(stringRequest);
}

    private void loadSpinnerDataCity (String URL_AllCity) {


    RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
    StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_AllCity, new Response.Listener<String>() {


        @Override
        public void onResponse(String response) {


            try {

                ArrayList<String> AllCity = new ArrayList<>();
                JSONArray jsonarray = new JSONArray(response);

                for (int i = 0; i < jsonarray.length(); i++) {
                    JSONObject jsonobject = jsonarray.getJSONObject(i);
                    AllCity.add(new String(jsonobject.getString("City").getBytes("ISO-8859-1"), "UTF-8"));


                }
                spinnerCity.setAdapter(new ArrayAdapter<>(FormActivity.this,
                        android.R.layout.simple_spinner_dropdown_item, AllCity));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }

    });

    int socketTimeout = 30000;
    RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
    stringRequest.setRetryPolicy(policy);
    requestQueue.add(stringRequest);
}

private void loadSpinnerDataUniversity (String URL_AllUniversity) {
    RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
    StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_AllUniversity, new Response.Listener<String>() {


        @Override
        public void onResponse(String response) {


            try {


                ArrayList<String> AllUniversity = new ArrayList<>();
                JSONArray jsonarray = new JSONArray(response);

                for (int i = 0; i < jsonarray.length(); i++) {
                    JSONObject jsonobject = jsonarray.getJSONObject(i);
                    AllUniversity.add(new String(jsonobject.getString("University").getBytes("ISO-8859-1"), "UTF-8"));

                    Toast.makeText(FormActivity.this,response,Toast.LENGTH_SHORT);
                }
                spinnerUniversity.setAdapter(new ArrayAdapter<>(FormActivity.this,
                        android.R.layout.simple_spinner_dropdown_item, AllUniversity));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }

    });

    int socketTimeout = 30000;
    RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
    stringRequest.setRetryPolicy(policy);
    requestQueue.add(stringRequest);
}

private void loadSpinnerDataStudy(String URL_AllStudy) {


        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_AllStudy, new Response.Listener<String>() {


            @Override
            public void onResponse(String response) {


                try {

                    ArrayList<String> AllStudy = new ArrayList<>();
                    JSONArray jsonarray = new JSONArray(response);

                    for (int i = 0; i < jsonarray.length(); i++) {
                        JSONObject jsonobject = jsonarray.getJSONObject(i);
                        AllStudy.add(new String(jsonobject.getString("Study").getBytes("ISO-8859-1"), "UTF-8"));


                    }
                    spinnerStudy.setAdapter(new ArrayAdapter<>(FormActivity.this,
                            android.R.layout.simple_spinner_dropdown_item, AllStudy));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }

        });

        int socketTimeout = 30000;
        RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
        stringRequest.setRetryPolicy(policy);
        requestQueue.add(stringRequest);
}

}

1 个答案:

答案 0 :(得分:2)

您从此行删除编码

 AllUniversity.add(new String(jsonobject.getString("University").getBytes("ISO-8859-1"), "UTF-8"));

并按照这样做

 AllUniversity.add(new String(jsonobject.getString("University")));