How to deal with unexpected response 500 in Android
Asked Answered
C

2

0

I am trying to retrieve a JSON from an external API using Volley. Here is my code

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);
    final TextView mTextView = findViewById(R.id.testreq);
    RequestQueue queue = Volley.newRequestQueue(this);
    String url = "https://horoscope-free-api.herokuapp.com/?time=today&sign=cancer";
    StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    // Display the first 500 characters of the response string.
                    mTextView.setText("Response is: "+ response.substring(0,500));
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            mTextView.setText("That didn't work");
            Log.d("Error", error.toString());
        }
    });
    queue.add(stringRequest);
}

It seems like the response is done but I am getting this error

E/Volley: [277] BasicNetwork.performRequest: Unexpected response code 500 and when I ran a test on the API in https://apitester.com/ It tells me its passed and I get this error

Response Headers HTTP/1.1 500 Internal Server Error Connection: keep-alive Date: Sat, 10 Nov 2018 19:56:18 GMT Server: Apache Transfer-Encoding: chunked Content-Type: application/json Via: 1.1 vegur

Any ideas how to solve this? Is it the API or is it me?

Condon answered 10/11, 2018 at 20:1 Comment(7)
@PushpeshKumarRajwanshi I do understand that status 500 means thats server error, and thats why I am asking if its me not handling the request properly or no. I wasnt going to ask if I didnt check my code already and couldnt figure it out. If theres nothing you can suggest why bother writing at all?Condon
I just tried accessing the URL you mentioned in your post and I am getting success response. So your problem hasn't reproduced to me. Can you try and check yourself again? May be server had some temporary problems, which got resolved. And now I can surely say, you don't have to fix anything at your end. Initially I thought may be you didn't pass some parameter or request body properly which might have resulted into this error, but that's not the case here as its a simple GET URL which needs no special headers/cookies/parameters. Sorry for the confusion and any inconvenience caused.Anzus
That's the thing, the URL works well in browser, however when it comes to getting a response it fails with this codeCondon
I just tried from code as well and I got the response successfully even from code. So seems like there is some silly mistake at your end only.Anzus
Can you post your code?Condon
Added my codes as answer. Not sure if you will be able to use on android. So lemme quickly do it using only java lib support.Anzus
stackoverflow.com/…Sinister
S
1

HTTP Status Codes starting with 5 inform that the error is on server side. The code 500 is interpreted as Internal Server Error, to solve this you have to check what might cause it. It may be caused by a mistake in the code in that case you can open your error_log to see the error and act accordingly.

It can be caused by server features being unavailable momently like accessing the database or having many simultaneous opened connections that exceed the associated mysql resources.

Some other times, the error is not logged into the error_log file. If you use a cpanel, at the homepage, under Metrics tab open Errors and check according to the time you requested to the server. If you are not using cpanel look for a corresponding server log.

With the link passed in your question that error code should not be unless it's overwritten or hardcoded in your server side script.

See these three tests:

enter image description here

With this, I set both time and cancer parameters, and as you can see, the Error Code is 500 but within the response body, every thing is okay with status 200.

enter image description here

And with this, I still have the answer even if I just set one parameter. within the response body, there is an error not server error but customized error: there is a parameter missing. Guess what's the returned HTTP code(500)

And what if I remove all parameters?

enter image description here

Every thing is okay. The HTTP Status Code and response body. That tells me that wether the author wants you to listen to customized request status rather than the returned HTTP Status Code. That's just my point of view.

Stable answered 10/11, 2018 at 21:0 Comment(0)
S
0

I found a Bug on API you are using is sending response code 500 + Response Data. And in volley whenever response code other then 200 to 299 is detected volley run there Error Listeners to handle it. That's why your code missed your Response data because Volley Error Listener get Call First.

Postman Picture mark with both Points

Frankly saying this is Nonstandard Poor Way of API Designing, which throwing status code on Responses as well.

Too things i want to tell you :

  1. Start using Postman (If you are not using it)

  2. Jamun Volley Library

start using helper libraries this is what i recommend you to Use which handle your most methods call and provide you better error handling with custom messages and individual status codes.

Hope this will help you to tackle future approaches.

Stonedeaf answered 13/11, 2018 at 18:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.