public class MyUtil {
private final RxJavas rxJavas;
private static class Holder {
private static MyUtil myUtil = new MyUtil();
}
public static MyUtil getInstance() {
return Holder.myUtil;
}
private MyUtil(){
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient()
.newBuilder()
.addInterceptor(loggingInterceptor)
.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request =chain.request().newBuilder()
.addHeader(“userId”,“1169”)
.addHeader(“sessionId”,“15625892070121169”)
.build();
return chain.proceed(request);
}
}).build();
Retrofit retrofit =new Retrofit.Builder()
.client(client)
.baseUrl(“http://mobile.bwstudent.com/”)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
rxJavas =retrofit.create(RxJavas.class);
}
public interface UtilBack {
void Success(String json);
void Fail(String json);
}
private UtilBack utilBack;
public void getUtilBack(UtilBack utilBack) {
this.utilBack = utilBack;
}
public void showinfo(File file, final UtilBack utilBack) {
RequestBody body = RequestBody.create(MediaType.parse(“multipart/form-data; charset=utf-8”), file);
MultipartBody.Part image = MultipartBody.Part.createFormData(“image”, file.getName(), body);
rxJavas.getinfo(“small/user/verify/v1/modifyHeadPic”,image)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer() {
@Override
public void onError(Throwable e) {
if (utilBack != null) {
utilBack.Fail(e.getMessage());
}
}
@Override
public void onComplete() {
}
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(ResponseBody responseBody) {
try {
if (utilBack != null) {
utilBack.Success(responseBody.string());
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}