我正在尝试使用我的DreamFactory API登录用户,为新会话执行POST erquest以获取令牌。但是我得到了Volley tileout错误......
这是我的代码:
public void getUserToken(View view){
final EditText editText_username = (EditText)view.findViewById(R.id.username_name_edittext);
final EditText editText_password = (EditText)view.findViewById(R.id.password_name_edittext);
RequestQueue queue = Volley.newRequestQueue(getContext());
String url = "http://my.virtual.machine/rest/user/session";
JSONObject js = new JSONObject();
try {
js.put("email", editText_username.getText().toString());
js.put("password", editText_password.getText().toString());
} catch (JSONException e) {
e.printStackTrace();
}
JsonObjectRequest userTokenRequest = new JsonObjectRequest(Request.Method.POST, url,js,
new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response) {
try {
userToken = response.getString("session_id");
userEmail = response.getString("email");
Log.d("UserToken Response", "OK => " + userToken);
getUserData();
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
Log.d("ERROR","error => "+error.toString());
}
}
) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("X-DreamFactory-Application-Name", "xxx");
return params;
}
};
queue.add(userTokenRequest);
}
当我从Hurl.it运行POST请求时,一切正常......
答案 0 :(得分:0)
发现问题,响应需要一些时间,所以通过添加延迟它可以工作! :)
int socketTimeout =5000;//5 seconds - change to what you want
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
userTokenRequest.setRetryPolicy(policy);
queue.add(userTokenRequest);