java HttpResponse 和 BufferedReader

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10809731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:41:06  来源:igfitidea点击:

HttpResponse and BufferedReader

javaandroidhttpresponsebufferedreaderhttp-get

提问by user1425108

I am trying to read the buffer (android application) and set the value to my TextView'httpStuff'. But i dont think i am getting some response from the URI.

我正在尝试读取缓冲区(android 应用程序)并将值设置为我的TextView'httpStuff'. 但我认为我没有从 URI 得到一些响应。

I don't get any runtime errors. I tried many flavour of the same logic. Nothing seems to be working.

我没有收到任何运行时错误。我尝试了许多相同逻辑的味道。似乎没有任何工作。

INTERNETpermission is already set in the manifest. SdkVersion="15". Any help ?

INTERNET权限已在清单中设置。SdkVersion="15". 有什么帮助吗?

HttpClient client = new DefaultHttpClient();
URI website = new URI("http://www.mybringback.com");
HttpGet request = new HttpGet();
request.setURI(website);
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity(); 
InputStream is = entity.getContent(); 
BufferedReader in = new BufferedReader(new InputStreamReader(is));

httpStuf.setText( in.readLine());

回答by UVM

I think you are missing the while loop and also, when you say only in.readLine(), may be it is returning you an empty line from the response, though it is having enough data.So make sure to read the reader entirely like this and check its contents.

我认为你错过了 while 循环,而且,当你说 only 时in.readLine(),它可能会从响应中返回一个空行,尽管它有足够的数据。所以一定要完全像这样阅读读者并检查其内容.

while ((line = rd.readLine()) != null) {
    httpStuf.setText(line+"\r\n");
}

Hope this will help you.

希望这会帮助你。

回答by R4j

This code worked for me

这段代码对我有用

  InputStream is = response.getEntity().getContent();
  String strResponse = inputStreamToString(is);


private String inputStreamToString(InputStream is)
{

    String line = "";
    StringBuilder total = new StringBuilder();
    // Wrap a BufferedReader around the InputStream
    BufferedReader rd = new BufferedReader(new InputStreamReader(is), 1024 * 4);
    // Read response until the end
    try
    {

        while ((line = rd.readLine()) != null)
        {
            total.append(line);
        }
    } catch (IOException e)
    {
        Log.e(TAG, "error build string" + e.getMessage());
    }
    // Return full string
    return total.toString();
}

回答by ZAQ

I am using this method to simply catch the HTTP response and it works fine for me.

我正在使用这种方法来简单地捕获 HTTP 响应,它对我来说很好用。

public String httpGetResponse(String url) {
        try {
            Log.i("HTTP Request", "httpGet Request for : " + url);
            DefaultHttpClient client = new DefaultHttpClient();
            HttpGet get = new HttpGet(url);
            //get.setHeader("Connection", "keep-alive");

            HttpResponse response = client.execute(get);

            InputStream is = response.getEntity().getContent();
            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(is));
            StringBuilder str = new StringBuilder();

            String line = null;

            while ((line = bufferedReader.readLine()) != null) {
                str.append(line + "\n");
            }
            return str.toString();
        } catch (Exception e) {
            Log.e("HTTP error", "Error in function httpGetResponse : "
                    + e.getMessage());
            return null;
        }
    }

回答by Zaz Gmy

try to get the status code of responseand Then you can compare with the (HTTP status)

尝试获取responseand的状态码然后你可以与(HTTP status)进行比较

int responseCode=response.getStatusLine().getStatusCode()

int responseCode=response.getStatusLine().getStatusCode()