我在这一行设置断点:
doc = Jsoup.connect(params[0]).get();
但出于某种原因,当我尝试从这一行开始跳过时,它会跳到
行} catch (IOException e) {
e.printStackTrace();
并抛出异常(因此永远不会获得标题的值),我不确定原因。
有什么建议吗?
package com.example.test;
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView tv;
String url = "http://sheriff.org/apps/arrest/results.cfm?lname=ABBOTT&fname=LAUREA";
String tr;
Document doc;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.TextView01);
new MyTask().execute(url);
}
private class MyTask extends AsyncTask<String, Void, String> {
ProgressDialog prog;
String title = "";
@Override
protected void onPreExecute() {
prog = new ProgressDialog(MainActivity.this);
prog.setMessage("Loading....");
prog.show();
}
@Override
protected String doInBackground(String... params) {
try {
doc = Jsoup.connect(params[0]).get();
Element tableElement = doc.select(".datagrid").first();
Elements tableRows = tableElement.select("tr");
for (Element row : tableRows) {
Elements cells = row.select("td");
if (cells.size() > 0) {
title = cells.get(0).text() + "; "
+ cells.get(1).text() + "; "
+ cells.get(2).text() + "; "
+ cells.get(3).text();
}
}
} catch (IOException e) {
e.printStackTrace();
}
return title;
}
@Override
protected void onPostExecute(String title) {
super.onPostExecute(title);
prog.dismiss();
tv.setText(title);
}
}
}
logcat的:
10-07 15:42:21.591: I/Adreno200-EGLSUB(7981): <ConfigWindowMatch:2165>: Format RGBA_8888.
10-07 15:42:21.611: D/memalloc(7981): ion: Mapped buffer base:0x5cbaa000 size:122880 offset:0 fd:60
10-07 15:42:21.611: E/(7981): Can't open file for reading
10-07 15:42:21.611: E/(7981): Can't open file for reading
10-07 15:42:21.621: I/Adreno200-EGLSUB(7981): <ConfigWindowMatch:2165>: Format RGBA_8888.
10-07 15:42:21.641: D/memalloc(7981): ion: Mapped buffer base:0x5ccd6000 size:614400 offset:0 fd:64
10-07 15:42:21.681: D/memalloc(7981): ion: Mapped buffer base:0x5cf02000 size:122880 offset:0 fd:67
10-07 15:42:21.721: D/memalloc(7981): ion: Mapped buffer base:0x5d166000 size:614400 offset:0 fd:70
10-07 15:42:21.771: D/memalloc(7981): ion: Mapped buffer base:0x5d2fc000 size:122880 offset:0 fd:74
10-07 15:42:22.091: W/System.err(7981): java.net.UnknownHostException: Unable to resolve host "sheriff.org": No address associated with hostname
10-07 15:42:22.281: W/System.err(7981): at java.net.InetAddress.lookupHostByName(InetAddress.java:463)
10-07 15:42:22.281: W/System.err(7981): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:279)
10-07 15:42:22.281: W/System.err(7981): at java.net.InetAddress.getAllByName(InetAddress.java:253)
10-07 15:42:22.281: W/System.err(7981): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
10-07 15:42:22.281: W/System.err(7981): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
10-07 15:42:22.281: W/System.err(7981): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
10-07 15:42:22.281: W/System.err(7981): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
10-07 15:42:22.291: W/System.err(7981): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
10-07 15:42:22.291: W/System.err(7981): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
10-07 15:42:22.291: W/System.err(7981): at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
10-07 15:42:22.291: W/System.err(7981): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
10-07 15:42:22.291: W/System.err(7981): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
10-07 15:42:22.291: W/System.err(7981): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
10-07 15:42:22.291: W/System.err(7981): at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:425)
10-07 15:42:22.291: W/System.err(7981): at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:410)
10-07 15:42:22.291: W/System.err(7981): at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:164)
10-07 15:42:22.291: W/System.err(7981): at org.jsoup.helper.HttpConnection.get(HttpConnection.java:153)
10-07 15:42:22.291: W/System.err(7981): at com.example.test.MainActivity$MyTask.doInBackground(MainActivity.java:46)
10-07 15:42:22.291: W/System.err(7981): at com.example.test.MainActivity$MyTask.doInBackground(MainActivity.java:1)
10-07 15:42:22.291: W/System.err(7981): at android.os.AsyncTask$2.call(AsyncTask.java:264)
10-07 15:42:22.291: W/System.err(7981): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
10-07 15:42:22.301: W/System.err(7981): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-07 15:42:22.301: W/System.err(7981): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
10-07 15:42:22.301: W/System.err(7981): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
10-07 15:42:22.301: W/System.err(7981): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
10-07 15:42:22.301: W/System.err(7981): at java.lang.Thread.run(Thread.java:856)
10-07 15:42:22.301: W/System.err(7981): Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
10-07 15:42:22.301: W/System.err(7981): at libcore.io.Posix.getaddrinfo(Native Method)
10-07 15:42:22.301: W/System.err(7981): at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:55)
10-07 15:42:22.301: W/System.err(7981): at java.net.InetAddress.lookupHostByName(InetAddress.java:448)
10-07 15:42:22.301: W/System.err(7981): ... 25 more
10-07 15:42:22.311: D/memalloc(7981): ion: Unmapping buffer base:0x5cbaa000 size:122880
10-07 15:42:22.311: D/memalloc(7981): ion: Unmapping buffer base:0x5cf02000 size:122880
10-07 15:42:22.311: D/memalloc(7981): ion: Unmapping buffer base:0x5d2fc000 size:122880
10-07 15:42:22.311: D/memalloc(7981): ion: Mapped buffer base:0x5ca92000 size:614400 offset:0 fd:54
答案 0 :(得分:1)
我错过了我的清单中的互联网权限,导致无法访问该网址。