我试图在textview中获取JSON数据,但它会出错

时间:2017-04-15 08:14:09

标签: android json android-volley

我正在尝试将json数据导入textview,但是,它会出错。代码如下。

THIS IS MY JSON DATA

这是我的配置类

package com.example.hp.eprincipal;

/**
 * Created by hp on 4/15/2017.
 */

public class Config {
    public static final String DATA_URL = "http://smcs.eprincipal.in/api/studentdetails.aspx?user_name=&password=";
    public static final String KEY_REG = "Registration_No";
    public static final String KEY_NAME = "Student_name";
    public static final String KEY_ADDRESS = "address";
    public static final String JSON_ARRAY = "result";
}

以下是代码,我希望将URL中的数据转到textview

ImageView std;
private EditText editTextId,editTextPass;
private Button buttonGet;
private TextView textViewResult;
private ProgressDialog loading;

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

        std = (ImageView) findViewById(R.id.imageView2);
        Picasso.with(getApplicationContext()).load("

    http://www.shikshakiore.com/image/logo.png").into(std);

            editTextId = (EditText) findViewById(R.id.editText6);
            editTextPass = (EditText) findViewById(R.id.editText7);
            buttonGet = (Button) findViewById(R.id.button2);
            textViewResult = (TextView) findViewById(R.id.textView3);
            buttonGet.setOnClickListener(this);

        }



           private void getData() {
                String id = editTextId.getText().toString().trim();
                String pass = editTextPass.getText().toString().trim();
                if (id.equals("")) {
                    Toast.makeText(this, "Please enter an id", Toast.LENGTH_LONG).show();
                    return;
                }

                if (pass.equals("")) {
                    Toast.makeText(this, "Please enter password", Toast.LENGTH_LONG).show();
                    return;
                }

                loading = ProgressDialog.show(this,"Please wait...","Fetching...",false,false);

                String url = Config.DATA_URL+editTextId+editTextPass.getText().toString().trim();


                StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        loading.dismiss();
                        showJSON(response);
                    }
                },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                Toast.makeText(My_info.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
                            }
                        });

                RequestQueue requestQueue = Volley.newRequestQueue(this);
                requestQueue.add(stringRequest);
            }

            private void showJSON(String response){
                String Registration_No="";
                String Student_name="";
                String address = "";
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);
                    JSONObject collegeData = result.getJSONObject(0);
                    Registration_No = collegeData.getString(Config.KEY_REG);
                    Student_name = collegeData.getString(Config.KEY_NAME);
                    address = collegeData.getString(Config.KEY_ADDRESS);

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


 textViewResult.setText("Registration No:\t"+Registration_No+"\nStudent Name:\t"+Student_name+"\nAddress:\t" +address);
        }



            @Override
            public void onClick(View v) {
             getData();
            }
            }

1 个答案:

答案 0 :(得分:1)

从您提供的JSON响应中提供的图像是返回一个JSON对象,因此您实际上没有JSON数组,并且在您的Java代码中,您正在尝试获取关键“结果”的JSON数组,但是您的JSON中不存在该密钥。我很确定如果你在JSONArray之后设置一个断点,你会检查你的json对象是否已创建,但你的JSONArray却没有。

我认为您只需要做以下事情:

JSONObject collegeData = new JSONObject(response);
Registration_No = collegeData.getString(Config.KEY_REG);
Student_name = collegeData.getString(Config.KEY_NAME);
address = collegeData.getString(Config.KEY_ADDRESS);

因此,为了简要说明发生了什么,您有一个JSON响应,来自您创建的服务器,JSONObject获取所有密钥并将它们放入JSONObject,然后根据密钥访问该值。在您的JSON响应中,您从未有过“结果”,因此您尝试从该键创建一个JSONArray列表不存在,因此您无法从中获取任何内容。

希望这个答案可以帮助您更好地理解JSON,并帮助您解决问题。