implementation 'com.squareup.picasso:picasso:2.5.2'
compile 'com.squareup.picasso:picasso:2.5.2'
<!--网络权限-->
<uses-permission android:name="android.permission.INTERNET" />
文件读写权限="android.permission.READ_EXTERNAL_STORAGE" />
private ImageView im;
im=findViewById(R.id.imageView2);
//picben("https://pic.cnblogs.com/face/1485202/20180908195218.png",im);
picben("/dongxiaodong/ww.jpg",im);
1 参数(地址,显示的控件)
2 public void picben(String panx,ImageView imgviewx){
3 File sd= Environment.getExternalStorageDirectory();得到SD卡的路径
4 Picasso.with(MainActivity.this)
5 .load(new File(sd,panx))文件路径,必须打开文件读写权限
6 .load(urlx)URL,必须添加网络权限
7 .memoryPolicy(MemoryPolicy.NO_CACHE,MemoryPolicy.NO_STORE)待测试,图片加载跳过内存缓存中查找,图片不缓存到内存缓存中
8 .memoryPolicy(MemoryPolicy.NO_CACHE)无缓存,加下面一句可实现
9 .networkPolicy(NetworkPolicy.NO_CACHE)
10 .placeholder(R.mipmap.mainon)正在加载图片
11 .error(R.mipmap.mainoff)尝试三次失败后 加载错误显示图片
12 .fit()填充控件与resize不可共用
13 .noFade()无淡入淡出效果
14 .resize(100,100)图片要调整后的大小,必须配合下面两个之一
15 .centerCrop()配合resize使用,仅显示被框占位
16 .centerInside()配合resize使用,裁剪后填充满控件
17 .rotate(30)旋转30度
18 .transform(new tCircle())可变为圆形状,但有黑底
19 .config(Bitmap.Config.RGB_565)比ARGB_8888 质量稍差,但看基本不出
20 .into(imgviewx);控件位置
21 }
implementation 'com.squareup.okhttp3:okhttp:3.2.0'
<!--网络权限-->
<uses-permission android:name="android.permission.INTERNET" />
<!--文件读写权限-->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
HashMap<String,String> mapx=new HashMap<String,String>();
mapx.put("yh","dongdong");
mapx.put("mm","xxxxxxx");
OKhttp.getOhttp().okpost("post.php",mapx,new OKhttp.Jkstr() {
@Override
jkstr(String str) {
Toast.makeText(MainActivity.,str,Toast.LENGTH_SHORT).show();
System.out.println(str);
}
});
OKhttp.getOhttp().okget("post.php?dong=dongxiaodong&dong2=dongdongx",1)"> }
});
1 package com.example.testdelete;
2 import android.os.Environment;
3 android.os.Handler;
4 java.io.File;
5 java.io.IOException;
6 java.util.Map;
7 okhttp3.Call;
8 okhttp3.Callback;
9 okhttp3.FormBody;
10 okhttp3.MediaType;
11 okhttp3.MultipartBody;
12 okhttp3.OkHttpClient;
13 okhttp3.Request;
14 okhttp3.RequestBody;
15 okhttp3.Response;
16 /**
17 * Created by 东东 on 2018/3/29.
18 */
19 class OKhttp {
20 OKhttp(){};
21 private static final OKhttp ohttp= OKhttp();
22 static OKhttp getOhttp(){
23 return ohttp;
24 }
25 private OkHttpClient client= OkHttpClient();
26 private Handler handlerx= Handler();
27 final String UR="http://193.110.8.6/testdelete/";
28 File sd= Environment.getExternalStorageDirectory();得到SD卡的路径
29 文件上传
30 参数(上传的url,文件地址,文件名,回调函数)
31 void okpostfile(String url,String path,String filenaem,1)">final Jkstr callbackfile) {get和post数据同时上传
32 File file = File(sd,path);
33 RequestBody fileBody = RequestBody.create(MediaType.parse("application/octet-stream"),file);
34 RequestBody requestBody = MultipartBody.Builder()
35 .setType(MultipartBody.FORM)
36 .addFormDataPart("mfile",filenaem,fileBody)Post参数
37 .build();
38 Request request = Request.Builder()
39 .url(UR+url)
40 .post(requestBody)
41 42 client.newCall(request).enqueue( Callback() {
43 @Override
44 void onFailure(Call call,IOException e) {请求失败
45 ohttpstr("0" 46 }
47 48 void onResponse(Call call,Response response) throws IOException {请求成功
49 ohttpstr(response.body().string(),1)"> 50 51 });
52 53 Get请求
54 参数(url,回调函数)
55 void okget(String url,1)">final Jkstr callbakestr){
56 final Request res=new Request.Builder().url(UR+url).build();
57 client.newCall(res).enqueue( 58 59 60 ohttpstr("0" 61 e.printStackTrace();
62 63 64 65 if(response.isSuccessful()){
66 ohttpstr(response.body().string(),1)"> 67 }
68 69 70 71 Post请求
72 73 void okpost(String ur,Map<String,String> mapx,1)"> Jkstr callbackform){
74 FormBody.Builder formx= FormBody.Builder();
75 if(mapx!=null&&!mapx.isEmpty()){
76 for(Map.Entry<String,1)">xx:mapx.entrySet()){
77 formx.add(xx.getKey(),xx.getValue());
78 79 RequestBody resbody=formx.build();
80 Request req=ur).post(resbody).build();
81 client.newCall(req).enqueue(new Callback() { 82 @Override
83 onFailure(Call call,IOException e) {
84 ohttpstr("0" 85 }
86
87 88 89 90 ohttpstr(response.body().string(),1)"> 91 }
92 93 });
94 }
95 96 请求返回为byte数组
97 void ohttpbyt(final byte[] bytx,1)"> Jkbyt callbackx){
98 handlerx.post( Runnable() {
99 100 run() {
101 if(callbackx!=null){
102 try{
103 callbackx.jkbyt(bytx);
104 }catch (Exception e){
105 e.printStackTrace();
106 }
107 108 109 110 111 处理返回为string类型数据
112 void ohttpstr(final String strx,1)"> Jkstr callback){
113 handlerx.post(114 115 116 if(callback!=117 118 callback.jkstr(strx.trim());
119 }120 e.printStackTrace();
121 122 123 124 125 126 interface Jkstr{ jkstr(String str); }
127 interface Jkbyt{void jkbyt(byte [] bytexx);}
128 }