小案例
网络图片查看器
1.布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/iv_beauty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="100" />
<EditText
android:id="@+id/et_path"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="http://a.hiphotos.baidu.com/album/w%3D2048/sign=0a938b00d53f8794d3ff4f2ee6230cf4/faedab64034f78f06fe0f24b78310a55b2191c9a.jpg" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="watch"
android:text="浏览" />
</LinearLayout>
2.Activity
public class MainActivity extends Activity {
private ImageView iv_beauty;
private EditText et_path;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.iv_beauty = (ImageView) this.findViewById(R.id.iv_beauty);
this.et_path = (EditText) this.findViewById(R.id.et_path);
}
public void watch(View view) {
String path = this.et_path.getText().toString().trim();
if (TextUtils.isEmpty(path)) {
Toast.makeText(this, "路径不能为空", 0).show();
} else {
try {
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
// 设置请求方式
connection.setRequestMethod("GET");
// 设置超时时间
connection.setConnectTimeout(10000);
// connection.setRequestProperty(field, newValue)
int code = connection.getResponseCode();
if (code == 200) {
InputStream is = connection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(is);
this.iv_beauty.setImageBitmap(bitmap);
} else {
Toast.makeText(this, "获取图片失败", 0).show();
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "读取图片失败", 0).show();
}
}
}
}
3.授权
<uses-permission android:name="android.permission.INTERNET"/>
代码只能在4.0以下运行,4.0以上会抛 android.os.NetworkOnMainThreadException
从 Android 2.3 开始提供了一个新的类 StrictMode,该类可以用于捕捉发生在应用程序主线程中耗时的磁盘、网络访问或函数调用,可以帮助开发者改进程序,使主线程处理 UI 和动画在磁盘读写和网络操作时变得更平滑,避免主线程被阻塞。
网络html查看器
1.布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<EditText
android:id="@+id/et_path"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入网址" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="确定"
android:onClick="click" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</ScrollView>
</LinearLayout>
2.Activity
public class MainActivity extends Activity {
protected static final int ERROR = 0;
protected static final int SHOW_CONTENT = 1;
private EditText et_path;
private TextView tv_content;
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case ERROR:
Toast.makeText(MainActivity.this, "获取网页信息失败",
Toast.LENGTH_SHORT).show();
break;
case SHOW_CONTENT:
tv_content.setText((String) msg.obj);
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.et_path = (EditText) this.findViewById(R.id.et_path);
this.tv_content = (TextView) this.findViewById(R.id.tv_content);
}
public void click(View view) {
final String path = this.et_path.getText().toString().trim();
if (TextUtils.isEmpty(path)) {
Toast.makeText(this, "路径不能为空!", Toast.LENGTH_SHORT).show();
} else {
new Thread() {
public void run() {
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.setRequestMethod("GET");
conn.setReadTimeout(5000);
int code = conn.getResponseCode();
if (code == 200) {
InputStream is = conn.getInputStream();
String result = StreamTool.readInputStream(is);
Message msg = new Message();
msg.what = SHOW_CONTENT;
msg.obj = result;
handler.sendMessage(msg);
} else {
Message msg = new Message();
msg.what = ERROR;
handler.sendMessage(msg);
}
} catch (Exception e) {
e.printStackTrace();
Message msg = new Message();
msg.what = ERROR;
handler.sendMessage(msg);
}
};
}.start();
}
}
}
3.工具类
public class StreamTool {
public static String readInputStream(InputStream is) {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
int length = 0;
byte[] buffer = new byte[1024];
if ((length = is.read(buffer)) != -1) {
out.write(buffer, 0, length);
}
byte[] result = out.toByteArray();
return new String(result);
} catch (Exception e) {
// TODO: handle exception
}
return "转换失败";
}
}
4.授权
<uses-permission android:name="android.permission.INTERNET"/>
posted on 2012-10-16 10:25 Freedom000 阅读(136) 评论(0) 编辑 收藏 举报