HttpGet 无法识别 url
因此,我使用另一篇旧文章中的以下代码,但在其中一个部分遇到问题,行: 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
HTTPGet 需要 URL 或字符串,因此请尝试将您的请求行更改为:
HTTPGet expects an URL or a string, so try to change your request line into:
使用以下形式的字符串:
即
"http://www.stackoverflow.com"
Use a string of the form:
i.e.
"http://www.stackoverflow.com"
试试这个:
HttpGet request = new HttpGet("www.stackoverflow.com");
Try this:
HttpGet request = new HttpGet("www.stackoverflow.com");
添加到上面给出的答案中,用 try 和 catch 语句包围您的代码以捕获异常。
Adding to above given answers surround your code with try and catch statements to catch the exceptions.