您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
本篇文章为大家展示了Android中如何使用retrofit上传文件,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
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); } }
拦截器:https://www.jb51.net/article/133257.htm
路径接口
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上传文件,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。