MCPcopy Create free account
hub / github.com/FongMi/CatVodSpider / OkRequest

Class OkRequest

app/src/main/java/com/github/catvod/net/OkRequest.java:18–73  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

16import okhttp3.Response;
17
18class OkRequest {
19
20 private final Map<String, String> header;
21 private final Map<String, String> params;
22 private final String method;
23 private final String json;
24 private Request request;
25 private String url;
26
27 OkRequest(String method, String url, Map<String, String> params, Map<String, String> header) {
28 this(method, url, params, null, header);
29 }
30
31 OkRequest(String method, String url, String json, Map<String, String> header) {
32 this(method, url, null, json, header);
33 }
34
35 private OkRequest(String method, String url, Map<String, String> params, String json, Map<String, String> header) {
36 this.url = url;
37 this.json = json;
38 this.method = method;
39 this.params = params;
40 this.header = header;
41 this.buildRequest();
42 }
43
44 private void buildRequest() {
45 Request.Builder builder = new Request.Builder();
46 if (method.equals(OkHttp.GET) && params != null) setParams();
47 if (method.equals(OkHttp.POST)) builder.post(getRequestBody());
48 if (header != null) for (String key : header.keySet()) builder.addHeader(key, header.get(key));
49 request = builder.url(url).build();
50 }
51
52 private RequestBody getRequestBody() {
53 if (!TextUtils.isEmpty(json)) return RequestBody.create(MediaType.get("application/json; charset=utf-8"), json);
54 FormBody.Builder formBody = new FormBody.Builder();
55 if (params != null) for (String key : params.keySet()) formBody.add(key, params.get(key));
56 return formBody.build();
57 }
58
59 private void setParams() {
60 url = url + "?";
61 for (String key : params.keySet()) url = url.concat(key + "=" + params.get(key) + "&");
62 url = Util.substring(url);
63 }
64
65 public OkResult execute(OkHttpClient client) {
66 try (Response res = client.newCall(request).execute()) {
67 return new OkResult(res.code(), res.body().string(), res.headers().toMultimap());
68 } catch (IOException e) {
69 SpiderDebug.log(e);
70 return new OkResult();
71 }
72 }
73}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected