com.android.volley.ParseError: org.json.JSONException: Value Invalid of type java.lang.String cannot be converted to JSONObject

37 views Asked by At

This is my activity.java.

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, URL, null, new Response.Listener < JSONObject > () {
    @Override
    public void onResponse(JSONObject response) {
        Log.d("volleytracker", response.toString());
        try {
            String message = response.getString("message");
            boolean successful = response.getBoolean("result");
            String userId = response.getString("user_id");
            if (successful) {
                //save userId on shared memory
                PrefManager prefManager = new PrefManager(context);
                prefManager.SaveUSerID(Integer.valueOf(userId));
                // intent NextActivity
                Intent intent = new Intent(Register.this, VerificationPage.class);
                startActivity(intent);
            } else {
                status.setText(message);
                status.setTextColor(Color.parseColor("#FF0000"));
            }

        } catch (JSONException e) {
            Log.d("checkError", e.toString());
            e.printStackTrace();
        }
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Toast.makeText(Register.this, error.toString().trim(), Toast.LENGTH_SHORT).show();
        Log.d("errorcheck", error.toString());
        regBtn.setEnabled(true);
    }
}) {
    public byte[] getBody() {
        JSONObject jsonParams = new JSONObject();
        try {
            jsonParams.put("name", name);
            jsonParams.put("PhoneNumber", phone);
            jsonParams.put("deviceId", deviceId);
            //jsonParams.put("DeviceIP", getiP);
            //Log.d("jsonPara",jsonParams.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonParams.toString().getBytes();
    }

    @Override
    public String getBodyContentType() {
        return "application/json";
    }


    @Override
    public Map < String, String > getHeaders() throws AuthFailureError {
        Map < String, String > headers = new HashMap < > ();
        return headers;
    }
};
RequestQueue requestQueue = Volley.newRequestQueue(Register.this);
requestQueue.add(jsonObjectRequest);

I need to send data using volley library in body. when I send data using postman it works. I am send data from body. So now when I run this it will given com.android.volley.ParseError: org.json.JSONException: Value Invalid of type java.lang.String cannot be converted to JSONObject this error. How to fix that error?

0

There are 0 answers