简体   繁体   中英

Why i can't add the value to ArrayList

I tried to made an app to get result from gmaps API.

I want to use array list to save the result from JSONObject The log from result_name.toString() return the correct array, but the log from result_phone.toString() is empty.

I'm confused because the res value is correct. checkEmptyJsonResponse is a class that i created to check whether or not the value for international_phone_number is empty JSON response. It returns either the JSON Value or "Not Available"

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

In this case, if i put Log to see the value of res, it return the correct value.

It seems that something wrong in result_phone.add(res) but i don't know what

What did i do wrong?


Here is the part of my code that i use

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

It's because you call result_phone.add(res); from second ASYNC request (it just launched, but will be complete later, not now), so when Log.d("RESULTZZZZZZ - ", result_phone.toString()); called in first request's callback - there are no results yet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM