如何使用HttpClient

发布于 2024-10-22 01:07:00 字数 453 浏览 2 评论 0原文

我目前正在尝试使用 java 中的以下代码通过“uri”获取一些数据:

HttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet(uri);
        HttpResponse response = client.execute(get);
        HttpEntity entity = response.getEntity();
        if(entity != null){
            InputStream stream = entity.getContent();
            callString = stream.toString();
            return callString;
        }

但这不起作用。有谁知道我在这里做错了什么?

I'm currently trying to get some data via a 'uri' using the following code in java:

HttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet(uri);
        HttpResponse response = client.execute(get);
        HttpEntity entity = response.getEntity();
        if(entity != null){
            InputStream stream = entity.getContent();
            callString = stream.toString();
            return callString;
        }

However this isn't working. Does anyone know what I'm doing wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

落日海湾 2024-10-29 01:07:00

您不能像那样打印出输入流...相反,请执行以下操作:-

    HttpClient client = new DefaultHttpClient();
    HttpGet get = new HttpGet("http://ichart.finance.yahoo.com/table.csv?s=MSFT");
    HttpResponse response = client.execute(get);
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        Scanner scanner = new Scanner(entity.getContent());
        while (scanner.hasNextLine()) {
            System.out.println(scanner.nextLine());
        }
    }

打印输出如下所示:-

1994-02-02,84.75,85.50,84.00,84.00,40924800,2.09
1994-02-01,85.00,85.75,84.50,85.12,44003200,2.12
1994-01-31,85.25,85.87,84.75,85.12,62566400,2.12
1994-01-28,84.50,85.50,84.25,84.87,41875200,2.11
1994-01-27,84.00,84.75,83.25,84.25,51129600,2.10
1994-01-26,85.00,85.00,84.00,84.25,50489600,2.10
1994-01-25,85.25,85.37,84.00,85.12,70361600,2.12
...

You cannot print out the input stream like that... instead, do something like this:-

    HttpClient client = new DefaultHttpClient();
    HttpGet get = new HttpGet("http://ichart.finance.yahoo.com/table.csv?s=MSFT");
    HttpResponse response = client.execute(get);
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        Scanner scanner = new Scanner(entity.getContent());
        while (scanner.hasNextLine()) {
            System.out.println(scanner.nextLine());
        }
    }

The printout looks like this:-

1994-02-02,84.75,85.50,84.00,84.00,40924800,2.09
1994-02-01,85.00,85.75,84.50,85.12,44003200,2.12
1994-01-31,85.25,85.87,84.75,85.12,62566400,2.12
1994-01-28,84.50,85.50,84.25,84.87,41875200,2.11
1994-01-27,84.00,84.75,83.25,84.25,51129600,2.10
1994-01-26,85.00,85.00,84.00,84.25,50489600,2.10
1994-01-25,85.25,85.37,84.00,85.12,70361600,2.12
...
救赎№ 2024-10-29 01:07:00

这完全是猜测,但不应该是:

String uri = "ichart.finance.yahoo.com/table.csv?s=MSFT"

HttpData data = HttpRequest.get(uri);
System.out.println(data.content);

Its a total guess but shouldn't it be:

String uri = "ichart.finance.yahoo.com/table.csv?s=MSFT"

HttpData data = HttpRequest.get(uri);
System.out.println(data.content);
金橙橙 2024-10-29 01:07:00

您正在尝试下载文件,并且 getEntity 用于获取您指定类型的对象。恕我直言,这行不通。

您需要编写能够实际读取响应流并从中读取内容的代码...

您想做什么?

you are trying to download a file and getEntity is used get an object for a type you have specified. IMHO this wont work.

You need to code which will actually read the response stream and read contents out of it...

What are you trying to do ?

只是一片海 2024-10-29 01:07:00

要将生成的实体读取为字符串,请使用 EntityUtils.toString(HttpEntity)EntityUtils.toString(HttpEntity, String) 如果您知道字符集。

To read the resulting entity to a String use EntityUtils.toString(HttpEntity) or EntityUtils.toString(HttpEntity, String) if you know the character set.

小猫一只 2024-10-29 01:07:00

如果您收到 NetworkOnMainThreadException 那么这意味着您正在调用 client.execute(get);在主线程上,这是在 Honeycomb 及更高版本上抛出的异常。请参阅http://developer.android.com/reference/android/os/NetworkOnMainThreadException。 html 了解详细信息。解决方案是在新线程中运行它。

If you are getting NetworkOnMainThreadException then that means you are calling client.execute(get); on the main thread which is an exception thrown on Honeycomb and higher. See http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html for details. The solution is to run this in a new thread.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文