Can I use php to throw server error with message?

徘徊边缘 提交于 2020-03-04 07:19:44

问题


I throw a error at server.

header('HTTP/1.1 500 Internal Server Error');
echo 'this is error message';
exit();

At the client side, I use android's Volley to send the request and handle the error in onErrorResponse

new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Toast.makeText(getApplicationContext(),error.getMessage(),Toast.LENGTH_LONG).show();
    }
}

But I cannot found any message inside the error variable, how can I send a message to client with error?


回答1:


IMO, you can try parse error message if available by using the following:

            @Override
            protected VolleyError parseNetworkError(VolleyError volleyError) {
                String json;
                if (volleyError.networkResponse != null && volleyError.networkResponse.data != null) {
                    try {
                        json = new String(volleyError.networkResponse.data,
                                HttpHeaderParser.parseCharset(volleyError.networkResponse.headers));
                    } catch (UnsupportedEncodingException e) {
                        return new VolleyError(e.getMessage());
                    }
                    return new VolleyError(json);
                }
                return volleyError;
            }

Hope it helps!



来源:https://stackoverflow.com/questions/34280023/can-i-use-php-to-throw-server-error-with-message

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!