1.報錯:Caused by: android.os.NetworkOnMainThreadException
原因:在Android 4.0以上,網絡連接不能放在主線程上,不然就會報錯android.os.NetworkOnMainThreadException。但是4.0下版本可以不會報錯。
解決方法:
①可以再Activity的onCreate()方法中加入這樣一段代碼,適用於網絡請求數據量很小的話,如下
if (android.os.Build.VERSION.SDK_INT > 9) { StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); }
②啟動一條子線程進行你的網絡請求,推薦使用這種
// Android 4.0 之后不能在主線程中請求HTTP請求 new Thread(new Runnable(){ @Override public void run() { cachedImage = asyncImageLoader.loadDrawable(imageUrl, position); imageView.setImageDrawable(cachedImage); } }).start();
2.報錯: Caused by: java.net.ConnectException: Connection refused
解決方法:
源代碼:
String url = "jdbc:mysql://localhost:3306/bishe_shoes";
修改:
//模擬器默認把localhost或者127.0.0.1當做本身,在模擬器上可以用10.0.2.2代替127.0.0.1和localhost String url = "jdbc:mysql://10.0.2.2:3306/bishe_shoes";
