0

这是我的代码,在模拟器中代码可以正常工作并打印

<!doctype html>
    <html itemscope="" itemtype="http://schema.org/WebPage">
    <head>
    <meta content="Search the world's information, including webpages, images, videos and more. Google has many special features to help you find exactly what you're looking for." name="description">
    <meta content="noodp" name="robots"><meta content="/images/google_favicon_128.png" itemprop="image"><meta content="origin" id="mref" name="referrer"><title>Google</title>        

    <script>(function(){

但在我的手机中,该程序不幸关闭了!

所以程序在我的模拟器中正确运行并在我的手机中强制关闭。在我的清单中,我设置了互联网权限

    package com.example.untitled11;

    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.TextView;

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.ArrayList;

    public class MyActivity extends Activity {

    static URL url = null;
    static TextView textView;

    static ArrayList<String> html = new ArrayList<String> (1000)  ;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        textView = (TextView) findViewById(R.id.textView);

        try {
            url = new URL("http://google.com/");
        } catch (MalformedURLException e) {
            textView.setText("URL\n"+e);
        }
        InputStream xx = null;
        try {
            xx = url.openConnection().getInputStream();
        } catch (IOException e) {
            textView.setText("InputStream\n" + e);
        }

        BufferedReader reader = null;
        reader = new BufferedReader( new InputStreamReader( xx  ));


        String line = "";

        try {
            while( (line = reader.readLine() ) != null )  {
                html.add(line+"");
            }
        } catch (IOException e) {
            textView.setText("adding\n" + e);
        }

        try {
            reader.close();
        } catch (IOException e) {
            textView.setText("closing\n"+e);

        }

        textView.setText(html.get(0)+"");

        }
}

这是我的清单:

 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.untitled11"
      android:versionCode="1"
      android:versionName="1.0">
<uses-sdk android:minSdkVersion="10"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application android:label="@string/app_name">
    <activity android:name="MyActivity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
</application>
</manifest> 
4

2 回答 2

1

使用线程和处理程序轻松运行网络相关操作

//Handler to send commands to UI thread
Handler handler = new Handler();

Thread th = new Thread(new Runnable() {
    public void run() {

        try {
    url = new URL("http://google.com/");
} catch (MalformedURLException e) {
    textView.setText("URL\n"+e);
}
InputStream xx = null;
try {
    xx = url.openConnection().getInputStream();
} catch (IOException e) {
    textView.setText("InputStream\n" + e);
}

BufferedReader reader = null;
reader = new BufferedReader( new InputStreamReader( xx  ));


String line = "";

try {
    while( (line = reader.readLine() ) != null )  {
        html.add(line+"");
    }
} catch (IOException e) {
    textView.setText("adding\n" + e);
}

try {
    reader.close();
} catch (IOException e) {
    textView.setText("closing\n"+e);

}


    }
});
th.start();
于 2013-11-12T12:33:50.920 回答
0

使用 Jsoup 库进行 HTml 解析进行网络操作使用 asynctask 或以下代码

http://jsoup.org/

public static int SDK_INT = android.os.Build.VERSION.SDK_INT;
if (SDK_INT >= 10) {
    ThreadPolicy tp = ThreadPolicy.LAX;
    StrictMode.setThreadPolicy(tp);
}

HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(
        "yourURL");
setDefaulHeadersForGet(request);
HttpResponse response = client.execute(request);

String html = "";
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();
html = "" + Html.fromHtml(html);

Document doc = Jsoup.parse(html);
于 2013-11-12T12:46:28.130 回答