【问题标题】:Getting a return value from volley onResponse从 volley onResponse 获取返回值
【发布时间】:2016-06-15 15:15:08
【问题描述】:

我正在尝试制作我的第一个真正的程序,所以请放轻松。 这部分使用 Google API 和 volley 将输入的邮政编码转换为 lat/lng 坐标,但我不知道如何让 volley 函数“返回”坐标。根据我的阅读,我需要实现一些回调方法,但我不知道该怎么做。

    //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

【问题讨论】:

  • new Response.Listener&lt;String&gt;() 回调

标签: java android json android-volley


【解决方案1】:

将您的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
}

【讨论】:

    【解决方案2】:

    我想你想做这样的事情:

    这将是你的回调类:(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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-04
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 2018-01-01
      • 1970-01-01
      相关资源
      最近更新 更多