如何处理Android中的意外响应500 [英] How to deal with unexpected response 500 in Android

查看:259
本文介绍了如何处理Android中的意外响应500的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Volley从外部API检索JSON. 这是我的代码

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:意外的响应代码500 当我在 https://apitester.com/中对API进行测试时,它告诉我它已通过收到此错误

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

响应标题 HTTP/1.1 500内部服务器错误 连接:保持活动状态 日期:2018年11月10日,星期六19:56:18 GMT 伺服器:Apache 传输编码:分块 内容类型:application/json 通过:1.1 vegur

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

任何想法如何解决这个问题?是API还是我?

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

推荐答案

5 开头的HTTP状态代码表明该错误位于服务器端.代码 500 被解释为内部服务器错误,要解决此问题,您必须检查可能引起该错误的原因.在这种情况下,可能是由于代码错误引起的,您可以打开error_log查看错误并采取相应措施.

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.

这可能是由于服务器功能暂时不可用(例如访问数据库)或同时打开的多个连接超过了相关的mysql资源而导致的.

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.

有时,该错误未登录到error_log文件中.如果使用面板,请在主页的指标标签下打开错误,然后根据向服务器请求的时间进行检查.如果您不使用cpanel,请查找相应的服务器日志.

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.

请参阅以下三个测试:

这样,我既设置了时间参数,又设置了癌症参数,如您所见,错误代码为500,但在响应体内,状态为200时一切正常.

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.

有了这个,即使我只设置了一个参数,我仍然有答案.在响应正文中,有一个错误不是服务器错误,而是自定义错误:缺少参数.猜猜返回的HTTP代码是什么(500)

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?

一切正常. HTTP状态代码和响应正文. 这告诉我,作者是否希望您监听自定义的请求status而不是返回的HTTP状态代码.那只是我的观点.

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.

这篇关于如何处理Android中的意外响应500的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆