我正在尝试使用如下所示的凌空实现登录:
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;
}
但每当我发送请求时,我都会获得null值作为响应。 web api调用有效,它创建了用户,但是如果出现重复用户名等错误,我无法获得json结果。
参数是:
网址:http://localhost:13480/api/Account/Register Hashmap :(作为json):
{ “电子邮件”:“admin @ admin”, “密码”:“密码12!”, “ConfirmPassword”:“Password12!” }
,自定义请求在这里: Volley - POST/GET parameters 所以我无法弄清楚出了什么问题。有帮助吗?
答案 0 :(得分:0)
TL,DR。此声明return response;
错误。
您尝试在获得响应之前返回响应(异步调用)。
事实是,volley以异步方式执行请求,因此您的调用将在请求服务器之前结束其执行,这就是您获得的响应为 null 的原因。
这是我写的一个类的子集,可以帮助您理解如何正确使用 Volley :
public class SubsetClass {
private Context context;
String serverURL;
public SubsetClass(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);
}
}
}
相关问题
答案 1 :(得分:0)
尝试使用此代替
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);