1. 报错
java.lang.NoClassDefFoundError:failed resolution of
:Lorg/apache/http/ProtocolVersion
解决:
在AndroidManifest.xml文件的application标签里面加入:
<uses-library android:name="org.apache.http.legacy" android:required="false" />
2. 报错:
android.os.NetworkOnMainThreadException...
解决:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 设置线程策略
setVersion();
getServerData();
}
void setVersion() {
StrictMode.setThreadPolicy(new StrictMode
.ThreadPolicy.Builder()
.detectDiskReads()
.detectDiskWrites()
.detectNetwork()
.penaltyLog()
.build());
StrictMode.setVmPolicy(new StrictMode
.VmPolicy.Builder()
.detectLeakedSqlLiteObjects() //探测SQLite数据库操作
.penaltyLog() //打印logcat
.penaltyDeath()
.build());
}
3. bitmap显示在线图片应该是:
此代码参考该文章:
public Bitmap getBitmap(String s) {
URL url=null;
HttpURLConnection urlConnection=null;
Bitmap bitmap = null;
try {
url=new URL(s);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setConnectTimeout(5000);
urlConnection.setReadTimeout(5000);
int res = urlConnection.getResponseCode();
if(res==200){
InputStream is = urlConnection.getInputStream();
bitmap = BitmapFactory.decodeStream(is);//把inputStream数据流转换为Bitmap
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}