Blame view

app/src/main/java/com/ectrip/checkincar/net/HttpVerifyApi.java 2.88 KB
0fd093a2   李进   初始提交:手持车牌核销应用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  package com.ectrip.checkincar.net;
  
  import com.ectrip.checkincar.core.prefs.AppPrefs;
  import com.ectrip.checkincar.model.VerifyRequest;
  import com.ectrip.checkincar.model.VerifyResult;
  
  import java.util.concurrent.TimeUnit;
  
  import okhttp3.MediaType;
  import okhttp3.OkHttpClient;
  import okhttp3.Request;
  import okhttp3.RequestBody;
  import okhttp3.Response;
  
  /**
   * 真实核销后台:POST {baseUrl}/tdos/consumeOrder_/appedit.do
   * 参数 licensePlate(车牌经 Base64 编码后传输,后台解码还原)。
   * 免登录接口,内部固定系统账号执行订单核销逻辑,核销成功同步更新车牌状态为已入场。
   * 返回 AjaxData{success,info}。
   */
  public class HttpVerifyApi implements VerifyApi {
  
      private static final String PATH = "/tdos/consumeOrder_/appedit.do";
  
      private static final MediaType FORM = MediaType.parse("application/x-www-form-urlencoded; charset=utf-8");
  
      private final OkHttpClient client = new OkHttpClient.Builder()
              .connectTimeout(5, TimeUnit.SECONDS)
              .readTimeout(8, TimeUnit.SECONDS)
              .writeTimeout(8, TimeUnit.SECONDS)
              .build();
  
      @Override
      public VerifyResult verify(VerifyRequest request) throws Exception {
          String base = AppPrefs.getBaseUrl();
          while (base.endsWith("/")) {
              base = base.substring(0, base.length() - 1);
          }
  
          String plate = request.plateNo == null ? "" : request.plateNo.trim();
  
          // 车牌先 Base64 编码再传输,后台解码还原
          String encodedPlate = android.util.Base64.encodeToString(
                  plate.getBytes("UTF-8"), android.util.Base64.NO_WRAP);
  
          // 表单体:licensePlate=Base64(车牌)
          String formBody = "licensePlate=" + java.net.URLEncoder.encode(encodedPlate, "UTF-8");
  
          Request httpRequest = new Request.Builder()
                  .url(base + PATH)
                  .post(RequestBody.create(FORM, formBody))
                  .build();
  
          try (Response response = client.newCall(httpRequest).execute()) {
              String body = response.body() != null ? response.body().string() : "";
              // 打印后台原始响应,便于排查「响应解析失败」类问题
              System.out.println("HttpVerifyApi resp code=" + response.code()
                      + " url=" + base + PATH
                      + " bodyLen=" + body.length()
                      + " body=" + (body.length() > 500 ? body.substring(0, 500) + "..." : body));
              if (!response.isSuccessful()) {
                  return VerifyResult.fail("后台返回 HTTP " + response.code() + ",报文:" + body);
              }
              VerifyResult result = VerifyResult.fromServerJson(body);
              // 后台不回车牌,回退用识别到的车牌展示
              if (result.plateNo.isEmpty()) {
                  result.plateNo = plate;
              }
              return result;
          }
      }
  }