Answer the question
In order to leave comments, you need to log in
How to write a method to work with API?
I can't write a method to work with Yandex API. To work with the API, I use the Volley library . Now I have this:
GeoCoder.java
public class GeoCoder {
public static void getLocations(String part, final VolleyCallback callback) {
StringBuilder stringbuilder = new StringBuilder("http://geocode-maps.yandex.ru/1.x/?geocode=");
stringbuilder.append(part);
stringbuilder.append((new StringBuilder("&lang=ru")));
stringbuilder.append("&format=json&kind=street&results=10&&ll=76.9120338,43.2520096");
String url = stringbuilder.toString();
Log.e("URL", url);
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONObject("response").getJSONObject("GeoObjectCollection").getJSONArray("featureMember");
ArrayList<Address> arrayList = new ArrayList<Address>(jsonArray.length());
for (int i = 0; i < jsonArray.length(); i++) {
Address address = new Address();
address.setAddressLine(jsonArray.getJSONObject(i).getJSONObject("GeoObject").getJSONObject("metaDataProperty").getJSONObject("GeocoderMetaData").getJSONObject("AddressDetails").getJSONObject("Country").getString("AddressLine"));
address.setCountry(jsonArray.getJSONObject(i).getJSONObject("GeoObject").getJSONObject("metaDataProperty").getJSONObject("GeocoderMetaData").getJSONObject("AddressDetails").getJSONObject("Country").getString("CountryName"));
arrayList.add(address);
}
callback.onSuccess(arrayList);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Handle error
}
});
VolleyApplication.getInstance().getRequestQueue().add(jsObjRequest);
}
public interface VolleyCallback {
void onSuccess(ArrayList<Address> result);
}
}
ArrayList<Address>addresses;
GeoCoder.getLocations(address,new GeoCoder.VolleyCallback() {
@Override
public void onSuccess(ArrayList<Address>result) {
addresses= result;
}
});
ArrayList<Address>results = GeoCoder.getLocations(address) ;
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question