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();
 }
 //泛型类
 public  T create(Class tClass){
 return mRetrofit.create(tClass);
 }
}

拦截器://www.jb51.net/article/133257.htm

路径接口

public interface RetiofitVpi {
 //查找用户信息
 @GET("user/getUserInfo")
 Observable userBean(@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
 Observable userInfo = 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上传文件实例(包含头像))