| | |
| | | private Integer timeout; |
| | | private TimeUnit timeUnit; |
| | | |
| | | public HttpHandler(Builder builder){ |
| | | public HttpHandler(Builder builder) { |
| | | this.uri = builder.uri; |
| | | this.path = builder.path; |
| | | this.json = builder.json; |
| | |
| | | public String doGet() throws IOException { |
| | | String url = paramsToUrl(uri, path, params, https); |
| | | Request.Builder headerBuilder = new Request.Builder(); |
| | | if (headers != null && headers.size()>0){ |
| | | for (Map.Entry<String, Object> entry : headers.entrySet()){ |
| | | if (headers != null && headers.size() > 0) { |
| | | for (Map.Entry<String, Object> entry : headers.entrySet()) { |
| | | headerBuilder.addHeader(entry.getKey(), String.valueOf(entry.getValue())); |
| | | } |
| | | } |
| | |
| | | public String doPost() throws IOException { |
| | | Request request; |
| | | Request.Builder headerBuilder = new Request.Builder(); |
| | | if (headers != null && headers.size()>0){ |
| | | for (Map.Entry<String, Object> entry : headers.entrySet()){ |
| | | if (headers != null && headers.size() > 0) { |
| | | for (Map.Entry<String, Object> entry : headers.entrySet()) { |
| | | headerBuilder.addHeader(entry.getKey(), String.valueOf(entry.getValue())); |
| | | } |
| | | } |
| | | if (json == null || "".equals(json)){ |
| | | if (json == null || "".equals(json)) { |
| | | FormBody.Builder builder = new FormBody.Builder(); |
| | | for (Map.Entry<String, Object> entry : params.entrySet()){ |
| | | for (Map.Entry<String, Object> entry : params.entrySet()) { |
| | | builder.add(entry.getKey(), String.valueOf(entry.getValue())); |
| | | } |
| | | FormBody body = builder.build(); |
| | | request = headerBuilder |
| | | .url((https?"https://":"http://")+uri+path) |
| | | .url((https ? "https://" : "http://") + uri + path) |
| | | .post(body) |
| | | .build(); |
| | | } else { |
| | | RequestBody body = RequestBody.create(MEDIA_TYPE, json); |
| | | Request.Builder builder = headerBuilder.url((https?"https://":"http://")+uri+path); |
| | | Request.Builder builder = headerBuilder.url((https ? "https://" : "http://") + uri + path); |
| | | builder.header("Content-Type", "application/json"); |
| | | request = builder.post(body).build(); |
| | | |
| | |
| | | StringBuilder res = new StringBuilder(); |
| | | res.append(isHttps ? "https://" : "http://"); |
| | | res.append(uri); |
| | | if (path.length() > 0 && !(path.charAt(0) == '/')){ |
| | | if (path.length() > 0 && !(path.charAt(0) == '/')) { |
| | | res.append("/"); |
| | | } |
| | | res.append(path); |
| | |
| | | } |
| | | ); |
| | | String url = res.toString(); |
| | | if ("&".equals(url.substring(url.length()-1, url.length()))){ |
| | | url = url.substring(0, url.length()-1); |
| | | if ("&".equals(url.substring(url.length() - 1, url.length()))) { |
| | | url = url.substring(0, url.length() - 1); |
| | | } |
| | | return url; |
| | | } |
| | |
| | | * 获取 okHttpClient |
| | | * @return the HttpHandler instance |
| | | */ |
| | | private OkHttpClient getClient(Integer timeout, TimeUnit timeUnit){ |
| | | private OkHttpClient getClient(Integer timeout, TimeUnit timeUnit) { |
| | | return new OkHttpClient |
| | | .Builder() |
| | | .connectTimeout(timeout, timeUnit) |
| | |
| | | * 建造器 |
| | | * @return the HttpHandler instance |
| | | */ |
| | | public HttpHandler build(){ |
| | | if (null == this.uri || "".equals(this.uri)){ |
| | | public HttpHandler build() { |
| | | if (null == this.uri || "".equals(this.uri)) { |
| | | throw new RuntimeException("uri is null"); |
| | | } |
| | | if (this.uri.startsWith("http://")){ |
| | | this.uri = this.uri.substring(6,uri.length()); |
| | | } else if (this.uri.startsWith("https://")){ |
| | | this.uri = this.uri.substring(7,uri.length()); |
| | | if (this.uri.startsWith("http://")) { |
| | | this.uri = this.uri.substring(6, uri.length()); |
| | | } else if (this.uri.startsWith("https://")) { |
| | | this.uri = this.uri.substring(7, uri.length()); |
| | | } |
| | | return new HttpHandler(this); |
| | | } |
| | |
| | | } |
| | | |
| | | public Builder setPath(String path) { |
| | | if (!path.startsWith("/")){ |
| | | if (!path.startsWith("/")) { |
| | | path = "/" + path; |
| | | } |
| | | this.path = path; |