为什么我无法将值添加到ArrayList

时间:2018-02-10 08:12:58

标签: java android arraylist

我尝试制作应用程序以获取gmaps API的结果。

我想使用数组列表来保存JSONObject的结果 来自result_name.toString()的日志返回正确的数组, 但result_phone.toString()的日志为空。

我很困惑,因为res值是正确的。 checkEmptyJsonResponse是我创建的一个类,用于检查international_phone_number的值是否为空JSON响应。 它返回JSON值或"不可用"

String res = checkEmptyJsonResponse(result,"international_phone_number");

在这种情况下,如果我把Log看到res的值,它会返回正确的值。

在result_phone.add(res)中似乎有问题,但我不知道

我做错了什么?

以下是我使用的代码部分

FragmentOne.java

ArrayList<String> result_name = new ArrayList<String>();
ArrayList<String> result_address = new ArrayList<String>();
ArrayList<String> result_placeid = new ArrayList<String>();
ArrayList<String> result_phone = new ArrayList<String>();

public void getLocation(String tipe_instansi){
    final AsyncHttpClient client = new AsyncHttpClient();

    final StringBuilder[] googlePlacesUrl = {new StringBuilder("https://maps.googleapis.com/maps/api/place/nearbysearch/json?")};
    googlePlacesUrl[0].append("location=" + latitude + "," + longitude);
    googlePlacesUrl[0].append("&rankby=prominence");
    googlePlacesUrl[0].append("&radius=" + PROXIMITY_RADIUS);
    googlePlacesUrl[0].append("&type="+tipe_instansi);
    googlePlacesUrl[0].append("&sensor=true");
    googlePlacesUrl[0].append("&key=AIzaSyCtaLQPSPN2SwGM2cr_itZIkWG0sjQJ7uc");
    Log.d("URL : ", googlePlacesUrl[0].toString());


    client.get(googlePlacesUrl[0].toString(), new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
            if(responseBody!=null){
                try {
                    JSONObject array = new JSONObject(new String(responseBody));
                    JSONArray result = array.getJSONArray("results");

                    for(int i=0; i<result.length(); i++){
                        JSONObject get_result = result.getJSONObject(i);

                        result_name.add(get_result.getString("name"));
                        result_address.add(get_result.getString("vicinity"));
                        result_placeid.add(get_result.getString("place_id"));

                        Log.d("PLACE ID : ", result_placeid.get(i).toString());

                        googlePlacesUrl[0] = new StringBuilder("https://maps.googleapis.com/maps/api/place/details/json?");
                        googlePlacesUrl[0].append("placeid=" + result_placeid.get(i).toString());
                        googlePlacesUrl[0].append("&key=AIzaSyCtaLQPSPN2SwGM2cr_itZIkWG0sjQJ7uc");

                        Log.d("URL2 : ", googlePlacesUrl[0].toString());

                        client.get(googlePlacesUrl[0].toString(), new AsyncHttpResponseHandler() {
                            @Override
                            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                                try {
                                    JSONObject array = new JSONObject(new String(responseBody));
                                    JSONObject result = array.getJSONObject("result");

                                    String res = checkEmptyJsonResponse(result, "international_phone_number");
                                    result_phone.add(res);

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

                            @Override
                            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                                Toast.makeText(getActivity(), "Failed to Get Phone Number", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }

                    for(int k = 0; k<result.length(); k++){
                        Log.d("RESULTZZ - ", "Name : "+result_name.get(k)+ " Address : "+result_address.get(k));
                    }
                    Log.d("RESULTZZZ - ", result_name.toString());
                    Log.d("RESULTZZZZZZ - ", result_phone.toString());

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            else{
                Toast.makeText(getActivity(), "responseBody Null", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
            Toast.makeText(getActivity(), "Failure to Connect", Toast.LENGTH_SHORT).show();
        }
    });
}

1 个答案:

答案 0 :(得分:-1)

这是因为您从第二个 ASYNC 请求中调用result_phone.add(res);(它刚刚启动,但稍后会完成,而不是现在完成),因此在第一个请求的回调中调用Log.d("RESULTZZZZZZ - ", result_phone.toString());时 - 还没有结果。