ex
Fork of mbed-os-example-mbed5-blinky by
dcs-sdk-java-master/app/src/main/java/com/baidu/duer/dcs/http/request/OkHttpRequest.java@45:2aa9f933c8d2, 2017-07-18 (annotated)
- Committer:
- TMBOY
- Date:
- Tue Jul 18 16:34:48 2017 +0800
- Revision:
- 45:2aa9f933c8d2
?
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| TMBOY | 45:2aa9f933c8d2 | 1 | /* |
| TMBOY | 45:2aa9f933c8d2 | 2 | * Copyright (c) 2017 Baidu, Inc. All Rights Reserved. |
| TMBOY | 45:2aa9f933c8d2 | 3 | * |
| TMBOY | 45:2aa9f933c8d2 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| TMBOY | 45:2aa9f933c8d2 | 5 | * you may not use this file except in compliance with the License. |
| TMBOY | 45:2aa9f933c8d2 | 6 | * You may obtain a copy of the License at |
| TMBOY | 45:2aa9f933c8d2 | 7 | * |
| TMBOY | 45:2aa9f933c8d2 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| TMBOY | 45:2aa9f933c8d2 | 9 | * |
| TMBOY | 45:2aa9f933c8d2 | 10 | * Unless required by applicable law or agreed to in writing, software |
| TMBOY | 45:2aa9f933c8d2 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| TMBOY | 45:2aa9f933c8d2 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| TMBOY | 45:2aa9f933c8d2 | 13 | * See the License for the specific language governing permissions and |
| TMBOY | 45:2aa9f933c8d2 | 14 | * limitations under the License. |
| TMBOY | 45:2aa9f933c8d2 | 15 | */ |
| TMBOY | 45:2aa9f933c8d2 | 16 | package com.baidu.duer.dcs.http.request; |
| TMBOY | 45:2aa9f933c8d2 | 17 | |
| TMBOY | 45:2aa9f933c8d2 | 18 | import com.baidu.duer.dcs.http.callback.DcsCallback; |
| TMBOY | 45:2aa9f933c8d2 | 19 | import com.baidu.duer.dcs.http.exceptions.Exceptions; |
| TMBOY | 45:2aa9f933c8d2 | 20 | |
| TMBOY | 45:2aa9f933c8d2 | 21 | import java.util.Map; |
| TMBOY | 45:2aa9f933c8d2 | 22 | |
| TMBOY | 45:2aa9f933c8d2 | 23 | import okhttp3.Headers; |
| TMBOY | 45:2aa9f933c8d2 | 24 | import okhttp3.Request; |
| TMBOY | 45:2aa9f933c8d2 | 25 | import okhttp3.RequestBody; |
| TMBOY | 45:2aa9f933c8d2 | 26 | |
| TMBOY | 45:2aa9f933c8d2 | 27 | /** |
| TMBOY | 45:2aa9f933c8d2 | 28 | * 请求基类 |
| TMBOY | 45:2aa9f933c8d2 | 29 | * <p> |
| TMBOY | 45:2aa9f933c8d2 | 30 | * Created by guxiuzhong@baidu.com on 2017/5/15. |
| TMBOY | 45:2aa9f933c8d2 | 31 | */ |
| TMBOY | 45:2aa9f933c8d2 | 32 | public abstract class OkHttpRequest { |
| TMBOY | 45:2aa9f933c8d2 | 33 | // url |
| TMBOY | 45:2aa9f933c8d2 | 34 | protected String url; |
| TMBOY | 45:2aa9f933c8d2 | 35 | // 用于取消请求的tag |
| TMBOY | 45:2aa9f933c8d2 | 36 | protected Object tag; |
| TMBOY | 45:2aa9f933c8d2 | 37 | // 请求参数 |
| TMBOY | 45:2aa9f933c8d2 | 38 | protected Map<String, String> params; |
| TMBOY | 45:2aa9f933c8d2 | 39 | // 请求header |
| TMBOY | 45:2aa9f933c8d2 | 40 | protected Map<String, String> headers; |
| TMBOY | 45:2aa9f933c8d2 | 41 | // 请求的标识id |
| TMBOY | 45:2aa9f933c8d2 | 42 | protected int id; |
| TMBOY | 45:2aa9f933c8d2 | 43 | // okhttp的请求构建类 |
| TMBOY | 45:2aa9f933c8d2 | 44 | protected Request.Builder builder = new Request.Builder(); |
| TMBOY | 45:2aa9f933c8d2 | 45 | |
| TMBOY | 45:2aa9f933c8d2 | 46 | protected OkHttpRequest(String url, Object tag, |
| TMBOY | 45:2aa9f933c8d2 | 47 | Map<String, String> params, |
| TMBOY | 45:2aa9f933c8d2 | 48 | Map<String, String> headers, |
| TMBOY | 45:2aa9f933c8d2 | 49 | int id) { |
| TMBOY | 45:2aa9f933c8d2 | 50 | this.url = url; |
| TMBOY | 45:2aa9f933c8d2 | 51 | this.tag = tag; |
| TMBOY | 45:2aa9f933c8d2 | 52 | this.params = params; |
| TMBOY | 45:2aa9f933c8d2 | 53 | this.headers = headers; |
| TMBOY | 45:2aa9f933c8d2 | 54 | this.id = id; |
| TMBOY | 45:2aa9f933c8d2 | 55 | if (url == null) { |
| TMBOY | 45:2aa9f933c8d2 | 56 | Exceptions.illegalArgument("url can not be null."); |
| TMBOY | 45:2aa9f933c8d2 | 57 | } |
| TMBOY | 45:2aa9f933c8d2 | 58 | initBuilder(); |
| TMBOY | 45:2aa9f933c8d2 | 59 | } |
| TMBOY | 45:2aa9f933c8d2 | 60 | |
| TMBOY | 45:2aa9f933c8d2 | 61 | /** |
| TMBOY | 45:2aa9f933c8d2 | 62 | * 初始化okhttp的请求builder |
| TMBOY | 45:2aa9f933c8d2 | 63 | */ |
| TMBOY | 45:2aa9f933c8d2 | 64 | private void initBuilder() { |
| TMBOY | 45:2aa9f933c8d2 | 65 | builder.url(url).tag(tag); |
| TMBOY | 45:2aa9f933c8d2 | 66 | appendHeaders(); |
| TMBOY | 45:2aa9f933c8d2 | 67 | } |
| TMBOY | 45:2aa9f933c8d2 | 68 | |
| TMBOY | 45:2aa9f933c8d2 | 69 | /** |
| TMBOY | 45:2aa9f933c8d2 | 70 | * 拼接请求的Header |
| TMBOY | 45:2aa9f933c8d2 | 71 | */ |
| TMBOY | 45:2aa9f933c8d2 | 72 | protected void appendHeaders() { |
| TMBOY | 45:2aa9f933c8d2 | 73 | Headers.Builder headerBuilder = new Headers.Builder(); |
| TMBOY | 45:2aa9f933c8d2 | 74 | if (headers == null || headers.isEmpty()) { |
| TMBOY | 45:2aa9f933c8d2 | 75 | return; |
| TMBOY | 45:2aa9f933c8d2 | 76 | } |
| TMBOY | 45:2aa9f933c8d2 | 77 | for (String key : headers.keySet()) { |
| TMBOY | 45:2aa9f933c8d2 | 78 | headerBuilder.add(key, headers.get(key)); |
| TMBOY | 45:2aa9f933c8d2 | 79 | } |
| TMBOY | 45:2aa9f933c8d2 | 80 | builder.headers(headerBuilder.build()); |
| TMBOY | 45:2aa9f933c8d2 | 81 | } |
| TMBOY | 45:2aa9f933c8d2 | 82 | |
| TMBOY | 45:2aa9f933c8d2 | 83 | public int getId() { |
| TMBOY | 45:2aa9f933c8d2 | 84 | return id; |
| TMBOY | 45:2aa9f933c8d2 | 85 | } |
| TMBOY | 45:2aa9f933c8d2 | 86 | |
| TMBOY | 45:2aa9f933c8d2 | 87 | public RequestCall build() { |
| TMBOY | 45:2aa9f933c8d2 | 88 | return new RequestCall(this); |
| TMBOY | 45:2aa9f933c8d2 | 89 | } |
| TMBOY | 45:2aa9f933c8d2 | 90 | |
| TMBOY | 45:2aa9f933c8d2 | 91 | public Request generateRequest(DcsCallback dcsCallback) { |
| TMBOY | 45:2aa9f933c8d2 | 92 | RequestBody requestBody = buildRequestBody(); |
| TMBOY | 45:2aa9f933c8d2 | 93 | RequestBody wrappedRequestBody = wrapRequestBody(requestBody, dcsCallback); |
| TMBOY | 45:2aa9f933c8d2 | 94 | Request request = buildRequest(wrappedRequestBody); |
| TMBOY | 45:2aa9f933c8d2 | 95 | return request; |
| TMBOY | 45:2aa9f933c8d2 | 96 | } |
| TMBOY | 45:2aa9f933c8d2 | 97 | |
| TMBOY | 45:2aa9f933c8d2 | 98 | protected RequestBody wrapRequestBody(RequestBody requestBody, final DcsCallback dcsCallback) { |
| TMBOY | 45:2aa9f933c8d2 | 99 | return requestBody; |
| TMBOY | 45:2aa9f933c8d2 | 100 | } |
| TMBOY | 45:2aa9f933c8d2 | 101 | |
| TMBOY | 45:2aa9f933c8d2 | 102 | protected abstract RequestBody buildRequestBody(); |
| TMBOY | 45:2aa9f933c8d2 | 103 | |
| TMBOY | 45:2aa9f933c8d2 | 104 | protected abstract Request buildRequest(RequestBody requestBody); |
| TMBOY | 45:2aa9f933c8d2 | 105 | } |
