运行Oncreate(android)时Json Array返回空白(“”)

时间:2015-11-30 03:57:29

标签: php android arrays json android-volley

我正在尝试使用volley库从服务器获取json数据并存储在数组列表中,然后使用微调器显示数据。

我在PHP服务器端的代码:

echo json_encode(array('result'=>$result));

我的获取数据功能:

private void getEventRespondTest (RequestQueue requestQueue) {
        //Creating a string request
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,Config.DATA_URL,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject respond) {
                            try {
                                eventArray = respond.getJSONArray("result");
                                Toast.makeText(Beacon_MainActivity.this,eventArray.toString(),Toast.LENGTH_LONG).show();
                                eventDetail = getEventDetail(eventArray);

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
//                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                            Toast.makeText(Beacon_MainActivity.this, "Unable to fetch data: " +error.getMessage(),Toast.LENGTH_LONG).show();
                        }
                    }
                );
        //Adding request to the queue
        requestQueue.add(jsonObjectRequest);
    }

    private ArrayList getEventDetail(JSONArray j) {
        ArrayList event = new ArrayList();
        //Traversing through all the items in the json array
        for (int i = 0; i < j.length(); i++) {
            try {
                //Getting json object
                JSONObject json = j.getJSONObject(i);

                //Adding the name of the event to array list
                event.add(json.getString(Config.EVENT_TITLE));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        Toast.makeText(Beacon_MainActivity.this,event.toString(),Toast.LENGTH_LONG).show();
        return event;
    }

在我的OnCreate方法中,我尝试使用微调器获取数据并显示结果,但在运行时,结果返回第一次为空“

eventDetail = new ArrayList<>();
        eventArray = new JSONArray();

//        //get event from server
        getEventRespondTest(Volley.newRequestQueue(Beacon_MainActivity.this));
        spinner.setAdapter(new ArrayAdapter<>(Beacon_MainActivity.this, android.R.layout.simple_spinner_dropdown_item, eventDetail));

任何帮助都非常感谢。感谢。

2 个答案:

答案 0 :(得分:0)

您的函数private void getEventRespondTest返回null,因为它是Void。你在做什么with eventDetail = getEventDetail(eventArray); getEventRespondTest的功能?或者尝试将POST更改为GET?

更新检查此代码

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getEventRespondTest();
    }


 private void getEventRespondTest () {
        RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
        StringRequest request = new StringRequest("http://vinhvumobile.com/phpconnect/geteventdetail.php", new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                // we got the response, now our job is to handle it
                getDataFromJson(response);
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                //something happened, treat the error.
                Log.e("Error", error.toString());
            }
        });
        queue.add(request);
    }

    private ArrayList getDataFromJson(String json) {
        ArrayList events=new ArrayList();
        JSONArray array;
        try {

            JSONObject gallery=new JSONObject(json);
            array=gallery.getJSONArray("result");
            //Do sth with it

            for(int i=0;i<array.length();i++){
                JSONObject jsonOBJ = (JSONObject) array.get(i);
                events.add(jsonOBJ.get("EventID"));
            }

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

答案 1 :(得分:0)

我认为您在方法false public void onResponse(JSONObject respond)中做出回应,因为您使用respond is null发出请求,但您的服务器(php)返回JsonObjectRequest它没有任何意义{{1} } 回来。尝试使用String代替JsonObject。在最好的解决方案是更改代码您的服务器返回一个`JsonObject'跟随此(PHP文件):

StringRequest