Agung
Agung

Reputation: 49

Internal server error while connecting to Server in android using volley?

here my function code to post using volley

    StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_CHECK_IN,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        int status = jsonObject.getInt("status");

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError response) {

                }
            }) {

        @Override
        public String getBodyContentType() {
            return "application/json; charset=utf-8";
        }

        @Override
        public byte[] getBody() throws AuthFailureError {
            try {
                return requestBody == null ? null : requestBody.getBytes("utf-8");
            } catch (UnsupportedEncodingException uee) {
                VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", requestBody, "utf-8");
                return null;
            }
        }

        @Override
        public Map<String, String> getHeaders () {
            Map<String, String> map = new HashMap<String, String>();
            map.put("Content-Type", "application/json");
            map.put("appid", appids);
            map.put("timestamp", timestamps);
            map.put("token", tokens);
            map.put("signature", signatures);
            return map;
        }
    };
}

I don't know what's wrong with my code, because of 2 days ago everything fine. and when I tried to debug, error show like this

BasicNetwork.performRequest: Unexpected response code 500 for http://api/presence/check_in

can anyone help me, please? because I'm stuck and need help or reference to solve my error thank you

Upvotes: 0

Views: 298

Answers (1)

ravi
ravi

Reputation: 1001

HTTP code 500 is Internal Server Error. Read more here. It generally implies that server is not able to process the request and come up with a response. This means that the code for your application might be alright whereas the server might be encountering some issue processing the current request body. I see that you are sending String in your request body. One peculiar thing I noticed with sending String in request body is that, we also need to check if the String is null or not, better to to use .trim() method at the end of your string too, which will delete starting and trailing spaces. Something simple like not escaping single quotes ( ' ) for the field you are trying to insert onto the database at your server might cause this. So server side field validation and best practices like Prepared Statements is also crucial. If you are absolutely sure that your client end [android app] is alright, maybe the server is encountering some issue at the endpoint you are hitting. Test your api with a rest client like POSTMAN or INSOMNIA to be absolutely sure that your server and api layer is working as intended. Good Luck

Upvotes: 2

Related Questions