【发布时间】:2019-06-17 21:44:03
【问题描述】:
我正在使用 volley 库从服务器获取响应
我希望当来自 volley 的响应收到然后函数返回存在 boolean 值时,我已经尝试了几乎所有堆栈溢出方法,如等待/通知等,但它们停止整个执行并且在那之后没有收到 volley 响应
boolean exsitance= false;
public boolean checkUserExist(final String id, final String email)
{
try {
RequestQueue requestQueue = Volley.newRequestQueue(context);
JSONObject jsonBody = new JSONObject();
if (id != null)
jsonBody.put("id", id);
if (email != null)
jsonBody.put("email", email);
final String mRequestBody = jsonBody.toString();
StringRequest stringRequest = new StringRequest(Request.Method.POST, UrlConstants.checkUserExsist, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i("LOG_RESPONSE", response);
exsitance = true;
Toast.makeText(context, "user exsist", Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
try {
if (error.networkResponse.statusCode == 400) {
Toast.makeText(context, "user not exsist" + error.networkResponse.statusCode, Toast.LENGTH_SHORT).show();
exsitance = false;
}
} catch (Exception e) {
}
}
}) {
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
@Override
public byte[] getBody() throws AuthFailureError {
try {
return mRequestBody == null ? null : mRequestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", mRequestBody, "utf-8");
return null;
}
}
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
String responseString = "";
if (response != null) {
responseString = String.valueOf(response.statusCode);
Log.d("statuscode_volley", responseString);
}
return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
}
};
requestQueue.add(stringRequest);
} catch (JSONException e) {
e.printStackTrace();
}
return exsitance;
}
请帮助我。提前致谢
【问题讨论】:
标签: android android-volley delay