简体   繁体   中英

Error posting JSONObject using Volley android studio

I am sending a JSONObject by using Volley but I get : value <br of type java.lang.string cannot be converted to JSONObject

anyone knows where is the problem?!

here is my java code :

final JSONObject jsonObject = new JSONObject();
    try {
        jsonObject.put("siteName", "example.com");
        jsonObject.put("field", "android");
    } catch (JSONException e) {
        e.printStackTrace();
    }

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            JsonObjectRequest objectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {

                    try {
                        textView.setText(response.getString("websiteInfo"));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                    Toast.makeText(JsonActivity.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
                }
            });

            Volley.newRequestQueue(JsonActivity.this).add(objectRequest);
        }
    });

and here is my php code :

    <?php 
$json = file_get_contents('php://input');

$obj = json_decode($json);

$website = $obj->{'siteName'};

$field = $obj->{'field'};

$output ='';

$output->websiteInfo = $website.$field;

$jsonObject=json_encode($output);

echo $jsonObject;
?>

You are passing params as a JSONObject.. You should override getParams method and pass ur params..

For your reference check the blog : Android Volley Tutorial – Making HTTP GET, POST, PUT

This is parsing error. Make sure your server is returning something like this

{ "websiteInfo":"this is a website for health" }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM