我已经向我朋友的服务器发了一个帖子请求。在我的logcat中,我收到齐射错误消息为null,没有响应正文或网络响应代码。
我使用了VolleySingleton模式并为我的操作提出了正确的请求类型。从邮递员测试应用程序,似乎一切正常,但在物理设备上运行,我无法在日志中找到任何消息。
这是我的代码:
public class VolleySingleton {
public RequestQueue requestQueue;
public Context appContext;
public static VolleySingleton instanceVolley;
public VolleySingleton(Context mContext) {
this.appContext = mContext;
this.requestQueue = getRequestQueue();
}
public static synchronized VolleySingleton getVolleySingleton(Context context){
if (instanceVolley == null){
return new VolleySingleton(context);
}
return instanceVolley;
}
public RequestQueue getRequestQueue(){
if (requestQueue == null){
requestQueue = Volley.newRequestQueue(appContext.getApplicationContext());
}
return requestQueue;
}
public <T> void addToRequestQueue(Request<T> request){
getRequestQueue().add(request);
}
}
另外,这是我的帖子请求:
JSONObject userObject = new JSONObject();
JSONObject paramsObject = new JSONObject();
try {
paramsObject.put("name", fullName);
paramsObject.put("phone", mobile_No);
paramsObject.put("gender", Gender);
paramsObject.put("dob", unixTime);
userObject.put("user", paramsObject);
} catch (JSONException e) {
e.printStackTrace();
}
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, Constants.TING_SIGNUP_ENDPOINT, userObject, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, "Response is:\t " + response.toString());
Snackbar.make(findViewById(android.R.id.content), R.string.sign_up_msg, Snackbar.LENGTH_LONG).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent signUpIntent = new Intent(SignUpActivity.this, LoginActivity.class);
signUpIntent.putExtra("fullNameKey", fullName);
startActivity(signUpIntent);
}
}, 3000);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "Failed with error msg:\t" + error.getMessage());
Log.d(TAG, "Error StackTrace: \t" + error.getStackTrace());
if (error.getMessage() == null){
createUser();
}
}
}) {
@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
int respCode = response.statusCode;
Log.d(TAG, "Response Code is:\t" + respCode);
return super.parseNetworkResponse(response);
}
};
jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(60000, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
VolleySingleton.getVolleySingleton(this).addToRequestQueue(jsonObjectRequest);
请问有什么问题?感谢
答案 0 :(得分:2)
试试这个。
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "Failed with error msg:\t" + error.getMessage());
Log.d(TAG, "Error StackTrace: \t" + error.getStackTrace());
// edited here
try {
byte[] htmlBodyBytes = error.networkResponse.data;
Log.e(TAG, new String(htmlBodyBytes), error);
} catch (NullPointerException e) {
e.printStackTrace();
}
if (error.getMessage() == null){
createUser();
}
}