Java HttpGet无法识别url
因此,我使用的代码来自另一篇旧文章,但有一部分有问题:Java HttpGet无法识别url,java,android,html,syntax,apache-commons-httpclient,Java,Android,Html,Syntax,Apache Commons Httpclient,因此,我使用的代码来自另一篇旧文章,但有一部分有问题:HttpGet-request=newhttpget(url)不起作用。在url位置,我放置了类似于www.stackoverflow.com,但这一部分不允许代码编译。我基本上是在尝试从html网站中提取文本。完整代码: public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.
HttpGet-request=newhttpget(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();
}
试试这个:HttpGet request=newhttpget(“www.stackoverflow.com”)代码>试试这个:HttpGet请求=newhttpget(“www.stackoverflow.com”)代码>需要一个或一个字符串,因此请尝试将您的请求行更改为:
HttpGet request = new HttpGet("http://www.stackoverflow.com/");
应为或字符串,因此请尝试将请求行更改为:
HttpGet request = new HttpGet("http://www.stackoverflow.com/");
使用以下格式的字符串:
[scheme:][//authority][path][?query][#fragment]
i、 e.”http://www.stackoverflow.com“
使用以下格式的字符串:
[scheme:][//authority][path][?query][#fragment]
i、 e.”http://www.stackoverflow.com“
在上述给定答案的基础上添加try-and-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
}
除了上面给出的答案之外,您的代码周围还有try-and-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
}
我尝试将其更改为url具有“”,但这会导致出现诸如“HttpResponse response=client.execute(request);”、“InputStream in=response.getEntity().getContent();”等行,以及其他几行错误。“error out”是指编译错误还是运行时异常?如果是异常,请发布它。编译错误,所有错误:未处理的异常类型IOException,eclipse中的大多数快速修复建议“使用try/catch进行修复”,我尝试更改它以使url具有“”,但这会导致出现以下行:“HttpResponse response=client.execute(request);”,“InputStream in=response.getEntity().getContent();”,还有更多的错误。错误是指编译错误还是运行时异常?如果是异常,请发布它。编译错误,所有这些错误:未处理的异常类型IOException,eclipse中的大多数快速修复建议HttpGet请求=新HttpGet(www.stackoverflow.com)使用try/catch“Surroned”;我把它改为(“www.stackoverflow.com”);正如许多人所建议的,但其他行,如“HttpResponse response=client.execute(request);”返回编译错误,所有这些错误:未处理的异常类型IOException,eclipse中的大多数快速修复建议对HttpGet request=new HttpGet(www.stackoverflow.com)使用try/catch进行“Surroned”;我把它改为(“www.stackoverflow.com”);正如许多人所建议的,但其他行,如“HttpResponse response=client.execute(request);”返回编译错误,所有这些错误:未处理的异常类型IOException,eclipse中的大多数快速修复建议“使用try/catch进行处理”