1 网络如果是http
在res 下创建xml文件 再创建 network-security-config.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true">
<trust-anchors>
<certificates src="system"/>
<certificates src="user"/>
</trust-anchors>
</base-config>
</network-security-config>
在Androidmanifest
//网络权限
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:networkSecurityConfig="@xml/network_security_config">
</application>
在build.gradle
AS引用
dependencies {
implementation 'com.squareup.okhttp3:okhttp:3.12.0'
implementation 'com.squareup.picasso:picasso:2.71828'
}
//数据加载
//Activy
//import okhttp3.Callback;
//import okhttp3.OkHttpClient;
//import okhttp3.Request;
//import okhttp3.Call;
//import okhttp3.Response;
public void addNework(){
String url = "";
OkHttpClient okHttpClient = new OkHttpClient();
final Request request = new Request.Builder()
.url(url)
.get()//默认就是GET请求,可以不写
.build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.v("Tag","eff"+e);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.v("TAG", "onResponse: " + response.body().string());
}
});
}
//图片下载
public void aaImageView(){
Picasso.get().load("http://www.chaiqian88.com/d/file/sybanner/2018-10-23/1e184615665d4cd9a6bb82949d1614b0.png").into(imageView);
// Picasso.with(this).load().info();
//.onlyScaleDown()当调用了resize 方法重新设置图片尺寸的时候,调用onlyScaleDown 方法,只有当原始图片的尺寸大于我们指定的尺寸时,resize才起作用,
//Picasso.get().load("http://www.chaiqian88.com/d/file/sybanner/2018-10-23/1e184615665d4cd9a6bb82949d1614b0.png").resize(400,200).onlyScaleDown().into(imageView);
}