本文整理汇总了Java中com.squareup.okhttp.MultipartBuilder.addFormDataPart方法的典型用法代码示例。如果您正苦于以下问题:Java MultipartBuilder.addFormDataPart方法的具体用法?Java MultipartBuilder.addFormDataPart怎么用?Java MultipartBuilder.addFormDataPart使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.squareup.okhttp.MultipartBuilder
的用法示例。
在下文中一共展示了MultipartBuilder.addFormDataPart方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: asyncMultipartPost
import com.squareup.okhttp.MultipartBuilder; //导入方法依赖的package包/类
private void asyncMultipartPost(String url, StringMap fields, ProgressHandler
progressHandler, String fileName, RequestBody file, CompletionHandler
completionHandler, CancellationHandler cancellationHandler) {
if (this.converter != null) {
url = this.converter.convert(url);
}
final MultipartBuilder mb = new MultipartBuilder();
mb.addFormDataPart("file", fileName, file);
fields.forEach(new Consumer() {
public void accept(String key, Object value) {
mb.addFormDataPart(key, value.toString());
}
});
mb.type(MediaType.parse("multipart/form-data"));
RequestBody body = mb.build();
if (progressHandler != null) {
body = new CountingRequestBody(body, progressHandler, cancellationHandler);
}
asyncSend(new Builder().url(url).post(body), null, completionHandler);
}
示例2: initRequestBody
import com.squareup.okhttp.MultipartBuilder; //导入方法依赖的package包/类
/**
* 初始化Body类型请求参数
* init Body type params
*/
private RequestBody initRequestBody(TreeMap<String , Object> params) {
MultipartBuilder bodyBuilder = new MultipartBuilder().type(MultipartBuilder.FORM);
Set<Map.Entry<String, Object>> entries = params.entrySet();
for (Map.Entry<String, Object> entry : entries) {
String key = entry.getKey();
Object value = entry.getValue();
if (value instanceof File) {
File file = (File) value;
try {
FileNameMap fileNameMap = URLConnection.getFileNameMap();
String mimeType = fileNameMap.getContentTypeFor(file.getAbsolutePath());
XgoLog.w("mimeType::" + mimeType);
bodyBuilder.addFormDataPart(key, file.getName(), RequestBody.create(MediaType.parse(mimeType), file));
} catch (Exception e) {
e.printStackTrace();
XgoLog.e("mimeType is Error !");
}
} else {
XgoLog.w(key + "::" + value);
bodyBuilder.addFormDataPart(key, value.toString());
}
}
return bodyBuilder.build();
}
示例3: multipartPost
import com.squareup.okhttp.MultipartBuilder; //导入方法依赖的package包/类
private Response multipartPost(String url,
StringMap fields,
String name,
String fileName,
RequestBody file,
StringMap headers) throws QiniuException {
final MultipartBuilder mb = new MultipartBuilder();
mb.addFormDataPart(name, fileName, file);
fields.forEach(new StringMap.Consumer() {
@Override
public void accept(String key, Object value) {
mb.addFormDataPart(key, value.toString());
}
});
mb.type(MediaType.parse("multipart/form-data"));
RequestBody body = mb.build();
Request.Builder requestBuilder = new Request.Builder().url(url).post(body);
return send(requestBuilder, headers);
}
示例4: getRequestBody
import com.squareup.okhttp.MultipartBuilder; //导入方法依赖的package包/类
private RequestBody getRequestBody(Map<String, T> formParams) {
MultipartBuilder multiBuilder = new MultipartBuilder().type(MultipartBuilder.FORM);
for (Map.Entry<String,T> entry : formParams.entrySet()) {
String key = entry.getKey();
T value = entry.getValue();
if (value instanceof String) {
multiBuilder.addFormDataPart(key, String.valueOf(value));
}else if (value instanceof File){
multiBuilder.addPart(
Headers.of("Content-Disposition", "form-data;name=\"emoji\"; filename=\"" + key + "\""),
RequestBody.create(MEDIA_TYPE_STREAM, (File) value));
}
}
return multiBuilder.build();
}
示例5: build
import com.squareup.okhttp.MultipartBuilder; //导入方法依赖的package包/类
private void build(final Map<String, String> params, final Map<String, Pair<File, String>> files) {
MultipartBuilder multipartBuilder = new MultipartBuilder().type(MultipartBuilder.FORM);
for (Map.Entry<String, Pair<File, String>> filePart : files.entrySet()) {
Pair<File, String> filePair = filePart.getValue();
File file = filePair.first;
String mediaType = filePair.second;
multipartBuilder.addFormDataPart(
filePart.getKey(),
file.getName(),
RequestBody.create(MediaType.parse(mediaType), file)
);
}
if (params != null) {
for (Map.Entry<String, String> stringPart : params.entrySet()) {
multipartBuilder.addFormDataPart(
stringPart.getKey(),
stringPart.getValue()
);
}
}
mRequestBody = multipartBuilder.build();
}
示例6: upload
import com.squareup.okhttp.MultipartBuilder; //导入方法依赖的package包/类
private void upload() {
if(file==null){
Toast.makeText(this,"请先下载文件",Toast.LENGTH_SHORT).show();
return;
}
progressDialog.setMessage("upload");
progressDialog.show();
progressDialog.setProgress(0);
MultipartBuilder mb = new MultipartBuilder();
mb.type(MultipartBuilder.FORM);
mb.addFormDataPart("user_id", "74");
mb.addFormDataPart("user_head", file.getName(), RequestBody.create(null, file));
RequestBody requestBody = mb.build();
String url="接口地址";
DOkHttp.getInstance().uploadPost2ServerProgress(this,url,requestBody,new DOkHttp.MyCallBack(){
@Override
public void onFailure(Request request, IOException e) {
tv_show.setText(e.getMessage());
}
@Override
public void onResponse(String json) {
progressDialog.dismiss();
tv_show.setText(json);
}
},new DOkHttp.UIchangeListener(){
@Override
public void progressUpdate(long bytesWrite, long contentLength, boolean done) {
int progress= (int) (bytesWrite*100/contentLength);
progressDialog.setProgress(progress);
tv_show.append(progress+"\n");
}
});
}
示例7: perform
import com.squareup.okhttp.MultipartBuilder; //导入方法依赖的package包/类
@SuppressWarnings({"unchecked", "deprecation"})
@Override
public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) {
if (build.getResult().isWorseOrEqualTo(Result.FAILURE)) {
listener.getLogger().println("HTTP POST: Skipping because of FAILURE");
return true;
}
List<Run.Artifact> artifacts = build.getArtifacts();
if (artifacts.isEmpty()) {
listener.getLogger().println("HTTP POST: No artifacts to POST");
return true;
}
Descriptor descriptor = getDescriptor();
String url = descriptor.url;
String headers = descriptor.headers;
if (url == null || url.length() == 0) {
listener.getLogger().println("HTTP POST: No URL specified");
return true;
}
try {
MultipartBuilder multipart = new MultipartBuilder();
multipart.type(MultipartBuilder.FORM);
for (Run.Artifact artifact : artifacts) {
multipart.addFormDataPart(artifact.getFileName(), artifact.getFileName(),
RequestBody.create(null, artifact.getFile()));
}
OkHttpClient client = new OkHttpClient();
client.setConnectTimeout(30, TimeUnit.SECONDS);
client.setReadTimeout(60, TimeUnit.SECONDS);
Request.Builder builder = new Request.Builder();
builder.url(url);
builder.header("Job-Name", build.getProject().getName());
builder.header("Build-Number", String.valueOf(build.getNumber()));
builder.header("Build-Timestamp", String.valueOf(build.getTimeInMillis()));
if (headers != null && headers.length() > 0) {
String[] lines = headers.split("\r?\n");
for (String line : lines) {
int index = line.indexOf(':');
builder.header(line.substring(0, index).trim(), line.substring(index + 1).trim());
}
}
builder.post(multipart.build());
Request request = builder.build();
listener.getLogger().println(String.format("---> POST %s", url));
listener.getLogger().println(request.headers());
long start = System.nanoTime();
Response response = client.newCall(request).execute();
long time = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);
listener.getLogger()
.println(String.format("<--- %s %s (%sms)", response.code(), response.message(), time));
listener.getLogger().println(response.body().string());
} catch (Exception e) {
e.printStackTrace(listener.getLogger());
}
return true;
}