Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

android - How to make a Volley class a public method

I have to call web service in multiple classes I am using Volley. so what should I do in onresponse method to trigger the other class for the response? I want to make syncData as a generic function for my all classes.

  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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Create a interface VolleyResultCallBack

public interface VolleyResultCallBack {

void onVolleyResultListener(String response, String requestUrl);

void onVolleyErrorListener(VolleyError error);

}

make activity implement this interface ,

your method will be like

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

.

In your activity make request like this

  YourClassName.syncData(this,url,this);

you will get responce in onVolleyResultListener method;

 @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
      }
    }

handle error in onVolleyErrorListener

@Override
public void onVolleyErrorListener(VolleyError error) {

 //do something with error ,may be show a toast

 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...