user8989957:
i tried to made an apps 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
result_name = new ArrayList ();
ArrayListresult_address = new ArrayList ();
ArrayListresult_placeid = new ArrayList ();
ArrayListresult_phone = new ArrayList ();
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; iJSONObject 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; kLog.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();
}
});
}
Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered
HERE