从volley onResponse获取返回值

时间:2016-06-15 15:15:08

标签: java android json android-volley

我正在努力制作我的第一个真正的节目,所以对我来说很容易。 这部分使用Google API和凌空将输入的邮政编码转换为lat / lng坐标,但我无法弄清楚如何让凌空函数“返回”坐标。根据我的阅读,我需要实现一些回调方法,但我无法弄清楚如何做到这一点。

    //Converts values in TextView to String
    locationurl = address.getText().toString();

    //RequestQueue Variable
    RequestQueue mRequestQueue;

    // Instantiate the cache and set up the network to use HttpURLConnection as the HTTP client.
    Cache cache = new DiskBasedCache(getCacheDir(), 1024 * 1024); // 1MB cap
    BasicNetwork network = new BasicNetwork(new HurlStack());

    //Instantiate the RequestQueue with the cache and network
    mRequestQueue = new RequestQueue(cache, network);

    //Start Queue
    mRequestQueue.start();

    String addressurl = "http://maps.googleapis.com/maps/api/geocode/json?address=" + location;

    //Formulate the request and handle the response
    StringRequest address = new StringRequest(Request.Method.GET, addressurl,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    coordinates = parseLocation(response);
                    if((coordinates == null && coordinates.isEmpty())||coordinates.equals("NOT_VALID_ADDRESS")) {
                        showAlert("Please Enter A Valid Zip Code");
                    } else {

                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    showAlert("Error."); //Error code display
                    //TODO: fix error handling
        }
    });
    mRequestQueue.add(address); //Add address to ResponseQueue

2 个答案:

答案 0 :(得分:0)

onResponse方法更改为:

@Override
public void onResponse(String response) {
    coordinates = parseLocation(response);
    if((coordinates == null && coordinates.isEmpty())||coordinates.equals("NOT_VALID_ADDRESS")) {
        showAlert("Please Enter A Valid Zip Code");
    } else {
        useCoordinates(coordinates);
    }
}

然后实现使用坐标的方法:

public void useCoordinates(Location coordinates) {
    // Use the value
}

答案 1 :(得分:0)

我想你想做这样的事情:

这将是你的回调类:(NetworkResponse.java)

public abstract class NetworkResponse<T> implements Response.Listener<T>, Response.ErrorListener {


    @Override
    public void onResponse(T t) {
        String coordinates = parseLocation(t);
        onCoordinatesReceived(coordinates;
    }

    @Override
    public void onErrorResponse(VolleyError volleyError) {
        onCoordinatesReceived("");
        }

    public String parseLocation(T s) {
        return null;
        //Implement parsing here.
    }

    public abstract void onCoordinatesReceived(String coordinates) ;


}

在现有课程中进行以下更改:

StringRequest address = new StringRequest(Request.Method.GET, addressurl,
        new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                coordinates = parseLocation(response);
                if((coordinates == null && coordinates.isEmpty())||coordinates.equals("NOT_VALID_ADDRESS")) {
                    showAlert("Please Enter A Valid Zip Code");
                } else {

                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                showAlert("Error."); //Error code display
                //TODO: fix error handling
    }
});
mRequestQueue.add(address); //Add address to ResponseQueue

--- ---到

  NetworkResponse<String> networkResponse =
                      new NetworkResponse<>() {
                          @Override
                          public void onCoordinatesReceived(String coordinates){
//Here are your coordinates.
                        if((coordinates == null && coordinates.isEmpty())||coordinates.equals("NOT_VALID_ADDRESS")) {
                        showAlert("Please Enter A Valid Zip Code");
                      else {
                        //Your implementation here.
                           }
}
        StringRequest address = new StringRequest(Request.Method.GET, addressurl,
                networkResponse, networkResponse);
        mRequestQueue.add(address); //Add address to ResponseQueue
  

请注意:此处的回调课程中有NetworkResponse。