您好,登錄后才能下訂單哦!
本篇文章為大家展示了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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。