Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
342 views
in Technique[技术] by (71.8m points)

java - How to deal with unexpected response 500 in Android

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...