开发者

How to get error message when connect to server in Android

I use GET method to connect t开发者_JAVA技巧o the server, and the server responses http status code 403. When I paste the url of my GET method to browser, I'm received "some text" and http status code 403. But when I send a GET request with the same url to the server by HttpURLConnection of Java(Android), I'm just received http status code 403, and response text is null. So anyone can tell me how to get the "some text" when server return code 403. Thanks in advance.


Just do as Zoombie wrote and add line:

String reasonPhrase = httpResponse.getStatusLine().getReasonPhrase();

If it doesn't work, your server doesn't set the reason. Then you should map codes to standard reason phrases:

List of HTTP status codes


Try to use DefaultHttpClient class,

client = new DefaultHttpClient(httpParameters);

                httpResponse = client.execute(request);
                responseCode = httpResponse.getStatusLine().getStatusCode();

                HttpEntity entity = httpResponse.getEntity();

                if (entity != null) {

                    InputStream instream = entity.getContent();
                    response = convertStreamToString(instream);
                    instream.close();
                }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜