首先是对应的依赖
implementation 'com.squareup.retrofit2:converter-gson:2.0.1' implementation 'com.squareup.retrofit2:adapter-rxjava:2.0.1' implementation 'io.reactivex:rxandroid:1.1.0'
下面先说post 参数拼接 表单:
public static final String HOME_URL="https://149.122.49.61:170681/apc_if/";//这是借口的域名
@FormUrlEncoded @POST("myActivityList?") //接口的参数 ObservablegetHome(@FieldMap Map map); order是对应的子级bean类, @fileMap 是map集合
Map hah=new HashMap<>(); //这个就是对应接口的参数 page ,sideid,userid
hah.put("page","0");
hah.put("userID",readApp.getAccountInfo().getMember().getUserid());
hah.put("siteId","1");
Retrofit retrofit = new Retrofit.Builder().baseUrl(url).addConverterFactory(GsonConverterFactory.create()).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build();
ApiService apiService = retrofit.create(ApiService.class);
//rx.Observable homes = apiService.getHomes();
rx.Observable homes = apiService.getHome(hah);
homes.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
Log.e("sb", "onNext: "+e.toString());
}
@Override
public void onNext(Order order) {
String createTime = order.getList().get(0).getCreateTime();
String substring = createTime.substring(0, 10);
list = order.getList();
onFinish.OnFinishListener(list);
}
下面先说post 参数拼接 json上传:
public static final String HOME_URLS="http://18s8.122.49.61:105681/app_if/paxy/"; 接口
// @FormUrlEncoded 注意 要加上headers 这个头部 json 第二行是参数 ,第三个@body RequestBody requestBody @Headers({"Content-Type: application/json"}) @POST("unifiedorder?") ObservablegetHomes(@Body RequestBody requestBody);
final Gson gson=new Gson(); 要把集合转成josn去请求
Map hah=new HashMap<>();
hah.put("activityId", fileid);
hah.put("userId",readApp.getAccountInfo().getMember().getUserid());
hah.put("orderNo",orderNo);
hah.put("clientIp",ips);
hah.put("participantCode",participateCode);
hah.put("write0ff",write0ff);
String s = gson.toJson(hah); 这里对应 转成json
// JSONObject jsonObject = new JSONObject(hah);
Loger.e("json","==="+s);
重点是下面这句话
RequestBody body =RequestBody.create(MediaType.parse("application/json;charset=UTF-8"),s);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
ApiServices apiService = retrofit.create(ApiServices.class);
Observable homes = apiService.getHomes(body); 这个地方和表达不同是body就是json
homes.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
Log.e("Fail", "Error: "+e.toString());
}
@Override
public void onNext(Pay pay) {
Log.e("微信支付参数", "onNext: "+gson.toJson(pay.getBody()));
list = new ArrayList<>();
list.add(pay.getBody());
/* list = (List) pay.getBody();*/
onFinish.OnFinishListener(list);
}
});
下面是 git 表单
public static final String HOME_URL="https://1186.122.68.61:1xx81/app_if/"; 接口
@GET("activityDetail?") //参数 Observablegetdetail(@QueryMap Mapmap); 对应的@querymap 集合
Map hah=new HashMap<>(); //对应的参数 三个
hah.put("fileId", fileid);
hah.put("siteId", "1");
hah.put("userId", userid);
Log.e("id", "getUrlq: "+userid );
// RequestBody body =RequestBody.create(MediaType.parse("application/json;charset=UTF-8"),s);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
ApiServiceq apiService = retrofit.create(ApiServiceq.class);
Observable homes = apiService.getdetail(hah); 把集合传过来
homes.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
Log.e("Fail", "Error: "+e.toString());
}
@Override
public void onNext(Details details) {
Log.e("详情页", "onNext: 金额"+details.getCost()+"是否参与"+details.getHasEnter());
list = new ArrayList<>();
list.add(details);
onFinish.OnFinishListener(list);
}