Unexpected response code 500 for POST method
Asked Answered
A

3

12

I am doing an update on the old project & I don't have much knowledge of Android as of now. In project we have Comments section on product.

For comment after sending earlier we had return as 0 (some error) & 1 (success).

Below is the code we were using.

final JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
    Method.POST,
    act.getString(R.string.CommentForUserURL),
    null, new Response.Listener<JSONObject>() {

    @Override
    public void onResponse(
            JSONObject response) {

        Log.d("response done", "done===" + response);

        mloading.setVisibility(View.GONE);
        if (response != null) {
            Comment obj = new Comment();
            JSONObject jsonObject = response;
            try {
                obj.setComment(jsonObject
                        .getString("Comment"));

Now we have changed the return object from 0/1 to user object.

Does this need need to update JsonObjectRequest to GJSON request? Or object will also get parsed with JsonObjectRequest?

I am asking because when I execute above, I get error as below.

01-25 12:30:21.754: E/Volley(16487): [10114] BasicNetwork.performRequest: 
Unexpected response code 500 for 
http://new.souqalharim.com/add/CommentForMerchant

Any idea why I am getting this error?

Note: This URL is working fine for iPhone application.


Edit 1

This is post method, so full url is not there. There are few more parameters to add like ?comment=MyComment&userId=123&productId=234. As it is post I am not adding parameters in actual url.

I have those in another methods

@Override
protected Map<String, String> getParams()
        throws AuthFailureError {
    Map<String, String> params = new HashMap<String, String>();
    params.put("productId", productId.toString());
    params.put("userId",
            mSessionManager.getUserCode().toString());
    params.put("comment", GlobalFunctions
            .EncodeParameter(med_comments
                    .getText().toString()));



    return params;
}

Full url is as below.

http://new.souqalharim.com/add/CommentForUser?productId=325&userId=5&comment=abcd

I tested this in Mozilla RESTClient and it works fine.


Edit 2

After checking further I found protected Map<String, String> getParams() throws AuthFailureError { is not getting called. Any idea why this is happening?

Alfilaria answered 25/1, 2015 at 9:32 Comment(4)
http://new.souqalharim.com/add/CommentForMerchant is not valid url make sure you are using righ urlFeoffee
@Alfilaria well, having 500 as response code, has nothing to do with response value 0 or 1, it's something wrong went on server while processing your request, so it could be some something in your request, some params were changed, or sent incorrectly.Theoretical
@Alfilaria i have just notice that, the url you posted is GET and you are using POST in JsonObjectRequest also opending this link now in browser shows 404 error.Theoretical
@Theoretical : nope, url is POST. I posted full url for you all to know what params I am passing...Alfilaria
G
9

The problem is below.

final JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
    Method.POST,
    act.getString(R.string.CommentForUserURL),
    null, new Response.Listener<JSONObject>() {
    ^^^^

It should be

final JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
    Method.POST,
    act.getString(R.string.CommentForUserURL),
    new JSONObject(params), new Response.Listener<JSONObject>() {
    ^^^^^^^^^^^^^^^^^^^^^^

Copy code from protected Map<String, String> getParams() before final JsonObjectRequest.

That's it!!!

Reason is as below.

The JsonObjectRequest is extended JsonRequest which override getBody() method directly, so your getParam() would never invoke, I recommend you extend StringRequest instead of JsonObjectRequest.

your can check this answer for more details.

Gossett answered 25/1, 2015 at 12:8 Comment(1)
i have same issue #37481728Buffet
F
5

Actually 500 means Internal server error and this is caused by the Rest-api you are calling and not due to Volley,so check the back-end code.

Foresee answered 4/4, 2018 at 13:3 Comment(1)
It's not Internal server error it's an unexpected error which means error happens in client level.Sholokhov
W
0

BasicNetwork.performRequest: Unexpected response code 500

The server receives the correct response, but detects an error, this error is due to the PHP connection file with the database, this code must have the function implemented to hide the values ​​of the query to avoid sql insertion

use this sample code:

<?php

 $con = new mysqli('localhost', 'u648230499_wololo', '*********', 'u648230899_ejexample');

 if ($con->connect_error) {

    echo 'error connect database: ', $con->connect_error;

    exit();
}

$participantId = $_POST['participantId'];

$name = $_POST['name'];

$sql = $con->prepare('INSERT INTO participant VALUES (?,?)');

$sql->bind_param('is', $participantId, $name);

$sql->execute();

echo 'OK\n';

$con->close();

?>

Woodrow answered 19/10, 2022 at 16:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.