Android中如何使用retrofit上传文件
本篇文章为大家展示了Android中如何使用retrofit上传文件,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
目前成都创新互联公司已为成百上千家的企业提供了网站建设、域名、网页空间、网站托管、企业网站设计、吴江网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
1.AndroidMainfest.xml中加入权限
添加依赖
//fresco compile 'com.facebook.fresco:fresco:+' //支持gif compile 'com.facebook.fresco:animated-gif:+' compile 'com.squareup.okhttp3:okhttp:3.9.1' compile 'com.google.code.gson:gson:2.8.1' //retrofit compile 'com.squareup.retrofit2:retrofit:+' compile 'com.squareup.retrofit2:converter-gson:+' //Rxjava2 compile 'io.reactivex.rxjava2:rxjava:+' compile 'io.reactivex.rxjava2:rxandroid:+' //让retrofit支持Rxjava2 compile 'com.squareup.retrofit2:adapter-rxjava2:+' compile 'com.squareup.okhttp3:logging-interceptor:3.9.1'
1.网络请求
public class RetrofitUtils { //自定义路径 public static final String BASE_URL ="http://120.27.23.105/"; private final Retrofit mRetrofit; public static class SINGLE_HOLDER{ public static final RetiofitUtils INSTANCE = new RetiofitUtils(BASE_URL); } public static RetrofitUtils getInstance(){ return SINGLE_HOLDER.INSTANCE; } r private RetrofitUtils(String baseUrl){ mRetrofit = buildRetrofit(); } // private OkHttpClient buildOkHttpClient(){ HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); logging.setLevel(HttpLoggingInterceptor.Level.BODY); return new OkHttpClient.Builder() .connectTimeout(10000, TimeUnit.MILLISECONDS) .addInterceptor(new Intercept())//拦截器 .addInterceptor(logging) .build(); } //创建retrofit private Retrofit buildRetrofit(){ return new Retrofit.Builder() .client(buildOkHttpClient()) .baseUrl(BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build(); } //泛型类 publicT create(Class tClass){ return mRetrofit.create(tClass); } }
拦截器:https://www.jb51.net/article/133257.htm
路径接口
public interface RetiofitVpi { //查找用户信息 @GET("user/getUserInfo") ObservableuserBean(@Query("uid") String uid); //上传文件 @Multipart @POST("file/upload") Observable uploadFile(@Query("uid") String uid, @Part("file\"; filename=\"avatar.jpg") RequestBody file); }
anim文件夹下的文件
布局
mainActiviy,请求要替换头像的接口的用户uid
public class MainActivity extends AppCompatActivity { private SimpleDraweeView simple_drawee_view; private PopupWindow window; private String path; private Uri uri; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //初始化 Fresco.initialize(this); setContentView(R.layout.activity_main); //获取到头像的布局 simple_drawee_view = (SimpleDraweeView) findViewById(R.id.simple_drawee_view); path = Environment.getExternalStorageDirectory() + "/head.jpg"; //获取uid ObservableuserInfo = userBean("3600"); userInfo //需要在io子线程联网 .subscribeOn(Schedulers.io()) //需要在主线程更新UI .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Consumer () { @Override public void accept(UserBean nicknameBean) throws Exception { UserBean.DataBean data = nicknameBean.getData(); uri = Uri.parse(data.getIcon()); simple_drawee_view.setImageURI(uri); } }, new Consumer () { @Override public void accept(Throwable throwable) throws Exception { Log.e("MainActivity",throwable.toString()); } }); //给上传头像的控件设置点击事件 simple_drawee_view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //得到更换头像的popwindow showPopwindow(); } }); } private Observable userBean(String uid) { RetiofitVpi iRetiofitVip = RetiofitUtils.getInstance().create(RetiofitVpi.class); return iRetiofitVip.userBean(uid); } }
上述内容就是Android中如何使用retrofit上传文件,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。
网页题目:Android中如何使用retrofit上传文件
当前链接:http://cdiso.cn/article/gosjjh.html