API
@Multipart @POST("file/upload") Observable<TouXiangBean> touxiang(@Query("uid") int uid, @Part MultipartBody.Part part); @GET("user/getUserInfo") Observable<YongHuBean> yonghu(@Query("uid") int uid);
//把这儿放开
头像mvp
model
package com.example.jingdong2.touxiang.model; import com.example.jingdong2.bean.TouXiangBean; import com.example.jingdong2.utils.ILoginApi; import com.example.jingdong2.utils.RetrofitManager; import java.io.File; import io.reactivex.Observable; import okhttp3.MediaType; import okhttp3.MultipartBody; import okhttp3.RequestBody; /** * Created by 红鼻子小黑 on 2018/11/22. */ public class TouXiangModel { public Observable<TouXiangBean> touxiang(int uid, File file){ ILoginApi iLoginApi = RetrofitManager.getInstance().create(ILoginApi.class); RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"),file); MultipartBody.Part part = MultipartBody.Part.createFormData("file",file.getName(),requestBody); Observable<TouXiangBean> touxiang = iLoginApi.touxiang(uid, part); return touxiang; } }
presenter
package com.example.jingdong2.touxiang.presenter; import com.example.jingdong2.bean.TouXiangBean; import com.example.jingdong2.touxiang.model.TouXiangModel; import com.example.jingdong2.touxiang.view.TouXiangIView; import java.io.File; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.functions.Consumer; import io.reactivex.schedulers.Schedulers; /** * Created by 红鼻子小黑 on 2018/11/22. */ public class TouXiangPresenter { private TouXiangIView iv; private TouXiangModel touXiangModel; public void attach(TouXiangIView iv){ this.iv = iv; touXiangModel = new TouXiangModel(); } public void xiangji(int uid, File file){ touXiangModel.touxiang(uid,file) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Consumer<TouXiangBean>() { @Override public void accept(TouXiangBean touXiangBean) throws Exception { if (touXiangBean !=null & "0".equals(touXiangBean)){ if (iv !=null) iv.onSuccess(touXiangBean); return; } if (iv !=null){ iv.onFaild(new Throwable("服务器未响应")); } } }, new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { iv.onFaild(new Throwable("网络请求失败")); } }); } public void dettach(){ if (iv !=null){ iv = null; } } }
view
package com.example.jingdong2.touxiang.view; import com.example.jingdong2.bean.TouXiangBean; /** * Created by 红鼻子小黑 on 2018/11/22. */ public interface TouXiangIView { void onSuccess(TouXiangBean touXiangBean); void onFaild(Throwable t); }
封装
package com.example.jingdong2.bean; /** * Created by 红鼻子小黑 on 2018/11/22. */ public class TouXiangBean { private String headPath; private String message; private String status; public String getHeadPath() { return headPath; } public void setHeadPath(String headPath) { this.headPath = headPath; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } }
@Override public void onSuccess(TouXiangBean touXiangBean) { Toast.makeText(GeRenActivity.this, "头像成功", Toast.LENGTH_SHORT).show(); SharedPreferences sp = getSharedPreferences("config", MODE_PRIVATE); int uid = sp.getInt("uid", 8); yongHuPresenter.yonghu(uid); } @Override public void onSuccess(YongHuBean yongHuBean) { icon = yongHuBean.getData().getIcon(); String replace = icon.replace("https", "http"); SharedPreferences sp = getSharedPreferences("config",MODE_PRIVATE); sp.edit().putString("toutou",replace).commit(); }
每次进入先走查询,再走一遍用户信息查询
@Override protected void onResume() { super.onResume(); SharedPreferences sp = getSharedPreferences("config", MODE_PRIVATE); int uid = sp.getInt("uid", 8); yongHuPresenter.yonghu(uid); }
http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2017/1102/8678.html