代理第三方接口方式

one

package com.qc.jy.service.impl;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import com.qc.jy.mapper.EventInformationMapper;
import com.qc.jy.pojo.EventInformation;
import com.qc.jy.pojo.Result;
import com.qc.jy.service.EventInformationService;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;

@Service
public class EventInformationServiceImpl implements EventInformationService {
    @Autowired
    private EventInformationMapper eventInformationMapper;

    @Override
    public Result<EventInformation> getEventList(String startTime, String endTime, int pageNo, int pageSize) throws IOException {
        String url = "";//地址
        HashMap param = new HashMap();
        param.put("startTime",startTime);
        param.put("endTime",endTime);
        param.put("pageNo", pageNo);
        param.put("pageSize",pageSize);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), new Gson().toJson(param));
        Response post = post(url, body);
        String s =  post.body().string();
        return new Gson().fromJson(s,new TypeToken<Result<EventInformation>>(){}.getType());
    }

    public  Response post(String url, RequestBody formBody) throws IOException {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request.Builder builder = new Request.Builder().url(url).post(formBody);
        Request request = builder.build();
        return okHttpClient.newCall(request).execute();
    }
}

two

package com.shqc.riverskeeper.carlink.util;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.shqc.riverskeeper.carlink.pojo.Gps51Daily;
import com.shqc.riverskeeper.carlink.pojo.Gps51Position;
import com.shqc.riverskeeper.carlink.pojo.Gps51Record;
import com.shqc.riverskeeper.carlink.pojo.Gps51Tk;
import com.shqc.riverskeeper.master.util.DateUtils;
import com.shqc.riverskeeper.master.util.FileUtils;
import com.shqc.riverskeeper.master.util.MD5Utils;
import okhttp3.*;
import org.springframework.util.StringUtils;

import java.io.File;
import java.util.*;

public class Gps51Utils {

    public static String TK;

    public static void login() throws Exception {
        String url = "" +//地址
                "?action=login";
        HashMap<String, String> param = new HashMap<>();
        param.put("type", "USER");
        param.put("from", "web");
        param.put("username", "");
        param.put("password", MD5Utils.Str2MD5(""));
        param.put("browser", "Chrome/104.0.0.0");
        Request request = new Request.Builder()
                .url(url)
                .post(RequestBody.create(
                        MediaType.parse("application/json"),
                        new Gson().toJson(param)
                ))
                .build();
        Call call = new OkHttpClient().newCall(request);
        try (Response response = call.execute()) {
            String s = response.body().string();
            Gps51Tk result = new Gson().fromJson(s, Gps51Tk.class);
            if (result.getStatus() == 0) {
                TK = result.getToken();
            }
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    public static List<Gps51Record> trajectory(String sim,
                                               String beginTime,
                                               String endTime) {
        String url = "" +//地址
                "?action=querytracks" +
                "&token=" + TK;
        HashMap<String, Object> param = new HashMap<>();
        param.put("deviceid", Long.parseLong(sim));
        param.put("begintime", beginTime);
        param.put("endtime", endTime);
        Request request = new Request.Builder()
                .url(url)
                .post(RequestBody.create(
                        MediaType.parse("application/json"),
                        new Gson().toJson(param)
                ))
                .build();
        Call call = new OkHttpClient().newCall(request);
        try (Response response = call.execute()) {
            String s = response.body().string();
            Gps51Record.Result result = new Gson().fromJson(s, Gps51Record.Result.class);
            if (result.getStatus() != 0) {
                throw new RuntimeException(result.getCause());
            }
            return result.getRecords() != null ? result.getRecords() : Collections.emptyList();
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    public static List<Gps51Position> position(List<String> sims) {
        String url = "" +//地址
                "?action=lastposition" +
                "&token=" + TK;
        HashMap<String, Object> param = new HashMap<>();
        param.put("deviceids", sims);
        param.put("lastquerypositiontime", 0);
        Request request = new Request.Builder()
                .url(url)
                .post(RequestBody.create(
                        MediaType.parse("application/json"),
                        new Gson().toJson(param)
                ))
                .build();
        Call call = new OkHttpClient().newCall(request);
        try (Response response = call.execute()) {
            String s = response.body().string();
            Gps51Position.Result result = new Gson().fromJson(s, Gps51Position.Result.class);
            if (result.getStatus() != 0) {
                throw new RuntimeException(result.getCause());
            }
            /*File dir = new File(System.getProperty("user.dir"), "cache");
            if (!dir.exists()) {
                boolean success = dir.mkdir();
            }
            File target = new File(dir, "lastquerypositiontime.timestamp");
            FileUtils.write(target, String.valueOf(result.getLastquerypositiontime()));*/
            return result.getRecords() != null ? result.getRecords() : Collections.emptyList();
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    public static Gps51Daily daily(String sim, String date) {
        String url = "" +//地址
                "?action=reportmileagedetail" +
                "&token=" + TK;
        HashMap<String, Object> param = new HashMap<>();
        param.put("deviceid", sim);
        param.put("startday", date);
        param.put("endday", date);
        param.put("offset", 8);
        Request request = new Request.Builder()
                .url(url)
                .post(RequestBody.create(
                        MediaType.parse("application/json"),
                        new Gson().toJson(param)
                ))
                .build();
        Call call = new OkHttpClient().newCall(request);
        try (Response response = call.execute()) {
            String s = response.body().string();
            Gps51Daily.Result result = new Gson().fromJson(s, Gps51Daily.Result.class);
            if (result.getStatus() != 0) {
                throw new RuntimeException(result.getCause());
            }
            if (result.getRecords() != null &&
                    !result.getRecords().isEmpty()) {
                return result.getRecords().get(0);
            }
            return null;
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    public static List<Gps51Daily> dailyTime(String sim, String startTime, String endTime) {
        String url = "" + //地址
                "?action=reportmileagedetail" +
                "&token=" + TK;
        HashMap<String, Object> param = new HashMap<>();
        param.put("deviceid", sim);
        param.put("startday", startTime);
        param.put("endday", endTime);
        param.put("offset", 8);
        Request request = new Request.Builder()
                .url(url)
                .post(RequestBody.create(
                        MediaType.parse("application/json"),
                        new Gson().toJson(param)
                ))
                .build();
        Call call = new OkHttpClient().newCall(request);
        try (Response response = call.execute()) {
            String s = response.body().string();
            Gps51Daily.Result result = new Gson().fromJson(s, Gps51Daily.Result.class);
            if (result.getStatus() != 0) {
                throw new RuntimeException(result.getCause());
            }
            // 如果记录不为 null,则返回记录列表,否则返回一个空列表
            return result.getRecords() != null ? result.getRecords() : new ArrayList<>();
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值