这是我的代码,在模拟器中代码可以正常工作并打印
<!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>