从数据库中获取后,JSON对象不显示值

I am fetching data from database in android and i can see data fetching from databse in JSON format using POSTMAN but when i am trying to display it in my android application, its not displaying any value. Values from POSTMAN:

{
  "result": [
    {
      "Date": "18-3-2016",
      "Events": "Local Holiday"
    },
    {
      "Date": "23-3-2016",
      "Events": "Monthly Fees"
    },
    {
      "Date": "15-4-2016",
      "Events": "Monthly Fees"
    },
    {
      "Date": "23-4-2016",
      "Events": "Annual Day"
    },
    {
      "Date": "30-4-2016",
      "Events": "session end"
    },
    {
      "Date": "9-4-2016",
      "Events": "Parent Teacher Meeting"
    }
  ]
}

I am following some tutorials and Code using:

 private void getData() {


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

        String url = config_events.DATA_URL;

        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(events.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
                    }
                });

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

    private void showJSON(String response) {

        String date = "";
        String comment="";
        //String vc = "";
        try {
            JSONObject jsonObject = new JSONObject(response);
            JSONArray result = jsonObject.getJSONArray(config_events.JSON_ARRAY);
            JSONObject collegeData = result.getJSONObject(0);
            date = collegeData.getString(config_events.KEY_NAME);
            comment = collegeData.getString(config_events.KEY_ADDRESS);
            //vc = collegeData.getString(config_events.KEY_VC);
        } catch (JSONException e) {
            e.printStackTrace();
        }
textViewResult.setText("Date:"+date + "Comment:"+ comment);
doudeng9425
doudeng9425 什么是错误
4 年多之前 回复
douliedai4838
douliedai4838 尝试将日志状态添加到catch();看起来你的try块中的某些东西会失败,它会停止日期,注释要分配的值。
4 年多之前 回复

2个回答

Assuming that you have tried to debug it, this could be the problem of user-permissions in Android manifest. Make sure that you have following permissions in your manifest file.

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
douju1865
douju1865 给出了所有权限
4 年多之前 回复

if you really getting the response through the API you might want to check the JSON parsing code this is something I wrote while assuming the response in the question

private void showJSON(String response){
    try{
        Log.d(TAG, "showJSON: 
"+response);// print here to check you are getting the right response
        JSONObject response_JsonObject = new JSONObject(response);
        JSONArray result_JsonArray = response_JsonObject.getJSONArray("result");

        ArrayList<Event> events = new ArrayList<>();
        for (int i = 0; i < result_JsonArray.length(); i++) {
            Event single_Event = new Event();
            single_Event.setDate(result_JsonArray.getJSONObject(i).getString("Date"));
            single_Event.setEvent(result_JsonArray.getJSONObject(i).getString("Events"));
            events.add(single_Event);
        }
        Log.d(TAG, "showJSON: Event list size: "+events.size()); // check number of elements
    }catch (Exception e){
        e.printStackTrace();
    }
}

private class Event{

    private String date;
    private String event;

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getEvent() {
        return event;
    }

    public void setEvent(String event) {
        this.event = event;
    }
}
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐