1.工具类将bitmap转换成file文件
package com.example.wdshop.util;
import android.graphics.Bitmap;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
public class ImageFileUtil {
public static void setBitmap(Bitmap bitmap,String path,int quality){
String sub = path.substring(0, path.lastIndexOf("/"));
File subFile = new File(sub);
if(!subFile.exists() || !subFile.isDirectory()){
try {
if(!subFile.mkdirs()){
return;
}
}catch (Exception e){
e.printStackTrace();
}
}
FileOutputStream outputStream;
try {
outputStream = new FileOutputStream(path);
if(bitmap.compress(Bitmap.CompressFormat.PNG,quality,outputStream)){
outputStream.flush();
outputStream.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
}
2.RetrofitManager工具类
package com.example.wdshop.network;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
import android.text.TextUtils;
import com.example.wdshop.application.MyApplication;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
public class RetrofitManager<E> {
private final String BASE_URL = "http://mobile.bwstudent.com/small/";
private static RetrofitManager instance;
private final BaseApis baseApis;
//单例
public static RetrofitManager getInstance() {
if (instance == null) {
synchronized (RetrofitManager.class) {
instance = new RetrofitManager();
}
}
return instance;
}
//无参构造
private RetrofitManager() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.readTimeout(10, TimeUnit.SECONDS);
builder.writeTimeout(10, TimeUnit.SECONDS);
builder.connectTimeout(10, TimeUnit.SECONDS);
builder.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
SharedPreferences preferences = MyApplication.getApplication().getSharedPreferences("User", Context.MODE_PRIVATE);
String userId = preferences.getString("userId", "");
String sessionId = preferences.getString("sessionId", "");
Request.Builder builder1 = request.newBuilder();
builder1.method(request.method(),request.body());
if(!TextUtils.isEmpty(userId)&&!TextUtils.isEmpty(sessionId)){
builder1.addHeader("userId",userId);
builder1.addHeader("sessionId",sessionId);
}
Request build = builder1.build();
return chain.proceed(build);
}
});
builder.retryOnConnectionFailure(true);
OkHttpClient build = builder.build();
Retrofit retrofit = new Retrofit.Builder()
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.baseUrl(BASE_URL)
.client(build)
.build();
baseApis = retrofit.create(BaseApis.class);
}
/**
* 可以这样生成Map<String, RequestBody> requestBodyMap
* Map<String, String> requestDataMap这里面放置上传数据的键值对。
*/
public Map<String, RequestBody> generateRequestBody(Map<String, String> requesrDataMap) {
Map<String, RequestBody> requestBodyMap = new HashMap<>();
for (String key : requesrDataMap.keySet()) {
RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"),
requesrDataMap.get(key) == null ? "" : requesrDataMap.get(key));
requestBodyMap.put(key, requestBody);
}
return requestBodyMap;
}
/**
* get
*/
public void get(String url,HttpListener listener) {
baseApis.get(url)
//后台执行在哪个线程
.subscribeOn(Schedulers.io())
//最终完成后执行在哪个线程
.observeOn(AndroidSchedulers.mainThread())
//设置rxjava
.subscribe(getObserver(listener));
}
/**
* delete
* */
public void delete(String url,HttpListener listener) {
baseApis.delete(url)
//后台执行在哪个线程
.subscribeOn(Schedulers.io())
//最终完成后执行在哪个线程
.observeOn(AndroidSchedulers.mainThread())
//设置rxjava
.subscribe(getObserver(listener));
}
/**
* 观察者
*/
private Observer getObserver(final HttpListener listener) {
Observer observer = new Observer<ResponseBody>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
if (listener != null) {
listener.onFail(e.getMessage());
}
}
@Override
public void onNext(ResponseBody responseBody) {
try {
String data = responseBody.string();
if (null != listener) {
listener.onSuccess(data);
}
} catch (IOException e) {
e.printStackTrace();
if (listener != null) {
listener.onFail(e.getMessage());
}
}
}
};
return observer;
}
/**
* 表单post请求
*/
public void postFormBody(String url, Map<String, RequestBody> map,HttpListener listener) {
if (map == null) {
map = new HashMap<>();
}
baseApis.postFormBody(url,map)
//后台执行在哪个线程
.subscribeOn(Schedulers.io())
//最终完成后执行在哪个线程
.observeOn(AndroidSchedulers.mainThread())
//设置rxjava
.subscribe(getObserver(listener));
}
/**
* 普通post
* */
public void post(String url,Map<String,String> map,HttpListener listener){
if(map == null){
map = new HashMap<>();
}
baseApis.post(url,map)
// 后台执行在哪个线程
.subscribeOn(Schedulers.io())
//最终完成后执行在哪个线程
.observeOn(AndroidSchedulers.mainThread())
//设置rxjava
.subscribe(getObserver(listener));
}
/**
* 普通put
* */
public void put(String url,Map<String,String> map,HttpListener listener){
if(map == null){
map = new HashMap<>();
}
baseApis.put(url,map)
// 后台执行在哪个线程
.subscribeOn(Schedulers.io())
//最终完成后执行在哪个线程
.observeOn(AndroidSchedulers.mainThread())
//设置rxjava
.subscribe(getObserver(listener));
}
/**
*上传头像
* */
public static MultipartBody filesMultipar(Map<String,String> map){
MultipartBody.Builder builder = new MultipartBody.Builder();
for(Map.Entry<String,String> entry:map.entrySet()){
File file = new File(entry.getValue());
builder.addFormDataPart(entry.getKey(),"tp.png",
RequestBody.create(MediaType.parse("multipart/form-data"),file));
}
builder.setType(MultipartBody.FORM);
MultipartBody multipartBody = builder.build();
return multipartBody;
}
public void imagePost(String url, Map<String,String> map,HttpListener listener){
if(map == null){
map = new HashMap<>();
}
MultipartBody multipartBody = filesMultipar(map);
baseApis.imagePost(url,multipartBody)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(getObserver(listener));
}
/**
* 上传多张图片
*/
public void postFileMore(String url, Map<String, Object> map,HttpListener listener) {
if (map == null) {
map = new HashMap<>();
}
MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
builder.addFormDataPart("commodityId", String.valueOf(map.get("commodityId")));
if(!String.valueOf(map.get("orderId")).equals("")){
builder.addFormDataPart("orderId", String.valueOf(map.get("orderId")));
}
builder.addFormDataPart("content", String.valueOf(map.get("content")));
if (!map.get("image").equals("")) {
List<String> image = (List<String>) map.get("image");
for(int i=0;i<image.size();i++){
File file = new File(image.get(i));
builder.addFormDataPart("image", file.getName(),RequestBody.create(MediaType.parse("multipart/form-data"),file));
}
}
builder.setType(MultipartBody.FORM);
MultipartBody multipartBody = builder.build();
baseApis.imagePost(url, multipartBody)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(getObserver(listener));
}
//定义接口
public interface HttpListener {
void onSuccess(String data);
void onFail(String error);
}
}
3.BaseApis
package com.example.wdshop.network;
import java.util.Map;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.http.Body;
import retrofit2.http.DELETE;
import retrofit2.http.GET;
import retrofit2.http.Headers;
import retrofit2.http.Multipart;
import retrofit2.http.POST;
import retrofit2.http.PUT;
import retrofit2.http.Part;
import retrofit2.http.PartMap;
import retrofit2.http.QueryMap;
import retrofit2.http.Url;
import rx.Observable;
public interface BaseApis<E> {
/**
* Observable被观察者
* */
@GET
Observable<ResponseBody> get(@Url String url);
@POST
Observable<ResponseBody> post(@Url String url, @QueryMap Map<String, String> map);
@Multipart
@POST
Observable<ResponseBody> postFormBody(@Url String url, @PartMap Map<String, RequestBody> requestBodyMap);
@DELETE
Observable<ResponseBody> delete(@Url String url);
@PUT
Observable<ResponseBody> put(@Url String url, @QueryMap Map<String, String> map);
@POST
Observable<ResponseBody> imagePost(@Url String url, @Body MultipartBody multipartBody);
}
3.model层接口
package com.example.wdshop.model;
import java.util.Map;
import okhttp3.MultipartBody;
public interface Imodel {
/**
* post
* */
void requestPost(String url, Map<String, String> map, Class clazz, MyCallBack myCallBack);
/**
* get
* */
void requestGet(String url,Class clazz, MyCallBack myCallBack);
/**
* delete
* */
void requestDelete(String url,Class clazz, MyCallBack myCallBack);
/**
* put
* */
void requestPut(String url, Map<String, String> map, Class clazz, MyCallBack myCallBack);
/**
* 上传头像
* */
void requestImagePost(String url,Map<String,String> map,Class clazz,MyCallBack myCallBack);
/**
* 上传多张图片
* */
void imagePost(String url,Map<String,Object> map,Class clazz,MyCallBack myCallBack);
}
5.model层实现类
package com.example.wdshop.model;
import com.example.wdshop.network.RetrofitManager;
import com.example.wdshop.util.NetUtil;
import com.google.gson.Gson;
import java.util.Map;
import okhttp3.MultipartBody;
public class ModelImpl implements Imodel {
private MyCallBack myCallBack;
/**
* post
* */
@Override
public void requestPost(String url, Map<String, String> map, final Class clazz, final MyCallBack myCallBack) {
this.myCallBack = myCallBack;
if(!NetUtil.hasNetWork()){
myCallBack.onFail("网络不可用,请检查网络状态");
}else {
RetrofitManager.getInstance().post(url, map, new RetrofitManager.HttpListener() {
@Override
public void onSuccess(String data) {
try {
Object o = new Gson().fromJson(data, clazz);
if (myCallBack != null) {
myCallBack.onSuccess(o);
}
} catch (Exception e) {
e.printStackTrace();
if (myCallBack != null) {
myCallBack.onFail(e.getMessage());
}
}
}
@Override
public void onFail(String error) {
if (myCallBack != null) {
myCallBack.onFail(error);
}
}
});
}
}
/**
* get
* */
@Override
public void requestGet(String url, final Class clazz, final MyCallBack myCallBack) {
if(!NetUtil.hasNetWork()){
myCallBack.onFail("网络不可用,请检查网络状态");
}else {
RetrofitManager.getInstance().get(url, new RetrofitManager.HttpListener() {
@Override
public void onSuccess(String data) {
try {
Object o = new Gson().fromJson(data, clazz);
if (myCallBack != null) {
myCallBack.onSuccess(o);
}
} catch (Exception e) {
e.printStackTrace();
if (myCallBack != null) {
myCallBack.onFail(e.getMessage());
}
}
}
@Override
public void onFail(String error) {
if (myCallBack != null) {
myCallBack.onFail(error);
}
}
});
}
}
/**
* delete
* */
@Override
public void requestDelete(String url, final Class clazz, final MyCallBack myCallBack) {
if(!NetUtil.hasNetWork()){
myCallBack.onFail("网络不可用,请检查网络状态");
}else {
RetrofitManager.getInstance().delete(url, new RetrofitManager.HttpListener() {
@Override
public void onSuccess(String data) {
try {
Object o = new Gson().fromJson(data, clazz);
if (myCallBack != null) {
myCallBack.onSuccess(o);
}
} catch (Exception e) {
if (myCallBack != null) {
myCallBack.onFail(e.getMessage());
}
}
}
@Override
public void onFail(String error) {
if (myCallBack != null) {
myCallBack.onFail(error);
}
}
});
}
}
/**
* put
* */
@Override
public void requestPut(String url, Map<String, String> map, final Class clazz, final MyCallBack myCallBack) {
if(!NetUtil.hasNetWork()){
myCallBack.onFail("网络不可用,请检查网络状态");
}else {
RetrofitManager.getInstance().put(url, map, new RetrofitManager.HttpListener() {
@Override
public void onSuccess(String data) {
try {
Object o = new Gson().fromJson(data, clazz);
if (myCallBack != null) {
myCallBack.onSuccess(o);
}
} catch (Exception e) {
if (myCallBack != null) {
myCallBack.onFail(e.getMessage());
}
}
}
@Override
public void onFail(String error) {
if (myCallBack != null) {
myCallBack.onFail(error);
}
}
});
}
}
/**
* 上传头像
* */
@Override
public void requestImagePost(String url, Map<String, String> map, final Class clazz, final MyCallBack myCallBack) {
RetrofitManager.getInstance().imagePost(url, map, new RetrofitManager.HttpListener() {
@Override
public void onSuccess(String data) {
try {
Object o = new Gson().fromJson(data, clazz);
if(myCallBack!=null){
myCallBack.onSuccess(o);
}
}catch (Exception e){
e.printStackTrace();
if(myCallBack!=null){
myCallBack.onFail(e.getMessage());
}
}
}
@Override
public void onFail(String error) {
if(myCallBack!=null){
myCallBack.onFail(error);
}
}
});
}
/**
* 上传多张图
* */
@Override
public void imagePost(String url, Map<String, Object> map, final Class clazz, final MyCallBack myCallBack) {
RetrofitManager.getInstance().postFileMore(url, map, new RetrofitManager.HttpListener() {
@Override
public void onSuccess(String data) {
try {
Object o = new Gson().fromJson(data, clazz);
if(myCallBack!=null){
myCallBack.onSuccess(o);
}
}catch (Exception e){
e.printStackTrace();
if(myCallBack!=null){
myCallBack.onFail(e.getMessage());
}
}
}
@Override
public void onFail(String error) {
if(myCallBack!=null){
myCallBack.onFail(error);
}
}
});
}
}
6.p层接口
package com.example.wdshop.presents;
import java.util.Map;
import okhttp3.MultipartBody;
public interface Ipresenter {
/**
* post请求
* */
void postRequest(String url, Map<String, String> map, Class clazz);
/**
* get请求
* */
void getRequest(String url,Class clazz);
/**
* delete请求
* */
void deleteRequest(String url,Class clazz);
/**
* put请求
* */
void putRequest(String url, Map<String, String> map, Class clazz);
/**
* 上传头像
* */
void imagePostRequest(String url,Map<String,String> map,Class clazz);
/**
* 多图上传
* */
void imagesPostRequest(String url,Map<String,Object> map,Class clazz);
}
7.p层实现类
package com.example.wdshop.presents;
import com.example.wdshop.model.ModelImpl;
import com.example.wdshop.model.MyCallBack;
import com.example.wdshop.view.Iview;
import java.util.Map;
import okhttp3.MultipartBody;
public class PresenterImpl implements Ipresenter {
private Iview mIview;
private ModelImpl model;
public PresenterImpl(Iview iview) {
mIview = iview;
model = new ModelImpl();
}
/**
* post
*/
@Override
public void postRequest(String url, Map<String, String> map, Class clazz) {
model.requestPost(url, map, clazz, new MyCallBack() {
@Override
public void onSuccess(Object data) {
mIview.requestData(data);
}
@Override
public void onFail(String error) {
mIview.requestFail(error);
}
});
}
/**
* get
*/
@Override
public void getRequest(String url, Class clazz) {
model.requestGet(url, clazz, new MyCallBack() {
@Override
public void onSuccess(Object data) {
mIview.requestData(data);
}
@Override
public void onFail(String error) {
mIview.requestFail(error);
}
});
}
/**
* delete
*/
@Override
public void deleteRequest(String url, Class clazz) {
model.requestDelete(url, clazz, new MyCallBack() {
@Override
public void onSuccess(Object data) {
mIview.requestData(data);
}
@Override
public void onFail(String error) {
mIview.requestFail(error);
}
});
}
/**
* put
*/
@Override
public void putRequest(String url, Map<String, String> map, Class clazz) {
model.requestPut(url, map, clazz, new MyCallBack() {
@Override
public void onSuccess(Object data) {
mIview.requestData(data);
}
@Override
public void onFail(String error) {
mIview.requestFail(error);
}
});
}
/**
* 上传头像
*/
@Override
public void imagePostRequest(String url, Map<String, String> map, Class clazz) {
model.requestImagePost(url, map, clazz, new MyCallBack() {
@Override
public void onSuccess(Object data) {
mIview.requestData(data);
}
@Override
public void onFail(String error) {
mIview.requestFail(error);
}
});
}
/**
* 上传多张图片
* */
@Override
public void imagesPostRequest(String url, Map<String, Object> map, Class clazz) {
model.imagePost(url, map, clazz, new MyCallBack() {
@Override
public void onSuccess(Object data) {
mIview.requestData(data);
}
@Override
public void onFail(String error) {
mIview.requestFail(error);
}
});
}
public void onDetach() {
if (model != null) {
model = null;
}
if (mIview != null) {
mIview = null;
}
}
}
8.Activity
package com.example.wdshop.mine.activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
import com.example.wdshop.R;
import com.example.wdshop.activity.BaseActivity;
import com.example.wdshop.api.Apis;
import com.example.wdshop.mine.bean.ImageHeadBean;
import com.example.wdshop.mine.bean.InformationBean;
import com.example.wdshop.mine.bean.MineUpdateNameBean;
import com.example.wdshop.mine.bean.MineUpdatePasswordBean;
import com.example.wdshop.presents.PresenterImpl;
import com.example.wdshop.shoppingcart.activity.ParticularsActivity;
import com.example.wdshop.util.ImageFileUtil;
import com.example.wdshop.view.Iview;
import com.facebook.drawee.view.SimpleDraweeView;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
/**
* 个人资料Activity
* */
public class PresonalDataActivity extends BaseActivity implements Iview {
@BindView(R.id.my_profile)
TextView myProfile;
@BindView(R.id.my_profile_text)
TextView myProfileText;
@BindView(R.id.my_profile_simple)
SimpleDraweeView myProfileSimple;
@BindView(R.id.my_nivkname_text)
TextView myNivknameText;
@BindView(R.id.my_nickname)
TextView myNickname;
@BindView(R.id.my_password_text)
TextView myPasswordText;
@BindView(R.id.my_password)
TextView myPassword;
private AlertDialog dialog;
private PresenterImpl presenter;
private TextView camera;
private TextView pick;
private TextView cancel;
private PopupWindow window;
private String path = Environment.getExternalStorageDirectory()
+ "/image.png";
private String file = Environment.getExternalStorageDirectory()
+ "/file.png";
/**
* 加载视图
*/
@Override
protected int getLayoutResId() {
return R.layout.activity_presonal_data;
}
/**
* 加载数据
*/
@Override
protected void initData() {
Intent intent = getIntent();
InformationBean.ResultBean result = (InformationBean.ResultBean) intent.getSerializableExtra("result");
String headPic = result.getHeadPic();
String nickName = result.getNickName();
String password = result.getPassword();
myProfileSimple.setImageURI(Uri.parse(headPic));
myNickname.setText(nickName);
myPassword.setText(password);
setResult(200, intent);
}
/**
* 判断sd卡是否挂载
* */
private boolean hasSdcard() {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
return true;
} else {
return false;
}
}
/**
* 初始化view
*/
@Override
protected void initView(Bundle savedInstanceState) {
presenter = new PresenterImpl(this);
ButterKnife.bind(this);
//加载视图
View view_p = View.inflate(this, R.layout.popupwindow_item, null);
camera = view_p.findViewById(R.id.text_camera);
pick = view_p.findViewById(R.id.text_pick);
cancel = view_p.findViewById(R.id.text_cancel);
//创建PopupWindow
window = new PopupWindow(view_p, ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
//设置焦点
window.setFocusable(true);
//设置背景
window.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
//设置可触摸
window.setTouchable(true);
//打开相机
camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 调取系统相机
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(hasSdcard()){
// 存到sdcard中
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(new File(path)));
//执行
startActivityForResult(intent, 100);
window.dismiss();
}
}
});
//打开相册
pick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//加载相册
Intent intent = new Intent(Intent.ACTION_PICK);
if(hasSdcard()){
//设置图片格式
intent.setType("image/*");
//执行
startActivityForResult(intent, 300);
window.dismiss();
}
}
});
//取消
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
window.dismiss();
}
});
}
@OnClick({R.id.my_profile_text, R.id.my_profile_simple, R.id.my_nickname, R.id.my_password})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.my_profile_text:
break;
case R.id.my_profile_simple:
window.showAtLocation(View.inflate(PresonalDataActivity.this, R.layout.activity_presonal_data, null),
Gravity.BOTTOM, 0, 0);
break;
case R.id.my_nickname:
final AlertDialog.Builder builder = new AlertDialog.Builder(PresonalDataActivity.this);
View viewname = View.inflate(this, R.layout.presonal_alertdalog_name, null);
builder.setView(viewname);
Button update = viewname.findViewById(R.id.updata_btn);
Button cencal = viewname.findViewById(R.id.cancal_btn);
final EditText updateedix = viewname.findViewById(R.id.updata_edix);
//修改
update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = updateedix.getText().toString().trim();
if (name.equals("")) {
Toast.makeText(PresonalDataActivity.this, "昵称不能为空", Toast.LENGTH_SHORT).show();
} else {
Map<String, String> map = new HashMap<>();
map.put("nickName", name);
presenter.putRequest(Apis.URL_UPDATE_NAME_PUT, map, MineUpdateNameBean.class);
myNickname.setText(name);
}
dialog.dismiss();
}
});
//取消
cencal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(PresonalDataActivity.this, "取消", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
dialog = builder.create();
dialog.show();
break;
case R.id.my_password:
final AlertDialog.Builder pbuilder = new AlertDialog.Builder(PresonalDataActivity.this);
View viewpas = View.inflate(this, R.layout.presonal_alertdalog_password, null);
pbuilder.setView(viewpas);
final EditText former_edix = viewpas.findViewById(R.id.former_edix);
final EditText new_edix = viewpas.findViewById(R.id.new_edix);
Button updatep = viewpas.findViewById(R.id.updata_btn);
Button cancelp = viewpas.findViewById(R.id.cancal_btn);
//修改
updatep.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String formerPass = former_edix.getText().toString().trim();
String newPass = new_edix.getText().toString().trim();
if (formerPass.equals("") || newPass.equals("")) {
Toast.makeText(PresonalDataActivity.this, "输入不能为空", Toast.LENGTH_SHORT).show();
}else if(formerPass.equals(newPass)){
Toast.makeText(PresonalDataActivity.this, "两次输入的密码一样", Toast.LENGTH_SHORT).show();
} else {
Map<String, String> map = new HashMap<>();
map.put("oldPwd", formerPass);
map.put("newPwd", newPass);
presenter.putRequest(Apis.URL_UPDATE_PWD_PUT, map, MineUpdatePasswordBean.class);
myPassword.setText(newPass);
dialog.dismiss();
}
}
});
//取消
cancelp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(PresonalDataActivity.this, "取消", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
dialog = pbuilder.create();
dialog.show();
break;
default:
break;
}
}
/**
* 请求成功
*/
@Override
public void requestData(Object o) {
//修改昵称
if (o instanceof MineUpdateNameBean) {
MineUpdateNameBean updateBean = (MineUpdateNameBean) o;
Toast.makeText(PresonalDataActivity.this, updateBean.getMessage(), Toast.LENGTH_SHORT).show();
//修改密码
} else if (o instanceof MineUpdatePasswordBean) {
MineUpdatePasswordBean passwordBean = (MineUpdatePasswordBean) o;
Toast.makeText(PresonalDataActivity.this, passwordBean.getMessage(), Toast.LENGTH_SHORT).show();
}else if(o instanceof ImageHeadBean){
ImageHeadBean headBean = (ImageHeadBean) o;
Toast.makeText(PresonalDataActivity.this,headBean.getMessage(),Toast.LENGTH_SHORT).show();
}
}
/**
* 请求失败
*/
@Override
public void requestFail(String error) {
Toast.makeText(PresonalDataActivity.this, error, Toast.LENGTH_SHORT).show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//相机
if(requestCode == 100 && resultCode == RESULT_OK){
//调取裁剪功能
Intent intent = new Intent("com.android.camera.action.CROP");
//将图片设置给裁剪
intent.setDataAndType(Uri.fromFile(new File(path)), "image/*");
//设置是否支持裁剪
intent.putExtra("CROP", true);
//设置宽高比
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
//设置显示大小
intent.putExtra("outputX", 50);
intent.putExtra("outputY", 50);
//将图片返回给data
intent.putExtra("return-data", true);
startActivityForResult(intent, 200);
}
//相册
if(requestCode == 300 && resultCode == RESULT_OK){
//获取相册路径
Uri uri = data.getData();
//调取裁剪功能
Intent intent = new Intent("com.android.camera.action.CROP");
//将图片设置给裁剪
intent.setDataAndType(uri, "image/*");
//设置是否支持裁剪
intent.putExtra("CROP", true);
//设置框高比
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
//设置显示大小
intent.putExtra("outputX", 50);
intent.putExtra("outputY", 50);
//将图片返回给data
intent.putExtra("return-data", true);
startActivityForResult(intent, 200);
}
if(requestCode == 200 && resultCode == RESULT_OK){
Bitmap bitmap = data.getParcelableExtra("data");
try {
ImageFileUtil.setBitmap(bitmap,file,50);
}catch (Exception e){
e.printStackTrace();
Toast.makeText(PresonalDataActivity.this,e.getMessage(),Toast.LENGTH_SHORT).show();
}
Map<String,String> map = new HashMap<>();
map.put("image",file);
presenter.imagePostRequest(Apis.URL_MODEIFY_HEAD_PIC,map,ImageHeadBean.class);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
presenter.onDetach();
}
}