ex
Fork of mbed-os-example-mbed5-blinky by
dcs-sdk-java-master/app/src/main/java/com/baidu/duer/dcs/http/builder/GetBuilder.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.builder; |
| TMBOY | 45:2aa9f933c8d2 | 17 | |
| TMBOY | 45:2aa9f933c8d2 | 18 | import android.net.Uri; |
| TMBOY | 45:2aa9f933c8d2 | 19 | |
| TMBOY | 45:2aa9f933c8d2 | 20 | import com.baidu.duer.dcs.http.request.GetRequest; |
| TMBOY | 45:2aa9f933c8d2 | 21 | import com.baidu.duer.dcs.http.request.RequestCall; |
| TMBOY | 45:2aa9f933c8d2 | 22 | |
| TMBOY | 45:2aa9f933c8d2 | 23 | import java.util.Iterator; |
| TMBOY | 45:2aa9f933c8d2 | 24 | import java.util.LinkedHashMap; |
| TMBOY | 45:2aa9f933c8d2 | 25 | import java.util.Map; |
| TMBOY | 45:2aa9f933c8d2 | 26 | import java.util.Set; |
| TMBOY | 45:2aa9f933c8d2 | 27 | |
| TMBOY | 45:2aa9f933c8d2 | 28 | /** |
| TMBOY | 45:2aa9f933c8d2 | 29 | * get请求构建 |
| TMBOY | 45:2aa9f933c8d2 | 30 | * <p> |
| TMBOY | 45:2aa9f933c8d2 | 31 | * Created by guxiuzhong@baidu.com on 2017/5/15. |
| TMBOY | 45:2aa9f933c8d2 | 32 | */ |
| TMBOY | 45:2aa9f933c8d2 | 33 | public class GetBuilder extends OkHttpRequestBuilder<GetBuilder> implements HasParamInterface { |
| TMBOY | 45:2aa9f933c8d2 | 34 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 35 | public RequestCall build() { |
| TMBOY | 45:2aa9f933c8d2 | 36 | if (params != null) { |
| TMBOY | 45:2aa9f933c8d2 | 37 | url = appendParams(url, params); |
| TMBOY | 45:2aa9f933c8d2 | 38 | } |
| TMBOY | 45:2aa9f933c8d2 | 39 | return new GetRequest(url, tag, params, headers, id).build(); |
| TMBOY | 45:2aa9f933c8d2 | 40 | } |
| TMBOY | 45:2aa9f933c8d2 | 41 | |
| TMBOY | 45:2aa9f933c8d2 | 42 | /** |
| TMBOY | 45:2aa9f933c8d2 | 43 | * 添加参数到url上 |
| TMBOY | 45:2aa9f933c8d2 | 44 | * |
| TMBOY | 45:2aa9f933c8d2 | 45 | * @param url url 请求的url |
| TMBOY | 45:2aa9f933c8d2 | 46 | * @param params params 参数集合 |
| TMBOY | 45:2aa9f933c8d2 | 47 | * @return url 拼接后的url |
| TMBOY | 45:2aa9f933c8d2 | 48 | */ |
| TMBOY | 45:2aa9f933c8d2 | 49 | protected String appendParams(String url, Map<String, String> params) { |
| TMBOY | 45:2aa9f933c8d2 | 50 | if (url == null || params == null || params.isEmpty()) { |
| TMBOY | 45:2aa9f933c8d2 | 51 | return url; |
| TMBOY | 45:2aa9f933c8d2 | 52 | } |
| TMBOY | 45:2aa9f933c8d2 | 53 | Uri.Builder builder = Uri.parse(url).buildUpon(); |
| TMBOY | 45:2aa9f933c8d2 | 54 | Set<String> keys = params.keySet(); |
| TMBOY | 45:2aa9f933c8d2 | 55 | Iterator<String> iterator = keys.iterator(); |
| TMBOY | 45:2aa9f933c8d2 | 56 | while (iterator.hasNext()) { |
| TMBOY | 45:2aa9f933c8d2 | 57 | String key = iterator.next(); |
| TMBOY | 45:2aa9f933c8d2 | 58 | builder.appendQueryParameter(key, params.get(key)); |
| TMBOY | 45:2aa9f933c8d2 | 59 | } |
| TMBOY | 45:2aa9f933c8d2 | 60 | return builder.build().toString(); |
| TMBOY | 45:2aa9f933c8d2 | 61 | } |
| TMBOY | 45:2aa9f933c8d2 | 62 | |
| TMBOY | 45:2aa9f933c8d2 | 63 | |
| TMBOY | 45:2aa9f933c8d2 | 64 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 65 | public GetBuilder params(Map<String, String> params) { |
| TMBOY | 45:2aa9f933c8d2 | 66 | this.params = params; |
| TMBOY | 45:2aa9f933c8d2 | 67 | return this; |
| TMBOY | 45:2aa9f933c8d2 | 68 | } |
| TMBOY | 45:2aa9f933c8d2 | 69 | |
| TMBOY | 45:2aa9f933c8d2 | 70 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 71 | public GetBuilder addParams(String key, String val) { |
| TMBOY | 45:2aa9f933c8d2 | 72 | if (this.params == null) { |
| TMBOY | 45:2aa9f933c8d2 | 73 | params = new LinkedHashMap<>(); |
| TMBOY | 45:2aa9f933c8d2 | 74 | } |
| TMBOY | 45:2aa9f933c8d2 | 75 | params.put(key, val); |
| TMBOY | 45:2aa9f933c8d2 | 76 | return this; |
| TMBOY | 45:2aa9f933c8d2 | 77 | } |
| TMBOY | 45:2aa9f933c8d2 | 78 | } |
