ex

Fork of mbed-os-example-mbed5-blinky by mbed-os-examples

Committer:
TMBOY
Date:
Tue Jul 18 16:34:48 2017 +0800
Revision:
45:2aa9f933c8d2
?

Who changed what in which revision?

UserRevisionLine numberNew 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.builder.PostMultipartBuilder;
TMBOY 45:2aa9f933c8d2 19
TMBOY 45:2aa9f933c8d2 20 import java.util.LinkedList;
TMBOY 45:2aa9f933c8d2 21 import java.util.List;
TMBOY 45:2aa9f933c8d2 22 import java.util.Map;
TMBOY 45:2aa9f933c8d2 23
TMBOY 45:2aa9f933c8d2 24 import okhttp3.Headers;
TMBOY 45:2aa9f933c8d2 25 import okhttp3.MultipartBody;
TMBOY 45:2aa9f933c8d2 26 import okhttp3.Request;
TMBOY 45:2aa9f933c8d2 27 import okhttp3.RequestBody;
TMBOY 45:2aa9f933c8d2 28
TMBOY 45:2aa9f933c8d2 29 /**
TMBOY 45:2aa9f933c8d2 30 * 用于post multipart请求
TMBOY 45:2aa9f933c8d2 31 * <p>
TMBOY 45:2aa9f933c8d2 32 * Created by guxiuzhong@baidu.com on 2017/5/15.
TMBOY 45:2aa9f933c8d2 33 */
TMBOY 45:2aa9f933c8d2 34 public class PostMultipartRequest extends OkHttpRequest {
TMBOY 45:2aa9f933c8d2 35 private List<PostMultipartBuilder.Multipart> multiParts;
TMBOY 45:2aa9f933c8d2 36
TMBOY 45:2aa9f933c8d2 37 public PostMultipartRequest(String url,
TMBOY 45:2aa9f933c8d2 38 Object tag,
TMBOY 45:2aa9f933c8d2 39 Map<String, String> params,
TMBOY 45:2aa9f933c8d2 40 Map<String, String> headers,
TMBOY 45:2aa9f933c8d2 41 LinkedList<PostMultipartBuilder.Multipart> multiParts,
TMBOY 45:2aa9f933c8d2 42 int id) {
TMBOY 45:2aa9f933c8d2 43 super(url, tag, params, headers, id);
TMBOY 45:2aa9f933c8d2 44 this.multiParts = multiParts;
TMBOY 45:2aa9f933c8d2 45 }
TMBOY 45:2aa9f933c8d2 46
TMBOY 45:2aa9f933c8d2 47 @Override
TMBOY 45:2aa9f933c8d2 48 protected RequestBody buildRequestBody() {
TMBOY 45:2aa9f933c8d2 49 MultipartBody.Builder builder = new MultipartBody.Builder()
TMBOY 45:2aa9f933c8d2 50 .setType(MultipartBody.FORM);
TMBOY 45:2aa9f933c8d2 51 addParams(builder);
TMBOY 45:2aa9f933c8d2 52 return builder.build();
TMBOY 45:2aa9f933c8d2 53 }
TMBOY 45:2aa9f933c8d2 54
TMBOY 45:2aa9f933c8d2 55 @Override
TMBOY 45:2aa9f933c8d2 56 protected Request buildRequest(RequestBody requestBody) {
TMBOY 45:2aa9f933c8d2 57 return builder.post(requestBody).build();
TMBOY 45:2aa9f933c8d2 58 }
TMBOY 45:2aa9f933c8d2 59
TMBOY 45:2aa9f933c8d2 60 private void addParams(MultipartBody.Builder builder) {
TMBOY 45:2aa9f933c8d2 61 if (multiParts != null && !multiParts.isEmpty()) {
TMBOY 45:2aa9f933c8d2 62 for (int i = 0; i < multiParts.size(); i++) {
TMBOY 45:2aa9f933c8d2 63 PostMultipartBuilder.Multipart part = multiParts.get(i);
TMBOY 45:2aa9f933c8d2 64 builder.addPart(Headers.of("Content-Disposition", "form-data; name=\"" + part.key + "\""),
TMBOY 45:2aa9f933c8d2 65 part.requestBody);
TMBOY 45:2aa9f933c8d2 66 }
TMBOY 45:2aa9f933c8d2 67 }
TMBOY 45:2aa9f933c8d2 68 }
TMBOY 45:2aa9f933c8d2 69 }