如何使Volley课成为一种公共方法

时间:2017-12-06 12:34:02

标签: android android-volley

我必须在我使用排球的多个班级中拨打网络服务。那么我应该如何在 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;
}

2 个答案:

答案 0 :(得分:1)

创建界面VolleyResultCallBack

public interface VolleyResultCallBack {

void onVolleyResultListener(String response, String requestUrl);

void onVolleyErrorListener(VolleyError error);

}

make 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

 }

答案 1 :(得分:0)

简单,使用排球作为 Singleton

这样您就可以在多个类中点击Web服务。

你的工作将完成。