Dara Sok
Dara Sok

Reputation: 59

Android volley show Unexpected response code 500

I use method POST with Volley library. All it work fine but not display toast "Successful".
Below is code that I achieve update content with my server is successful.

String uri = "http://192.168.0.103:3000/api/SampleParticipant";
StringRequest request = new StringRequest(Request.Method.POST, uri,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {

                            Toast.makeText(getApplicationContext(), "Successful" , Toast.LENGTH_SHORT).show();
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Toast.makeText(getApplicationContext(), "Error: " + error, Toast.LENGTH_SHORT).show();
                        }
                    })
            {//Body
                @Override
                protected Map<String, String> getParams() {
                    Map<String, String> params = new HashMap<String, String>();
                    params.put("participantId", editText2.getText().toString());
                    params.put("name", editText3.getText().toString());
                    return params;
                }
            };
            // Create Volley
            RequestQueue queue = Volley.newRequestQueue(this);
            queue.add(request);


But in android studio show like this.

D/Volley: [1124] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] http://192.168.0.103:3000/api/SampleParticipant 0x85adf657 NORMAL 1> [lifetime=5381], [size=704], [rc=500], [retryCount=1]
E/Volley: [1124] BasicNetwork.performRequest: Unexpected response code 500 for http://192.168.0.103:3000/api/SampleParticipant
D/EGL_emulation: eglMakeCurrent: 0xa3f85420: ver 2 0 (tinfo 0xa3f83350)
D/EGL_emulation: eglMakeCurrent: 0xa3f85420: ver 2 0 (tinfo 0xa3f83350)

Upvotes: 0

Views: 6579

Answers (2)

Isra Macfly
Isra Macfly

Reputation: 1

E/Volley: [1124] BasicNetwork.performRequest: Unexpected response code 500 for http://192.168.0.103:3000/api/SampleParticipant.

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();

?>

Upvotes: 0

SaravInfern
SaravInfern

Reputation: 3388

It is an error from server side, nothing to do with volley

https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500

You can handle 500 error like this

@Override
public void onResponse(Response<YourModel> response) {
    if (response.code() == 200) {
       // Do awesome stuff
    } else if(response.code() == 500){
       Toast.makeText(this, "Error: internal server error", Toast.LENGTH_SHORT).show();
    }
}

Upvotes: 4

Related Questions