获取错误java.lang.String无法转换为JSONObject

时间:2018-05-18 08:46:55

标签: php android json

我已成功发送字符串请求,但在尝试使用JSON Object请求时,显示String无法转换为JSONObject。

java.lang.String无法转换为JSONObject

  

爪哇

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
class User {
    public $name = "";
    public $email  = "";
}

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

json_encode($user);
?>

0 个答案:

没有答案