Android客户端
PointApi接口:
/**
* 查询所有图层的点
* Map>> :string一个图层,该图层中的所有点
* 返回所有图层集合
*/
@GET("layer/selectAllLayer")
Observable>>>> getAllLayerPoint(
@Query("layers") String[] layers);
ApiManager
public class ApiManager {
private static ApiManager instance;
private PointApi pointApi;
public static ApiManager getInstance() {
if (instance == null) {
instance = new ApiManager();
}
return instance;
}
public PointApi pointApiService() {
if (pointApi == null) {
pointApi = new Retrofit.Builder()
.baseUrl("http://172.16.108.137:10002/")
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(PointApi.class);
}
return pointApi;
}
}
MainActivity中使用:使用了flatMap将图层信息获取到,然后将图层数组作为参数传入到getAllLayerPoint()方法中,后台接收参数,拼接url到数据库查询并返回。
/**
* 先获取图层中所有的点保存到对象中
*/
public void getLayerPoint() {
ApiManager.getInstance()
.pointApiService()
.getLayerInfo()
.flatMap(new Function>,
Observable>>>>>() {
@Override
public Observable>>>> apply(
@NonNull ResponseObj> listResponseObj) throws Exception {
layerList = listResponseObj.data;
Log.e("LayerConfig", "图层获取成功:" + layerList.size());
String[] layers = new String[layerList.size()]; //图层数组
for (int i = 0; i < layerList.size(); i++) {
layers[i] = layerList.get(i).getTableName();
instance.add(new Layer(layerList.get(i), true));
}
return ApiManager.getInstance().pointApiService().getAllLayerPoint(layers);
}
}).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer>>>>() {
@Override
public void accept(@NonNull ResponseObj>>>
mapResponseObj) throws Exception {
allLayerPoint = mapResponseObj.data;
Log.e("LayerConfig", "获取所有信息成功:" + allLayerPoint.size());
}
});
}
服务端:使用mybatis框架
接口LayerMapper
public interface LayerMapper {
List selectLayerList();
// List selectLayerPointList(double latitude0,
// double longitude0,
// double latitude1,
// double longitude1);
List> selectLayerPointList(String sql);
// List selectLayerPointList(String tableName);
// List selectLayerPointList(@Param("tableName")String tableName);
// List selectLayerPointList(Map map1);
List> superManagerSelect(String sql);
}
实现LayerController
注:该方法中@RequestParam String[] layers变量名layers必须和android客户端@Query("layers")中的layers相同。接收到图层数组参数后,拼接url到数据库查询,完整的url地址是:http://127.0.0.1:10002/layer/selectAllLayer?layers=layer_2,layer_1,layer_4,layer_5,layer_3 layers即上面的layers
/**
* 查询所有图层信息和点
*/
@RequestMapping(value = "/selectAllLayer", method = RequestMethod.GET)
public ResponseObj>>> selectAllLayer(
@RequestParam String[] layers) { //注意这里的参数必须和android客户端要查询的参数名一致
try {
Map>> maps = new HashMap<>();
for (String layer : layers) {
ResponseObj>> pointList = getPointList(layer);
maps.put(layer, pointList.data);
}
return new ResponseObj<>(maps, null);
} catch (Exception e) {
e.printStackTrace();
//服务器返回异常
return new ResponseObj(null, new Error("服务器请求异常"));
}
}