首先是代码依赖
implementation 'com.squareup.okhttp3:okhttp:3.10.0'
implementation 'com.facebook.fresco:fresco:1.13.0'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.facebook.fresco:animated-gif:1.13.0'
implementation 'com.hjm:BottomTabBar:1.1.1'
implementation 'com.github.bumptech.glide:glide:4.9.0'
implementation 'io.reactivex:rxjava:1.3.0'
//retrofit
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.6.0'
//implementation 'io.reactivex:rxandroid:1.2.1'
// implementation 'io.reactivex:rxjava:1.3.0'
implementation 'com.hwangjr.rxbus:rxbus:1.0.6'//指定结果返回线程
implementation 'com.squareup.retrofit2:adapter-rxjava:2.0.2'
//implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
// implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0'
//noinspection GradleCompatible
//主要用到
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.6.0'
implementation 'com.hwangjr.rxbus:rxbus:1.0.6'//指定结果返回线程
implementation 'com.squareup.retrofit2:adapter-rxjava:2.0.2'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
//接下来接口代码
//定义网络请求类型
package com.wd.weidologin.okutil;
import java.util.Map;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.http.Body;
import retrofit2.http.FieldMap;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.Header;
import retrofit2.http.HeaderMap;
import retrofit2.http.Headers;
import retrofit2.http.Multipart;
import retrofit2.http.POST;
import retrofit2.http.Part;
import retrofit2.http.QueryMap;
import retrofit2.http.Url;
import rx.Observable;
public interface iAiService {
/**
* json入参
*/
@Headers({"Content-Type: application/json;charset=UTF-8"})
@POST
Observable<ResponseBody> requestByPostBody(@Url String url
, @HeaderMap Map<String, String> Headmap
, @Body RequestBody body);
@POST
@Multipart
Observable<ResponseBody> postMoreImage(@Url String url
, @HeaderMap Map<String, String> Headmap
, @QueryMap Map<String, String> map
, @Part MultipartBody.Part[] parts);
@POST
@Multipart
Observable<ResponseBody> postMoreVoice(@Url String url
, @HeaderMap Map<String, String> Headmap
, @QueryMap Map<String, String> map
, @Part MultipartBody.Part[] parts);
}
//接下来是实现类负责请求等逻辑
package com.wd.weidologin.okutil;
import android.util.Log;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.HeaderMap;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
public class okRE {
private iAiService iAiService;
public okRE() {
init();
}
public static class okPP{
public static okRE insancetan = new okRE();
}
public static okRE getInstance(){
return okPP.insancetan;
}
private void init() {
OkHttpClient.Builder builder = new OkHttpClient.Builder()
.connectTimeout(60, TimeUnit.SECONDS)
.readTimeout(60,TimeUnit.SECONDS)
.writeTimeout(60,TimeUnit.SECONDS)
.retryOnConnectionFailure(true);
OkHttpClient client = builder.build();
Retrofit builder1 = new Retrofit.Builder()
.client(client)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.baseUrl("这里是接口")
.build();
iAiService = builder1.create(iAiService.class);
}
/**
* Post请求,作用于医生端注册
*/
public void requestByPostBody(String url, String json, Map<String, String> Headmap, NetCallBack netCallBack) {
if (Headmap == null){
Headmap = new HashMap<>();
}
RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json);
iAiService.requestByPostBody(url,Headmap,body)
//指定请求线程
.subscribeOn(Schedulers.io())
//指定结果返回线程
.observeOn(AndroidSchedulers.mainThread())
.subscribe(getObserver(netCallBack));
}
//多图片上传
public void postMoreImage(String url, Map<String, String> Headmap,Map<String, String> params, List<File> list,NetCallBack netCallBack) {
MultipartBody.Part[] parts = new MultipartBody.Part[list.size()];
int index = 0;
for (File file : list) {
RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
MultipartBody.Part filePart = MultipartBody.Part.createFormData("picture", file.getName(), requestBody);
parts[index] = filePart;
index++;
}
iAiService.postMoreImage(url, Headmap,params, parts)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(getObserver(netCallBack));
}
//多图片上传
public void postMoreVoice(String url, Map<String, String> Headmap,Map<String, String> params, List<File> list,NetCallBack netCallBack) {
MultipartBody.Part[] parts = new MultipartBody.Part[list.size()];
int index = 0;
for (File file : list) {
RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
MultipartBody.Part filePart = MultipartBody.Part.createFormData("content", file.getName(), requestBody);
parts[index] = filePart;
index++;
}
iAiService.postMoreVoice(url, Headmap,params, parts)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(getObserver(netCallBack));
}
private Observer getObserver(final NetCallBack netCallBack){
Observer observer = new Observer<ResponseBody>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
netCallBack.onFail(e.getMessage().toString());
}
@Override
public void onNext(ResponseBody responseBody) {
try {
String string = responseBody.string();
netCallBack.onSuccess(string);
} catch (IOException e) {
e.printStackTrace();
}
}
};
return observer;
}
public interface NetCallBack{
void onSuccess(String string);
void onFail(String string);
}
}
//调用方法
正常如参数我就不多说了举一个@RequestBody入参吧
private String params;
private Map<String, String> headerMap;
//如果要添加头参数就用map
headerMap = new HashMap<>();
headerMap.put("userId","6");
headerMap.put("sessionId","15639712684306");
//RequestBody格式
JSONObject ClientKey = new JSONObject();
try {
ClientKey.put("phone", "18810857699");
ClientKey.put("pwd", s);
params = String.valueOf(ClientKey);
} catch (JSONException e) {
e.printStackTrace();
}
okRE.getInstance().requestByPostBody(url, params, ”headerMap“这个是头参数没有就设置为null, new okRE.NetCallBack() {
@Override
public void onSuccess(String string) {
Toast.makeText(MainActivity.this, string+"", Toast.LENGTH_SHORT).show();
/*SickCircleBean collectionBean = new Gson().fromJson(string, SickCircleBean.class);
nt resultID = collectionBean.getResult();
// Toast.makeText(PublishCircleActivity.this, ""+collectionBean.getMessage(), Toast.LENGTH_SHORT).show();
UploadingImage(resultID);//发表相关图片*/
}
@Override
public void onFail(String string) {
Toast.makeText(MainActivity.this, string+"", Toast.LENGTH_SHORT).show();
// Log.i("发表病友圈错误日志", "onSuccess:错误 "+string);
}
});
感谢观看希望对你有所帮助