4

I have made a post request to my friend's server. In my logcat I am getting volley error message as null and no response body or network response code.

I have used the VolleySingleton pattern and made the right type of requests for my operation. Testing the app from postman, all seems to be working fine but running it on a physical device, I am not able to find any message in logs.

Here's my code:

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);
    }

}

Also, here is my post 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);

Pls what is the problem? Thanks

Anand Rajput
  • 469
  • 1
  • 8
  • 21
  • Show you log info . – KeLiuyue Oct 11 '17 at 01:08
  • @KeLiuyue, log cat shows - Failed with error message null – Anand Rajput Oct 11 '17 at 01:14
  • I did not see it . I only saw `Log.d(TAG, "Response Code is:\t" + respCode);` in your code . – KeLiuyue Oct 11 '17 at 01:17
  • It's in onErrorListener, and that too the response code not displayed in logcat. Pls check again – Anand Rajput Oct 11 '17 at 01:18
  • Like this in your code .`08-20 20:48:03.606: E/LOGIN-ERROR(6656): null 08-20 20:48:03.606: E/LOGIN-ERROR(6656): com.android.volley.ServerError 08-20 20:48:03.606: E/LOGIN-ERROR(6656): at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:145) 08-20 20:48:03.606: E/LOGIN-ERROR(6656): at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:105)` – KeLiuyue Oct 11 '17 at 01:21
  • And you can show like this info .`08-20 20:48:03.606: E/LOGIN-ERROR(6656): null` – KeLiuyue Oct 11 '17 at 01:23

1 Answers1

5

Try this .

@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();
    }
}
KeLiuyue
  • 8,149
  • 4
  • 25
  • 42