亲宝软件园·资讯

展开

Android retrofit上传文件实例(包含头像)

人气:0

上传文件主要就是通过接口

1.AndroidMainfest.xml中加入权限

 <uses-permission android:name="android.permission.INTERNET"/>
 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
 <uses-permission android:name="android.permission.CAMERA"/>
 <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

添加依赖

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

拦截器:

路径接口

public interface RetiofitVpi {
 //查找用户信息
 @GET("user/getUserInfo")
 Observable<UserBean> userBean(@Query("uid") String uid);
 //上传文件
 @Multipart
 @POST("file/upload")
 Observable<FileBean> uploadFile(@Query("uid") String uid,
     @Part("file\"; filename=\"avatar.jpg") RequestBody file);   
}

anim文件夹下的文件

<!--android<set>标签代表一系列的帧动画,可以在里面添加动画效果 -->
<set xmlns:android="http://schemas.android.com/apk/res/android" >
 <translate
 android:duration="2000"
 android:fromYDelta="100%p"
 android:toYDelta="0" />
 <alpha
 android:duration="2000"
 android:fromAlpha="0.0"
 android:toAlpha="1.0" />
</set>
<!--android<set>标签代表一系列的帧动画,可以在里面添加动画效果 -->
<set xmlns:android="http://schemas.android.com/apk/res/android" >
 <translate
 android:duration="2000"
 android:fromYDelta="100%p"
 android:toYDelta="0" />
 <alpha
 android:duration="2000"
 android:fromAlpha="0.0"
 android:toAlpha="1.0" />
</set>

布局

<com.facebook.drawee.view.SimpleDraweeView
 android:layout_centerInParent="true"
 android:id="@+id/simple_drawee_view"
 android:layout_width="300dp"
 android:layout_height="300dp"
 fresco:failureImage="@drawable/icon_failure"
 fresco:progressBarImage="@drawable/icon_placeholder"
 fresco:placeholderImage="@drawable/icon"
 fresco:progressBarAutoRotateInterval="1000"
 fresco:retryImageScaleType="centerInside"
 fresco:roundAsCircle="true"
 tools:layout_editor_absoluteY="41dp"
 tools:layout_editor_absoluteX="55dp"
 />

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<UserBean> userInfo = userBean("3600");
 userInfo
  //需要在io子线程联网
  .subscribeOn(Schedulers.io())
  //需要在主线程更新UI
  .observeOn(AndroidSchedulers.mainThread())
  .subscribe(new Consumer<UserBean>() {
   @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<Throwable>() {
   @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> userBean(String uid) {
 RetiofitVpi iRetiofitVip = RetiofitUtils.getInstance().create(RetiofitVpi.class);
 return iRetiofitVip.userBean(uid);
 }
 }

以上这篇Android retrofit上传文件实例(包含头像)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

您可能感兴趣的文章:

加载全部内容

相关教程
猜你喜欢
用户评论