package com.web.test;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.util.ByteArrayBuffer;
import org.apache.http.util.EncodingUtils;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloWeb extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = new TextView(this);
String myString = null;
try {
URL uri = new URL("http://www.baidupcs.com/file/1cb251ec0d568de6a929b520c4aed8d1?xcode=8b8eddcff9b768b0fe3b5383d02719e1682db0a8df3ab0c0&fid=2987144212-250528-3553184730&time=1379061716&sign=FDTAXER-DCb740ccc5511e5e8fedcff06b081203-IajFP7ZTY8WIHqM2f7Ao%2BQKeNbQ%3D&to=wb&fm=N,B,M,mn&expires=8h&rt=sh&r=128013118&logid=2936171168&sh=1&fn=test.txt");
URLConnection ucon = uri.openConnection();
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(100);
int current = 0;
while((current = bis.read()) != -1) {
baf.append((byte)current);
}
myString = new String(baf.toByteArray(), "GBK");
} catch(Exception e) {
myString = e.getMessage();
}
tv.setText(myString);
this.setContentView(tv);
}
} 求大神看一下哪里出问题了?在真机上测试总会抛出异常。
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.util.ByteArrayBuffer;
import org.apache.http.util.EncodingUtils;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloWeb extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = new TextView(this);
String myString = null;
try {
URL uri = new URL("http://www.baidupcs.com/file/1cb251ec0d568de6a929b520c4aed8d1?xcode=8b8eddcff9b768b0fe3b5383d02719e1682db0a8df3ab0c0&fid=2987144212-250528-3553184730&time=1379061716&sign=FDTAXER-DCb740ccc5511e5e8fedcff06b081203-IajFP7ZTY8WIHqM2f7Ao%2BQKeNbQ%3D&to=wb&fm=N,B,M,mn&expires=8h&rt=sh&r=128013118&logid=2936171168&sh=1&fn=test.txt");
URLConnection ucon = uri.openConnection();
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(100);
int current = 0;
while((current = bis.read()) != -1) {
baf.append((byte)current);
}
myString = new String(baf.toByteArray(), "GBK");
} catch(Exception e) {
myString = e.getMessage();
}
tv.setText(myString);
this.setContentView(tv);
}
} 求大神看一下哪里出问题了?在真机上测试总会抛出异常。
解决方案 »
- Android 应用读写I2C
- 关于BitmapFactory.setDefaultConfig(Bitmap.Config.ARGB_8888);这个方法的使用
- android拍照与本地上传图片功能,在线等
- Google Map:MapView的经纬度图标切换
- Androidmainfest里对main的设置
- 黑马教程 netviewImage android 4.4之后的网络政策使之前的代码异常运行
- 关于spinner的setOnItemLongClickListener
- 请问如何限制某些程序的安装?
- 如何调节加速度传感的灵敏度?
- eclipse中较新版本的adt中的layout editor是不是只有一种视图模式了
- android没有finish却执行了onDestroy方法,太诡异了?
- 一个关于监听菜单键的问题
4.0以后强制要求这么做,否则会报android.os.NetworkOnMainThreadException。
问题是我是在安卓2.3平台上测试的。每次测试总会跳到catch(Exception e) {
myString = e.getMessage();
} 这个语句里
抛出的异常是java.net.UnknownHostException:www.baidupcs.com
你Manifest里面有没有加这个权限:
<uses-permission android:name="android.permission.INTERNET"/>
有这个才能联网