Android retrofit上傳文件實(shí)例(包含頭像)
上傳文件主要就是通過(guò)接口
1.AndroidMainfest.xml中加入權(quán)限
<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"/>
添加依賴(lài)
//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.網(wǎng)絡(luò)請(qǐng)求
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(); } //創(chuàng)建retrofit private Retrofit buildRetrofit(){ return new Retrofit.Builder() .client(buildOkHttpClient()) .baseUrl(BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build(); } //泛型類(lèi) public <T> T create(Class<T> tClass){ return mRetrofit.create(tClass); } }
攔截器:http://www.dbjr.com.cn/article/133257.htm
路徑接口
public interface RetiofitVpi { //查找用戶(hù)信息 @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>標(biāo)簽代表一系列的幀動(dòng)畫(huà),可以在里面添加動(dòng)畫(huà)效果 --> <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>標(biāo)簽代表一系列的幀動(dòng)畫(huà),可以在里面添加動(dòng)畫(huà)效果 --> <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,請(qǐng)求要替換頭像的接口的用戶(hù)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子線(xiàn)程聯(lián)網(wǎng) .subscribeOn(Schedulers.io()) //需要在主線(xiàn)程更新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()); } }); //給上傳頭像的控件設(shè)置點(diǎn)擊事件 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上傳文件實(shí)例(包含頭像)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Android使用原生組件WebView加載網(wǎng)頁(yè)和數(shù)據(jù)的方法
這篇文章主要介紹了Android使用原生組件WebView加載網(wǎng)頁(yè)和數(shù)據(jù)的方法的相關(guān)資料,需要的朋友可以參考下2016-09-09Android adb安裝apk時(shí)提示Invalid APK file的問(wèn)題
這篇文章主要介紹了Android adb安裝apk時(shí)提示Invalid APK file的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08Android 監(jiān)聽(tīng)Notification 被清除實(shí)例代碼
本文主要介紹Android 監(jiān)聽(tīng)Notification 事件,這里給大家提供實(shí)例代碼進(jìn)行參考,有需要的小伙伴可以參考下2016-07-07Android實(shí)現(xiàn)計(jì)算器(計(jì)算表達(dá)式/計(jì)算小數(shù)點(diǎn)以及括號(hào))
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)計(jì)算器功能,計(jì)算表達(dá)式,能計(jì)算小數(shù)點(diǎn)以及括號(hào),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09Android實(shí)現(xiàn)GPS定位代碼實(shí)例
這篇文章主要介紹了Android實(shí)現(xiàn)GPS定位實(shí)例,對(duì)關(guān)鍵操作部份給出代碼示例并做了一定的注釋,需要的朋友可以參考下2014-07-07Android實(shí)現(xiàn)簡(jiǎn)單卡片布局
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)卡片布局的相關(guān)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10Android中使用 AutoCompleteTextView 實(shí)現(xiàn)手機(jī)號(hào)格式化附帶清空歷史的操作
有個(gè)小伙伴遇到了這樣一個(gè)問(wèn)題,就是AutoCompleteTextView實(shí)現(xiàn)自動(dòng)填充的功能。同時(shí)要具備手機(jī)格式化的功能。接下來(lái)通過(guò)本文給大家分享使用 AutoCompleteTextView 實(shí)現(xiàn)手機(jī)號(hào)格式化附帶清空歷史的操作方法,需要的朋友參考下2017-03-03詳解 android 光線(xiàn)傳感器 light sensor的使用
這篇文章主要介紹了詳解 android 光線(xiàn)傳感器 light sensor的使用的相關(guān)資料,需要的朋友可以參考下2017-06-06Android BroadcastReceiver廣播注冊(cè)方式總結(jié)
這篇文章主要介紹了Android BroadcastReceiver廣播注冊(cè)方式總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-01-01photoView實(shí)現(xiàn)圖片多點(diǎn)觸控效果
這篇文章主要為大家詳細(xì)介紹了photoView實(shí)現(xiàn)圖片多點(diǎn)觸控效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12