HttpGet 无法识别 url

发布于 2024-11-02 15:18:14 字数 865 浏览 0 评论 0原文

因此,我使用另一篇旧文章中的以下代码,但在其中一个部分遇到问题,行: HttpGet request = new HttpGet(url); 不起作用。在 url 位置,我放置了类似 www.stackoverflow.com 的内容,但这一部分不会让代码编译。我基本上是想从 html 网站中提取文本。完整代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet(www.stackoverflow.com);
    HttpResponse response = client.execute(request);

    String html = "Toronto-GTA";
    InputStream in = response.getEntity().getContent();
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    StringBuilder str = new StringBuilder();
    String line = null;
    while((line = reader.readLine()) != null)
    {
        str.append(line);
    }
    in.close();
    html = str.toString();
}

So I'm using the code below from a different older post, but having trouble with one part, the line for: HttpGet request = new HttpGet(url); doesn't work. In the url spot I put something like www.stackoverflow.com, but that one part won't let the code compile. I'm basically trying to pull text writing from an html website. The complete code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet(www.stackoverflow.com);
    HttpResponse response = client.execute(request);

    String html = "Toronto-GTA";
    InputStream in = response.getEntity().getContent();
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    StringBuilder str = new StringBuilder();
    String line = null;
    while((line = reader.readLine()) != null)
    {
        str.append(line);
    }
    in.close();
    html = str.toString();
}

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

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

发布评论

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

评论(4

王权女流氓 2024-11-09 15:18:14

HTTPGet 需要 URL 或字符串,因此请尝试将您的请求行更改为:

HttpGet request = new HttpGet("http://www.stackoverflow.com/");

HTTPGet expects an URL or a string, so try to change your request line into:

HttpGet request = new HttpGet("http://www.stackoverflow.com/");
三寸金莲 2024-11-09 15:18:14

使用以下形式的字符串:

[scheme:][//authority][path][?query][#fragment]

"http://www.stackoverflow.com"

Use a string of the form:

[scheme:][//authority][path][?query][#fragment]

i.e. "http://www.stackoverflow.com"

秋千易 2024-11-09 15:18:14

试试这个:HttpGet request = new HttpGet("www.stackoverflow.com");

Try this: HttpGet request = new HttpGet("www.stackoverflow.com");

我早已燃尽 2024-11-09 15:18:14

添加到上面给出的答案中,用 try 和 catch 语句包围您的代码以捕获异常。

try{
HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet("www.stackoverflow.com");
    HttpResponse response = client.execute(request);
    InputStream in = response.getEntity().getContent();
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    StringBuilder str = new StringBuilder();
    String line, html = null;
    while((line = reader.readLine()) != null)
    {
        str.append(line);
    }
    in.close();
    html = str.toString();
}
catch(Exception e){
//Do something here like printing the stacktrace
}

Adding to above given answers surround your code with try and catch statements to catch the exceptions.

try{
HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet("www.stackoverflow.com");
    HttpResponse response = client.execute(request);
    InputStream in = response.getEntity().getContent();
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    StringBuilder str = new StringBuilder();
    String line, html = null;
    while((line = reader.readLine()) != null)
    {
        str.append(line);
    }
    in.close();
    html = str.toString();
}
catch(Exception e){
//Do something here like printing the stacktrace
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文