我正在使用Volley API(用于完成电话号码注册过程)向我的本地服务器发送密码,如下所述
final HashMap<String, String> params = new HashMap<String, String>();
params.put("PHONE_NUMBER",1234567890);
params.put("PIN", 9999);
addRequestToQueue("http://"127.0.0.1:5000"/registerClient", params);
public void addRequestToQueue(String url, final HashMap<String, String> params)
{
JsonObjectRequest jsonRequest = new JsonObjectRequest (url, new JSONObject(params), new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) {VolleyLog.v("Response from server: %s", response.toString());}},new Response.ErrorListener() {@Override public void onErrorResponse(VolleyError error) {VolleyLog.e("Error:", error.toString());}});
mRequestQueue.add(jsonRequest);
}
这工作正常并在服务器上存储信息,但我无法从服务器获取响应代码/字符串。可能是因为异步调用......
请有人帮我解决问题,我需要从服务器获取响应字符串/代码,以便我可以验证用户是否正确注册。
答案 0 :(得分:2)
试试这个
RequestFuture<JSONObject> future = RequestFuture.newFuture();
JsonObjectRequest request = new JsonObjectRequest(Method.POST, url, new JSONObject(params), future, future)
mRequestQueue.add(request);
try {
JSONObject response = future.get();
... do something
} catch (InterruptedException e) {
} catch (ExecutionException e) {
}