这是一个经典错误, Socket不能对外连接,错误不会被报出,调试的时候,能看到Exception, 一般是抛出 java.net.socketexception permission denied这个异常。 只要你的程序想联网,就会抛出这个异常,最终联网失败。 原因是: 需要访问到网络必须要有权限, 在AndroidManifest.xml中,需要进行如下配置: <uses-permission android:name="android.permission.INTERNET" />
注意:造成这样的错误原因是代码不符合Android规范,如果把上面访问方式改为异步操作就不会出现在4.0上访问出现 android.os.NetworkOnMainThreadException异常
如:
new Thread(){
@Override
public void run(){
//你要执行的方法
//执行完毕后给handler发送一个空消息
handler.sendEmptyMessage(0);
}
}.start();
//定义Handler对象
private Handler handler =new Handler(){
@Override
//当有消息发送出来的时候就执行Handler的这个方法
public void handleMessage(Message msg){
super.handleMessage(msg);
//处理UI
}
};
10-01 08:35:59.125: W/ActivityManager(297): Unable to start service Intent { act=com.android.email.ACCOUNT_INTENT } U=0: not found
10-01 08:35:59.175: E/ActivityThread(678): Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d07088 that was originally bound here
10-01 08:35:59.175: E/ActivityThread(678): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d07088 that was originally bound here
10-01 08:35:59.175: E/ActivityThread(678): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
10-01 08:35:59.175: E/ActivityThread(678): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
10-01 08:35:59.175: E/ActivityThread(678): at android.app.ContextImpl.bindService(ContextImpl.java:1418)
10-01 08:35:59.175: E/ActivityThread(678): at android.app.ContextImpl.bindService(ContextImpl.java:1407)
10-01 08:35:59.175: E/ActivityThread(678): at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
10-01 08:35:59.175: E/ActivityThread(678): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
10-01 08:35:59.175: E/ActivityThread(678): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
10-01 08:35:59.175: E/ActivityThread(678): at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
10-01 08:35:59.175: E/ActivityThread(678): at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
10-01 08:35:59.175: E/ActivityThread(678): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
10-01 08:35:59.175: E/ActivityThread(678): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
10-01 08:35:59.175: E/ActivityThread(678): at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-01 08:35:59.175: E/ActivityThread(678): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
10-01 08:35:59.175: E/ActivityThread(678): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
10-01 08:35:59.175: E/ActivityThread(678): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
10-01 08:35:59.175: E/ActivityThread(678): at java.lang.Thread.run(Thread.java:856)
10-01 08:35:59.205: E/StrictMode(678): null
10-01 08:35:59.205: E/StrictMode(678): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d07088 that was originally bound here