我只是下载最新版本的jsoup(1.7.1)并按照官方代码(更改了网址).然后我得到"http错误提取网址"
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
loadData();
} catch (IOException e) {
Log.i("error",e.getMessage());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void loadData() throws IOException {
Document doc = Jsoup.connect("http://forum.mtr.com.hk/search.php?station=30&cat=&x=25&y=2").get();
String title = doc.title();
Log.i("title",title);
}}
Run Code Online (Sandbox Code Playgroud)
我的代码有什么问题?似乎错误只是在Android项目中发生,因为我在Java项目中做同样的事情,工作正常.
注意: - 我已经添加了Internet权限
我遇到了类似的问题.可能是你的设备连接太慢而且connect()函数太快超时,或者它可能因某些无害的HTTP错误状态而失败.此外,我的Jsoup连接适用于大多数页面,但对于一些我得到"HTTP错误提取URL.状态= 307".事实证明,这实际上是从Web服务器到另一个URL的重定向请求.我用以下代码解决了所有这些问题:
Connection.Response res = Jsoup.connect(sUrl).
timeout(5000).ignoreHttpErrors(true).followRedirects(true).execute();
if (res.statusCode() == 307) {
String sNewUrl = res.header("Location");
if (sNewUrl != null && sNewUrl.length() > 7)
sUrl = sNewUrl;
res = Jsoup.connect(sUrl).
timeout(5000).execute();
}
Document doc = res.parse();
Run Code Online (Sandbox Code Playgroud)
希望这有助于,或至少激发您在调用get()或execute()之前尝试更多设置.
格雷格
| 归档时间: |
|
| 查看次数: |
5923 次 |
| 最近记录: |