我正在尝试在Android应用中获取网站(其来源)的内容。
URL url = new URL("http://verbmaps.com/");
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "Mozilla/5.0");
InputStream is = con.getInputStream();
现在运行时会出现很多错误。
(我已经说过这是一个js片段,所以我可以隐藏它)
/*
W/System.err: android.os.NetworkOnMainThreadException
W/System.err: at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
W/System.err: at java.net.InetAddress.lookupHostByName(InetAddress.java:431)
W/System.err: at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
W/System.err: at java.net.InetAddress.getAllByName(InetAddress.java:215)
W/System.err: at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
W/System.err: at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:188)
W/System.err: at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:157)
W/System.err: at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:100)
W/System.err: at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:357)
W/System.err: at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:340)
W/System.err: at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330)
W/System.err: at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:433)
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:384)
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:231)
W/System.err: at com.example.yeeverbs.MainActivity$override.onCreate(MainActivity.java:41)
W/System.err: at com.example.yeeverbs.MainActivity$override.access$dispatch(MainActivity.java)
W/System.err: at com.example.yeeverbs.MainActivity.onCreate(MainActivity.java:0)
W/System.err: at android.app.Activity.performCreate(Activity.java:6237)
W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
W/System.err: at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4077)
W/System.err: at android.app.ActivityThread.-wrap15(ActivityThread.java)
W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1350)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err: at android.os.Looper.loop(Looper.java:148)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5417)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
*/
其中一个错误指向行41
,即InputStream
的行:
InputStream is = con.getInputStream();
因此,我无法成功运行它。
我在Android Manifest文件中拥有所需的权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
我也看到有几个解决方案使用DefaultHttpClient
代替URL
。我试过了,但Android Studio告诉我它已被弃用。所以我不得不回到使用URL
,除非有任何其他更好的替代品不被弃用。
此外,我试图在没有任何外部库的情况下执行此操作,因此我不会接受JSoup
或任何其他此类库。
我使用URL
获取网站的来源。但是InputStream
给了我错误。
我想知道为什么会这样。此外,任何不被弃用的更好的替代方案或者是此任务的外部库都将受到赞赏。
答案 0 :(得分:1)
如错误所示,您正在主线程上进行网络呼叫。在android中,你应该在后台线程中进行任何此类调用。提示:scheck asyncTasks
答案 1 :(得分:0)
使用它,应该有效。
public static String getResponseFromUrl(String url) {
HttpClient httpclient = new DefaultHttpClient(); /
HttpGet httpget = new HttpGet(URL);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
sb.append(line);
String resString = sb.toString();
is.close();
return resString;
}
就像在评论中说的那样,在不同的线程中使用它,没有主线程。
答案 2 :(得分:0)
创建一个类并扩展异步任务,因为您无法在主线程上执行此操作
class webdownload extends AsyncTask<String,Void,String>{
@Override
protected String doInBackground(String... urls) {
try{
String result="";
URL url =new URL(urls[0]);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.connect();
InputStream in = connection.getInputStream();
InputStreamReader reader = new InputStreamReader(in);
int data = reader.read();
while (data!=-1){
char current = (char) data;
result += current;
data = reader.read();
}
return result;
}catch (Exception e){
e.printStackTrace();
return null;
}
}
}
现在创建一个要执行的功能
public void Scrape(View view){
String htmlcontent;
webdownload wb= new webdownload();
try {
htmlcontent = wb.execute("YOUR_WEBSITE_LINK").get();
} catch (Exception e) {
e.printStackTrace();
}
}