Al Hennessey
Al Hennessey

Reputation: 2445

Error when using Volley for POST and JSONObject Response

i am trying to post some data from my android application to my web server and then send a value response back, but i keep getting this error

Error: org.json.JSONException: Value SQLSTATE of type java.lang.String cannot be converted to JSONObject

Here is the android code thats relevant

                JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
                        url, null,
                        new Response.Listener<JSONObject>() {

                            @Override
                            public void onResponse(JSONObject response) {
                                Log.d(TAG, response.toString());

                            }
                        }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.d(TAG, "Error: " + error.getMessage());

                    }
                }) {

                    @Override
                    protected Map<String, String> getParams() {
                        Map<String, String> params = new HashMap<String, String>();
                        params.put("name", regName);
                        params.put("email", regEmail);
                        params.put("password", regPassword1);

                        return params;
                    }

                };
                AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);

Here is the relevant php code

    <?php


$iname = $_POST['name'];
$iemail = $_POST['email'];
$ipassword = $_POST['password'];

try {


$list = array();
$x = 1;
$name = "Epic App";
$image = "http://www.example.com/feed/img/c123.jpg";
$profilePic = "http://www.example.com/feed/img/nat.jpg";
$timeStamp = "1403375851930";

while($x<=10){
    $list[] = array('id' => $x, 'name' => $name, 'image' => $image, 'profilePic' => $profilePic, 'timeStamp' => $timeStamp);
    $x++;
}
echo json_encode(array('feed' => $list));

exit;

}
catch(PDOException $e) {
    echo $e->getMessage();
    exit();
}

?>

There is more code in that php but i took it out as i dont think its relevant, just querys etc...

Thanks for the help

Upvotes: 0

Views: 883

Answers (1)

AngelInKate
AngelInKate

Reputation: 46

If using JsonObjectRequest, you do provide param as a JSONObject instead of getParams(...)

below i.e,

JSONObject o = new JSONObject();
o.put("name", regName);
o.put("email", regEmail); 
o.put("password", regPassword1); 
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST, url, o,.. );

Upvotes: 1

Related Questions