上传头像

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值