0

I have successfully send String Request but while trying with JSON Object request it show String cannot be converted to JSONObject.

java.lang.String cannot be converted to JSONObject

Java

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        _response.setText(response.toString());

                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        _response.setText(error.getMessage().toString());
                        System.out.println(error.getMessage().toString());

                    }
                }) {
            //adding parameters to the request
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("name", _name.getText().toString());
                params.put("email", _email.getText().toString());
                return params;
            }
        };
        MySingleton.getInstance(getApplicationContext()).addToRequestQueue(jsonObjectRequest);

Php

<?php
class User {
    public $name = "";
    public $email  = "";
}

$user = new User();
$user->name = trim($_POST['name']);
$user->email  = trim($_POST['email']);

json_encode($user);
?>
Tahsin
  • 9
  • 5
  • 2
    You missed to `echo` the JSON string? `echo json_encode($user);`. – Syscall May 18 '18 at 08:47
  • Sorry, but it is unclear what you actually do. Please take the time to revise your question. Try to think from our point of view, we know nothing about what you try to do... – arkascha May 18 '18 at 08:50
  • After writing the echo getting the same problem. – Tahsin May 18 '18 at 08:53

0 Answers0