Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java android HttpURLConnection如何获取响应401_Java_Android_Httpurlconnection - Fatal编程技术网

java android HttpURLConnection如何获取响应401

java android HttpURLConnection如何获取响应401,java,android,httpurlconnection,Java,Android,Httpurlconnection,我正在制作一个android应用程序,它将使用xauth身份验证连接到web服务器。我的问题是,若服务器返回401导致java异常,那个么我就无法得到响应 我需要这种反应来知道我做错了什么 首先,HttpURLConnection适合这种类型的东西吗 第二,我怎样才能得到回应呢 URL u_rl = new URL(url); final HttpURLConnection connection = (HttpURLConnection) u_rl.openConnect

我正在制作一个android应用程序,它将使用xauth身份验证连接到web服务器。我的问题是,若服务器返回401导致java异常,那个么我就无法得到响应

我需要这种反应来知道我做错了什么

首先,HttpURLConnection适合这种类型的东西吗 第二,我怎样才能得到回应呢

URL u_rl = new URL(url);
    final HttpURLConnection connection =
        (HttpURLConnection) u_rl.openConnection();

    connection.setRequestMethod(method);

    if (authorization != null) {
        connection.setRequestProperty("Authorization", authorization);
    }
    final byte[] body = parameters.getBytes();

        connection.setRequestProperty(
            "Content-Length", Integer.toString(body.length));
        connection.setDoOutput(true);
        connection.connect();
        final OutputStream output = connection.getOutputStream();
        output.write(body);
        output.flush();
        System.out.println(connection.getResponseCode());
        if (connection.getResponseCode() != 200) {
            result.put("error", connection.getResponseCode()+"");
            return null;
        }

        String out = IOUtils.toString(connection.getInputStream());

我知道为什么我得到400或其他响应代码,问题是用户不知道为什么。服务器返回一个JSON错误,即使响应代码不是200,我也希望得到该JSON代码。除非您提供错误,否则很难判断代码中的错误。我建议您使用这个答案,这肯定会对您有所帮助

 InputStream retrieveStream(String url) {

        DefaultHttpClient client = new DefaultHttpClient();

        HttpGet getRequest = new HttpGet(url);

        try {

            HttpResponse getResponse = client.execute(getRequest);
            final int statusCode = getResponse.getStatusLine().getStatusCode();

            if (statusCode != HttpStatus.SC_OK) {
                Log.w(getClass().getSimpleName(), "Error " + statusCode
                        + " for URL " + url);
                return null;
            }

            HttpEntity getResponseEntity = getResponse.getEntity();
            return getResponseEntity.getContent();

        } catch (IOException e) {
            getRequest.abort();
            Log.w(getClass().getSimpleName(), "Error for URL " + url, e);
        }

        return null;

    }
并使用此代码将inputstream转换为字符串可读的数据格式

BufferedReader r = new BufferedReader(new InputStreamReader(retrieveStream("YOU URL")));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
    total.append(line);
}

谢谢:)

您需要发布一些代码吗?如果我下面的答案对您有所帮助,请接受我的答案并向上投票,我们将不胜感激。谢谢:)发布了代码,但我认为它与我的问题没有任何共同之处?它不能解决我的问题,因为在:if(statusCode!=HttpStatus.SC_OK){Log.w(getClass().getSimpleName(),“Error”+statusCode+“for URL”+URL);return null;}我仍然无法从服务器上获得JSON响应。请提供您点击的URL。谢谢:)