get 参数拼接 如下接口
localhost:8080/uav/plotting/page/app?pageNum=1&pageSize=10&appId=e3c59e28-2032-4ddf-a762-7cec96f772a4&orgId=65&plottingType=point
@GET("https:/uav/plotting/page/app") Observable<PlotList.DataBean> allPointList(@Query("pageNum") String pageNum, @Query("pageSize") String pageSize, @Query("appId") String appId, @Query("orgId") String orgId, @Query("itemCode") String itemCode, @Query("plottingType") String plottingType);
get 直接拼接url 如下接口
localhost:8080/uav/uav/info/bind/5YSZK9P0020BKU,1581F5FHB229F00201W1
@Header("Authorization") 是我项目的token 认证授权
@GET("/uav/uav/info/bind/{aircraftSn}") Observable<BindOrgBean.DataBean> getBindOrgStutas(@Header("Authorization") String authorization, @Path("aircraftSn") String aircraftSn);
psot 参数拼接 如下接口https://test.uav.gafss.work/uav/uav/info/bind
@Headers({"Content-Type: application/json"}) @POST("/uav/uav/info/bind") Observable<BindOrg.DataBean> bindTissue(@Header("Authorization") String authorization, @Body String params); HashMap<String, Object> hashMap = new HashMap<>(); hashMap.put("appId", appId); hashMap.put("orgId", orgId); hashMap.put("uavNum", uavNum); hashMap.put("itemCode", itemCode); String params = new Gson().toJson(hashMap); return NetWorkManager.getRequest().bindTissue(authorization,params);
delete参数拼接如下接口 localhost:8080/uav/plotting?ids=a667d699-4995-4370-8ab8-908da163047f
@Headers({"Content-Type: application/json"}) @POST("/uav/plotting/update/app") Observable<AddDot> editPoint(@Body String params);
put 参数拼接如下接口口 localhost:8080/ /uav/v2/titem/item/{uav_num}
{
"itemCode": "一风基分质布联",
"inviteCode": "出了放指",
"appId": "自听没个",
"orgId": -2464645433090784
}
@PUT("/uav/v2/titem/item") Observable<AddProject.DataBean> addProject(@Header("Authorization") String authorization,@Body String params);
下面是给服务器传集合 或者 json
public class BindTissueModel implements BindTissueContact.Model { @Override public Observable<Response<ArrayList<BindOrg.DataBean>>> bingTissue(String authorization, String appId, String orgId, String uavNum, String itemCode) { HashMap<String, Object> hashMap = new HashMap<>(); hashMap.put("appId", appId); hashMap.put("orgId", orgId); hashMap.put("uavNum", uavNum); hashMap.put("itemCode", itemCode); String params = new Gson().toJson(hashMap); return NetWorkManager.getRequest().bindTissue(authorization,params); } @Override public Observable<Response<ArrayList<CutOrgItem.BindDevicesBean>>> cutOrgItem(String authorization, ArrayList<CutOrgItem.BindDevicesBean> list) { HashMap<String, Object> hashMap = new HashMap<>(); hashMap.put("bindDevices", list); String params = new Gson().toJson(hashMap); return NetWorkManager.getRequest().cutOrgItem(authorization,params); } }
接下来是完整的
NetWorkManager.class
package com.bjdj.droneflyV5.net; import static androidx.core.view.OneShotPreDrawListener.add; import androidx.annotation.NonNull; import com.bjdj.droneflyV5.common.FlyApplication; import com.bjdj.droneflyV5.net.interceptor.DownloadInterceptor; import com.bjdj.droneflyV5.net.request.RequestApi; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Objects; import java.util.concurrent.TimeUnit; import kotlin.Pair; import me.jessyan.retrofiturlmanager.RetrofitUrlManager; import okhttp3.Headers; import okhttp3.Interceptor; import okhttp3.OkHttpClient; import okhttp3.Protocol; import okhttp3.Request; import okhttp3.Response; import okhttp3.logging.HttpLoggingInterceptor; import retrofit2.Retrofit; import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory; import retrofit2.converter.gson.GsonConverterFactory; import retrofit2.converter.scalars.ScalarsConverterFactory; /** * Created by Zaifeng on 2018/2/28. * API初始化类 */ public class NetWorkManager { private static NetWorkManager mInstance; private static Retrofit retrofit; private static volatile RequestApi request = null; public static NetWorkManager getInstance() { if (mInstance == null) { synchronized (NetWorkManager.class) { if (mInstance == null) { mInstance = new NetWorkManager(); } } } return mInstance; } /** * 初始化必要对象和参数 */ public void init() { // List<Protocol> protocols = new ArrayList<Protocol>() {{ add(Protocol.HTTP_1_1); // <-- The only protocol used // add(Protocol.HTTP_2); // }}; // 初始化okhttp HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); logging.setLevel(HttpLoggingInterceptor.Level.BODY); // 构建 OkHttpClient 时,将 OkHttpClient.Builder() 传入 with() 方法,进行初始化配置 OkHttpClient client = RetrofitUrlManager.getInstance() .with(new OkHttpClient.Builder()) .readTimeout(40, TimeUnit.SECONDS) .addInterceptor(logging) .build(); Gson gson = new GsonBuilder() .setLenient() .create(); // 初始化Retrofit retrofit = new Retrofit.Builder() .client(client) // .baseUrl(RequestApi.HOST) .baseUrl(Objects.requireNonNull(FlyApplication.getInstance().getApiHost().getHost())) .addConverterFactory(ScalarsConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create(gson)) .build(); //自定义OkHttpClient对象 // GsonConverterFactory.create(gson)) // RxHttpPlugins.init(client) // .setDebug(true,true,5) ; //是否开启调试模式、是否分段打印、打印json数据缩进空格数量 // .setCache(File, long,CacheMode) //配置缓存目录,最大size及缓存模式 // .setExcludeCacheKeys(String...) //设置一些key,不参与cacheKey的组拼 // .setResultDecoder(Function) //设置数据解密/解码器,非必须 // .setConverter(IConverter) //设置全局的转换器,非必须 // .setOnParamAssembly(Consumer); //设置公共参数/请求头回调 } public static RequestApi getRequest() { if (request == null) { synchronized (RequestApi.class) { request = retrofit.create(RequestApi.class); } } return request; } }
/** * 绑定设备 */ @Headers({"Content-Type: application/json"}) @POST("/uav/uav/info/bind") Observable<Response<ArrayList<BindOrg.DataBean>>> bindTissue(@Header("Authorization") String authorization, @Body String params); 创建接口 view层 public interface BindTissueContact { interface Model { Observable<Response<ArrayList<BindOrg.DataBean>>> bingTissue(String authorization, String appId, String orgId, String uavNum, String itemCode); } interface View extends BaseView { @Override void showLoading(); @Override void hideLoading(); @Override void onError(String errMessage); void getBindTissue(Response<ArrayList<BindOrg.DataBean>> bindTissue); } interface Presenter { void bingTissue(String authorization, String appId, String orgId, String uavNum, String itemCode); } } 创建model层 public class BindTissueModel implements BindTissueContact.Model { @Override public Observable<Response<ArrayList<BindOrg.DataBean>>> bingTissue(String authorization, String appId, String orgId, String uavNum, String itemCode) { HashMap<String, Object> hashMap = new HashMap<>(); hashMap.put("appId", appId); hashMap.put("orgId", orgId); hashMap.put("uavNum", uavNum); hashMap.put("itemCode", itemCode); String params = new Gson().toJson(hashMap); return NetWorkManager.getRequest().bindTissue(authorization,params); } } 创建p层 public class BindTissuePresenter implements BindTissueContact.Presenter { private final BindTissueModel bindTissueModel; private final BindTissueContact.View view; public BindTissuePresenter(BindTissueModel bindTissueModel, BindTissueContact.View view) { this.bindTissueModel = bindTissueModel; this.view = view; } @Override public void bingTissue(String authorization, String appId, String orgId, String uavNum, String itemCode) { bindTissueModel.bingTissue(authorization,appId,orgId,uavNum,itemCode) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer<Response<ArrayList<BindOrg.DataBean>>>() { @Override public void onSubscribe(Disposable d) { view.showLoading(); } @Override public void onNext(Response<ArrayList<BindOrg.DataBean>> orgProject) { view.getBindTissue(orgProject); } @Override public void onError(Throwable e) { view.onError(e.getMessage()); } @Override public void onComplete() { view.hideLoading(); } }); } } 接下来就是 activity 或者 fragment 实现view 层 接口 public class BindOrgActivity extends AppCompatActivity implements BindTissueContact.View { public static final String TAG = "BindOrgActivity"; private BindTissuePresenter bindTissuePresenter; private String accessToken; private String aircraftSn, uavNum, isLogin; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); RxBarTool.hideStatusBar(this); setContentView(R.layout.activity_bind_org); initview(); initData(); } private void initview() { } private void initData() { bindTissuePresenter = new BindTissuePresenter(new BindTissueModel(), this); bindTissuePresenter.bingTissue(accessToken, appId, orgId, aircraftSn, itemCode); } @Override public <T> AutoDisposeConverter<T> bindAutoDispose() { return null; } @Override public void showLoading() { } @Override public void hideLoading() { } @Override public void onError(String errMessage) { } /* * 绑定组织 * */ @SuppressLint("ResourceAsColor") @Override public void getBindTissue(Response<ArrayList<BindOrg.DataBean>> bindTissue) { if (bindTissue.getCode() == 200) { Toast.makeText(this, "绑定成功", Toast.LENGTH_SHORT).show(); status.setText("已绑定"); status.setTextColor(R.color.black); } } }
BaseView
public interface BaseView { /** * 显示加载中 */ void showLoading(); /** * 隐藏加载 */ void hideLoading(); /** * 数据获取失败 * @param errMessage */ void onError(String errMessage); /** * 绑定Android生命周期 防止RxJava内存泄漏 * * @param <T> * @return */ <T> AutoDisposeConverter<T> bindAutoDispose(); }