Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

java - Android - Wait for volley response to return

I need execute a Volley request and wait for the response to parse it and return it, but have no idea on how to achieve this. Can someone help?

What I have now is this:

public String getLink() {
    JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                shortenURL = response.getString("url");
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                // TODO Auto-generated method stub

           }
    });

    queue.add(jsObjRequest);

    return shortenURL;
}
See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

A reusable Factory method design pattern solution:

To return or get Volley response from another method, you need to write a Callback functionality, which is all easy using Interfaces

This simple solution is taken from my MVC architecture for Android apps developed with complete reusability and separation of concern concept.

Supposing JSONObject is your response from server

Step 1)

Create an Interface ServerCallback

package xx.xx.xx.utils;

    import org.json.JSONObject;

    public interface ServerCallback{
        void onSuccess(JSONObject result);
    }

Step 2) Supposing yours Volley server request method is in Controller or any other shared 'context' class do this in yours any Activity

Controller controller = new Controller();
controller.youFunctionForVolleyRequest(email, password, this, loginUrl, new ServerCallback() {
                    @Override
                    public void onSuccess(JSONObject response) {
                       // do stuff here 
                    }
                  }
               );

3) In your Controller class, call ServerCallback function in inResponse() which will execute yours code in Activity only on the response from the server- mission accomplished!

 public void youFunctionForVolleyRequest(final String email , final String password ,final Context context , final String URL, final ServerCallback callback)
    {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("email", email);
        params.put("password", password);

        Log.e("sending json",params.toString());
        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
                URL, new JSONObject(params), new Response.Listener<JSONObject>()
        {

            @Override
            public void onResponse(JSONObject response) {
               callback.onSuccess(response); // call call back function here

            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                //VolleyLog.d("Volley error json object ", "Error: " + error.getMessage());

            }
        }){
            @Override
            public String getBodyContentType()
            {
                return "application/json";
            }
        };

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(jsonObjReq);

    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...