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
- Committer:
- TMBOY
- Date:
- 2017-07-18
- Revision:
- 45:2aa9f933c8d2
File content as of revision 45:2aa9f933c8d2:
/*
* Copyright (c) 2017 Baidu, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.baidu.duer.dcs.http.builder;
import android.net.Uri;
import com.baidu.duer.dcs.http.request.GetRequest;
import com.baidu.duer.dcs.http.request.RequestCall;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
/**
* get请求构建
* <p>
* Created by guxiuzhong@baidu.com on 2017/5/15.
*/
public class GetBuilder extends OkHttpRequestBuilder<GetBuilder> implements HasParamInterface {
@Override
public RequestCall build() {
if (params != null) {
url = appendParams(url, params);
}
return new GetRequest(url, tag, params, headers, id).build();
}
/**
* 添加参数到url上
*
* @param url url 请求的url
* @param params params 参数集合
* @return url 拼接后的url
*/
protected String appendParams(String url, Map<String, String> params) {
if (url == null || params == null || params.isEmpty()) {
return url;
}
Uri.Builder builder = Uri.parse(url).buildUpon();
Set<String> keys = params.keySet();
Iterator<String> iterator = keys.iterator();
while (iterator.hasNext()) {
String key = iterator.next();
builder.appendQueryParameter(key, params.get(key));
}
return builder.build().toString();
}
@Override
public GetBuilder params(Map<String, String> params) {
this.params = params;
return this;
}
@Override
public GetBuilder addParams(String key, String val) {
if (this.params == null) {
params = new LinkedHashMap<>();
}
params.put(key, val);
return this;
}
}
