K
K
kamadi2015-02-05 12:09:36
Java
kamadi, 2015-02-05 12:09:36

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

Using GeoCoder.java
ArrayList<Address>addresses;
        GeoCoder.getLocations(address,new GeoCoder.VolleyCallback() {
            @Override
            public void onSuccess(ArrayList<Address>result) {
                addresses= result;
            }
        });

How to make it so:
ArrayList<Address>results = GeoCoder.getLocations(address) ;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
one pavel, 2015-02-05
@onepavel

Here it is written how to make a synchronous request in Volley

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question