当前位置: 动力学知识库 > 问答 > 编程问答 >

android - Volley ExecutionException

问题描述:

i am trying to implement login using volley like below:

public static JSONObject register(RequestQueue requestQueue, String url, HashMap<String, String> params) {

RequestFuture<JSONObject> requestFuture = RequestFuture.newFuture();

CustomRequest customRequest = new CustomRequest(Request.Method.POST,url,params,requestFuture,requestFuture);

JSONObject response = null;

requestQueue.add(customRequest);

try {

response = requestFuture.get(30000, TimeUnit.MILLISECONDS);

} catch (InterruptedException e) {

L.m(e + "");

} catch (ExecutionException e) {

L.m(e + "");

} catch (TimeoutException e) {

L.m(e + "");

}

return response;

}

But whenever i send request i get null value as response. the web api call works, it creates the user but i can't get json result in case of an error like duplicate username etc.

parameters are :

url: http://localhost:13480/api/Account/Register

Hashmap: (as json):

{

"Email": "[email protected]",

"Password": "Password12!",

"ConfirmPassword": "Password12!"

}

and the custom request is here:

Volley - POST/GET parameters

So i can't figure out what is wrong. Any helps?

网友答案:

Dont use return response; cause you are retrurning the responce before it is getted, cause its done asynchronously. You should get the response in the VolleyResponce look at this code:

The fact is that volley does the request asynchronously so your function will end its execution before the request is ended thats why the response you are getting is null.

If you want to do some generic methods to do your request see my code please:

package controlerObjects;

import android.content.Context;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.infoplusplus.anix.currentapp.R;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by hp pc on 28/04/2015.
 */
public class BridgeToServer {
    //All this portion of code starting from <THIS>
    private Context context;
    String serverURL;

    public BridgeToServer(Context context) {
        this.context = context;
        serverURL = context.getResources().getString(R.string.serverURL);
    }

    private void genericPOSTRequest(Map<String, String> parameters, Response.Listener<String> responseListener, Response.ErrorListener errorListener) {
        GenericRequest gen = new GenericRequest();
        gen.makePOSTRequest(parameters, responseListener, errorListener);
    }

    private void genericGETRequest(Map<String, String> parameters, Response.Listener<String> responseListener, Response.ErrorListener errorListener) {
        GenericRequest gen = new GenericRequest();
        gen.makeGETRequest(parameters, responseListener, errorListener);
    }

    private class GenericRequest {

        private void makePOSTRequest(final Map<String, String> parameters, Response.Listener<String> responseListener, Response.ErrorListener errorListener) {
            RequestQueue queue = Volley.newRequestQueue(context);
            StringRequest stringRequest = new StringRequest(Request.Method.POST, serverURL,
                    responseListener, errorListener) {
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    return parameters;
                }
            };
            queue.add(stringRequest);
        }

        private void makeGETRequest(final Map<String, String> parameters, Response.Listener<String> responseListener, Response.ErrorListener errorListener) {
            RequestQueue queue = Volley.newRequestQueue(context);
            StringRequest stringRequest = new StringRequest(Request.Method.GET, serverURL,
                    responseListener, errorListener) {
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    return parameters;
                }
            };
            queue.add(stringRequest);
        }
    }
    //to </THIS> are the major calls to communicate with the server.

    private static String INSIDER = "INSIDER";
    private static String TAG = "TAG";
    private static String ID = "facebook_id";
    private static String NAME = "name";
    private static String FIRST_NAME = "first_name";
    private static String LAST_NAME = "last_name";
    private static String MIDDLE_NAME = "middle_name";
    private static String LINK_URI = "link_uri";
    private static String PICTURE_URI = "picture_uri";
    private static String DEVICE_MODEL = "device_model";
    private static String DEVICE_MANUFACTURER = "device_manufacturer";
    private static String ACCESS_TOKEN = "access_token";
    private static String QUESTION_NUMBER = "question_number";

    public void heartbeat(Response.Listener<String> responseListener, Response.ErrorListener errorListener) {
        Map<String, String> parameters = new HashMap<>();
        String VALUE = "heartbeat";
        parameters.put(TAG, VALUE);
        genericPOSTRequest(parameters, responseListener, errorListener);
    }

}

Volley - POST/GET parameters

网友答案:

Try using this instead

JsonObjectRequest jr = new JsonObjectRequest(Request.Method.POST, url, params, new Response.Listener<JSONObject>() {
    @Override
    public void onResponse(JSONObject response) {
      // handle the response here 
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {

    }
});

    requestQueue.add(jr);
分享给朋友:
您可能感兴趣的文章:
随机阅读: