如何从URL中将Json数据放入微调器中?

时间:2016-02-29 03:59:55

标签: android android-layout android-studio android-spinner android-json

我在文本框中获取了json我试图将这些数据放在微调器中,但无法这样做。下面是我的主要活动课,我正在使用Volley。

    public class Mainactivity  extends Activity {
        private String urlJsonArry = "https://www.abc.json";
        private static String TAG = MainActivity.class.getSimpleName();
        private Button btnMakeArrayRequest;

        // Progress dialog
        private ProgressDialog pDialog;

        private TextView txtResponse,txtResponse2,txtResponse3,txtResponse4;

        // temporary string to show the parsed response
        private String jsonResponse, jsonResponse2, jsonResponse3, jsonResponse4;
        Spinner spinner;
        ArrayAdapter<String> adapter;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.spinners);

            spinner= (Spinner) findViewById(R.id.spinner);




            btnMakeArrayRequest = (Button) findViewById(R.id.btnArrayRequest);
            txtResponse = (TextView) findViewById(R.id.txtResponse);
            txtResponse2 = (TextView) findViewById(R.id.txtResponse2);
            txtResponse3 = (TextView) findViewById(R.id.txtResponse3);
            //  txtResponse4 = (TextView) findViewById(R.id.txtResponse4);


            pDialog = new ProgressDialog(this);
            pDialog.setMessage("Please wait...");
            pDialog.setCancelable(false);

            btnMakeArrayRequest.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // making json array request
                    makeJsonArrayRequest();
                }
            });

        }



        private void makeJsonArrayRequest () {

        showpDialog();
        JsonObjectRequest req = new JsonObjectRequest(urlJsonArry,

                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        Log.d(TAG, response.toString());

                        try {

                            JSONArray array = response.getJSONArray("results");
                            // Parsing json array response
                            // loop through each json object
                            jsonResponse = "";
                            for (int i = 0; i < array.length(); i++) {

                                JSONObject person = (JSONObject) array
                                        .get(i);

                                System.out.println(person.toString());

                                String syllabus = person.getString("grade");
                                String grade = person.getString("grade");
                                String subject = person.getString("subject");
                                jsonResponse += "Board: " + syllabus + "\n\n";
                                jsonResponse2 += "Class: " + grade + "\n\n";
                                jsonResponse3 += "Subject: " + subject + "\n\n";
                                // jsonResponse4 += "ID: " + id + "\n\n";

//Getting Compile time error here

 adapter= new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,array);
                                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                                spinner.setAdapter(adapter);}



                            txtResponse.setText(jsonResponse);



                            txtResponse2.setText(jsonResponse2);
                            txtResponse3.setText(jsonResponse3);
                            //txtResponse4.setText(jsonResponse4);


                        } catch (JSONException e) {
                            e.printStackTrace();
                            Toast.makeText(getApplicationContext(),
                                    "Error: " + e.getMessage(),
                                    Toast.LENGTH_LONG).show();
                        }

                        hidepDialog();
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                Toast.makeText(getApplicationContext(),
                        error.getMessage(), Toast.LENGTH_SHORT).show();
                hidepDialog();
            }
             });

2 个答案:

答案 0 :(得分:0)

您需要使用活动上下文this不是活动上下文尝试这样

adapter= new ArrayAdapter<String>(Mainactivity.this, android.R.layout.simple_spinner_item, array);

编辑:

更改阵列格式。例如,在您的微调器中,您将显示主题,请执行以下操作

String[] mySubject = new String[array.length()];
for (int i = 0; i < array.length(); i++) {

                                JSONObject person = (JSONObject) array
                                        .get(i);

                                System.out.println(person.toString());

                                String syllabus = person.getString("grade");
                                String grade = person.getString("grade");
                                String subject = person.getString("subject");
                                jsonResponse += "Board: " + syllabus + "\n\n";
                                jsonResponse2 += "Class: " + grade + "\n\n";
                                jsonResponse3 += "Subject: " + subject + "\n\n";
                                // jsonResponse4 += "ID: " + id + "\n\n";

                                mySubject[i] = subject;

}


 adapter= new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, mySubject);
                                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                                spinner.setAdapter(adapter);

注意:  您还需要将设置适配器移出for循环。

答案 1 :(得分:0)

您正在为JsonObjectRequest回调在内部类中填充适配器,因此这指的是匿名内部类(new Response.Listener<JSONObject>())。要解决此问题,您可以更改以下行:

adapter= new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,array);

adapter= new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item,array);