【问题标题】:How to make a Volley class a public method如何使 Volley 类成为公共方法
【发布时间】:2018-05-20 08:12:40
【问题描述】:

我必须在我使用 Volley 的多个课程中调用 web 服务。那么我应该在 onresponse 方法中做什么来触发另一个类的响应? 我想让 syncData 作为我所有类的通用函数。

  public String syncData(Context mContext, String url) {
    try {
        RequestQueue queue = Volley.newRequestQueue(mContext);

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    jsonResponse=response.toString();//what I should do here to trigger another class that responds achieved

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                try {
                    Log.e("Response", "error");
                    //  updateForecastUI(isCentigradeType);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(
                Constants.MY_SOCKET_TIMEOUT_MS,
                Constants.MAX_RETRY,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        jsonObjectRequest.setShouldCache(false);
        queue.add(jsonObjectRequest);

    } catch (Exception e) {
        e.printStackTrace();
    }
    return  jsonResponse;
}

【问题讨论】:

    标签: android android-volley


    【解决方案1】:

    创建接口VolleyResultCallBack

    public interface VolleyResultCallBack {
    
    void onVolleyResultListener(String response, String requestUrl);
    
    void onVolleyErrorListener(VolleyError error);
    
    }
    

    让activity实现这个接口,

    你的方法会像

     public static void syncData(Context mContext, String url,VolleyResultCallBack resultCallBack) {
    try {
        RequestQueue queue = Volley.newRequestQueue(mContext);
    
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    jsonResponse=response.toString();//what I should do here to trigger another class that responds achieved
                   resultCallBack.onVolleyResultListener(jsonResponse,url);
    
                } catch (Exception e) {
                    e.printStackTrace();
                }
    
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                try {
                    Log.e("Response", "error");
                    resultCallBack.onVolleyErrorListener(error);
                    //  updateForecastUI(isCentigradeType);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    
        jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(
                Constants.MY_SOCKET_TIMEOUT_MS,
                Constants.MAX_RETRY,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        jsonObjectRequest.setShouldCache(false);
        queue.add(jsonObjectRequest);
    
    } catch (Exception e) {
        e.printStackTrace();
    }
    return  jsonResponse;
    }
    

    .

    在您的活动中提出这样的请求

      YourClassName.syncData(this,url,this);
    

    你会在 onVolleyResultListener 方法中得到响应;

     @Override
     public void onVolleyResultListener(String response, String requestUrl) {
    
          if(requestUrl.contains(url){  //  required to check because there may be multiple requests in same activity 
    
            //do something with responce
          }
        }
    

    处理 onVolleyErrorListener 中的错误

    @Override
    public void onVolleyErrorListener(VolleyError error) {
    
     //do something with error ,may be show a toast
    
     }
    

    【讨论】:

      【解决方案2】:

      简单,将Volley用作Singleton

      这样您就可以在多个类中访问 Web 服务。

      你的工作会完成的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-09-27
        • 2017-11-23
        • 2020-03-27
        • 2011-01-30
        • 2015-12-17
        • 2018-06-03
        相关资源
        最近更新 更多