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

时间:2016-03-22 20:27:05

标签: java php android json database

我从android中的数据库中获取数据,我可以看到使用POSTMAN从JSON格式的数据库获取数据但是当我试图在我的Android应用程序中显示它时,它没有显示任何值。 来自POSTMAN的值:

 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);

我正在使用以下一些教程和代码:

{{1}}

2 个答案:

答案 0 :(得分:0)

假设您尝试调试它,这可能是Android清单中的用户权限问题。确保您在清单文件中具有以下权限。

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

答案 1 :(得分:0)

如果您真的通过API获得响应,您可能需要检查JSON解析代码这是我在假设问题中的响应时编写的内容

private void showJSON(String response){
    try{
        Log.d(TAG, "showJSON: \n"+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;
    }
}