Android获取网站内容/源代码

时间:2016-03-26 08:10:22

标签: android

我正在进入需要获取网站源代码的项目。所以我从 Mark B 回答:How to get the html-source of a page from a html link in android?

获取了我的代码

问题是 HttpClient,HttpGet,HttpResponse 现已弃用。

因此我将其更改为 HttpURLConnection ,但没有运气,它会强制关闭。

    URL url = new URL("http://google.com");
    HttpURLConnection response = (HttpURLConnection) url.openConnection();

    String html = "";
    InputStream in = response.getInputStream();
    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();
    return html;

这有什么问题? 这是我的stacktrace

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.listview_load_data_from_json/com.kaleidosstudio.listview_load_data_from_json.MainActivity}: android.os.NetworkOnMainThreadException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2319)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2370)
        at android.app.ActivityThread.access$800(ActivityThread.java:155)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5426)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
        at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.os.NetworkOnMainThreadException
        at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1156)
        at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
        at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
        at java.net.InetAddress.getAllByName(InetAddress.java:214)
        at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
        at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
        at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
        at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
        at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
        at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
        at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
        at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
        at com.kaleidosstudio.listview_load_data_from_json.GetMP3.getJSON(GetMP3.java:49)
        at com.kaleidosstudio.listview_load_data_from_json.GetMP3.GetMP3_9Cloud(GetMP3.java:34)
        at com.kaleidosstudio.listview_load_data_from_json.MainActivity.onCreate(MainActivity.java:52)
        at android.app.Activity.performCreate(Activity.java:5296)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2283)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2370)
        at android.app.ActivityThread.access$800(ActivityThread.java:155)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5426)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
        at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
        at dalvik.system.NativeStart.main(Native Method)

4 个答案:

答案 0 :(得分:0)

您正在主线程中调用这些函数。现在,他们有两种可能的方法来解决这个问题。

  1. AsyncTask :AsyncTask可以正确,轻松地使用UI线程。此类允许执行后台操作并在UI线程上发布结果,而无需操作线程和/或处理程序。以下是对AsyncTask

    的引用
    public class myAsyncTask extends AsyncTask<String, Void, Void> {
    public String html;
    
    @Override
    private void doInBackground(String... params) {
    URL url = new URL(params[0]);
    HttpURLConnection response = (HttpURLConnection) url.openConnection();
    
    String html = "";
    InputStream in = response.getInputStream();
    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();
    }
    }
    

    然后像这样调用它

     myAsyncTask task= new myAsyncTask();
     task.execute("http://google.com");
    
  2. 线程:线程是并发执行单元。它有自己的调用堆栈,用于调用方法,它们的参数和局部变量。每个应用程序在启动时至少有一个线程运行,主线程在主ThreadGroup中运行。运行时在系统线程组中保留自己的线程。

  3. 有两种方法可以在新线程中执行代码。您既可以继承Thread并重写其run()方法,也可以构造一个新的Thread并将Runnable传递给构造函数。在任何一种情况下,都必须调用start()方法来实际执行新的Thread。

    每个线程都有一个整数优先级,它会影响操作系统调度线程的方式。新线程继承其父级的优先级。可以使用setPriority(int)方法设置线程的优先级。这是一个快速参考Thread

答案 1 :(得分:0)

你得到NetworkOnMainThreadException。这就是说你不能在主UI线程上发送Http请求。你必须在后台线程中做所有这些。

以下是一个如何操作的简单示例:

public class WebsiteDataWorker extends AsyncTask<String, Void, Void> {
    public String html;

    @Override
    private void doInBackground(String... params) {
        URL url = params[0];
        HttpURLConnection response = (HttpURLConnection) url.openConnection();

        String html = "";
        InputStream in = response.getInputStream();
        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();
    }
}

现在你可以通过说

来调用你的后台主题
WebsiteDataWorker worker = new WebsiteDataWorker();
worker.execute("http://google.com");
String html = worker.html;
//do whatever stuff you want with the html variable

答案 2 :(得分:0)

private class RegistrationAsyncTask extends AsyncTask<Void, Void, String> {
ProgressDialog dialog;

@Override
protected void onPreExecute() {
    dialog = new ProgressDialog(MainActivity.this);
    dialog.setTitle("Registration");
    dialog.setMessage("Registration in process...");
    dialog.setCancelable(false);
    dialog.show();
}

@Override
protected String doInBackground(Void... params) {
    try {

        response = HttpClientWrapper.getResponseGET("Your url put here");
        Log.e(TAG, "Response: " + response);
    } catch (Exception e) {
        e.printStackTrace();
        this.error = e.getMessage();
    }
    return response;
}

@Override
protected void onPostExecute(String result) {
    Log.e(TAG, "result: " + response);

    if (dialog.isShowing()) {
        dialog.dismiss();
    }
}}

使用此异步任务

答案 3 :(得分:-1)

public static String getResponseGET(String url) {
String response = "";
HttpURLConnection c = null;

try {
    URL u = new URL(url);
    c = (HttpURLConnection) u.openConnection();
    c.setRequestMethod("GET");
    c.setConnectTimeout(15000);
    c.setReadTimeout(15000);

    c.connect();
    int status = c.getResponseCode();

    switch (status) {
        case 200:
        case 201:
            BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line+"\n");
                response = sb.toString().substring(0, sb.toString().length() - 1);
            }
            br.close();
            return response;
    }

} catch (IOException ex) {
    if (c != null) {
        c.disconnect();
    }
} finally {
    if (c != null) {
        try {
            c.disconnect();
        } catch (Exception ex) {

        }
    }
}
return null;}

将此http调用用于GET方法