本文整理匯總了Java中com.squareup.okhttp.MultipartBuilder.type方法的典型用法代碼示例。如果您正苦於以下問題:Java MultipartBuilder.type方法的具體用法?Java MultipartBuilder.type怎麽用?Java MultipartBuilder.type使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.squareup.okhttp.MultipartBuilder
的用法示例。
在下文中一共展示了MultipartBuilder.type方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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: 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);
}
示例3: constructMultipartBody
import com.squareup.okhttp.MultipartBuilder; //導入方法依賴的package包/類
private
@Nullable
MultipartBuilder constructMultipartBody(
ExecutorToken ExecutorToken,
ReadableArray body,
String contentType,
int requestId) {
MultipartBuilder multipartBuilder = new MultipartBuilder();
multipartBuilder.type(MediaType.parse(contentType));
for (int i = 0, size = body.size(); i < size; i++) {
ReadableMap bodyPart = body.getMap(i);
// Determine part's content type.
ReadableArray headersArray = bodyPart.getArray("headers");
Headers headers = extractHeaders(headersArray, null);
if (headers == null) {
onRequestError(
ExecutorToken,
requestId,
"Missing or invalid header format for FormData part.");
return null;
}
MediaType partContentType = null;
String partContentTypeStr = headers.get(CONTENT_TYPE_HEADER_NAME);
if (partContentTypeStr != null) {
partContentType = MediaType.parse(partContentTypeStr);
// Remove the content-type header because MultipartBuilder gets it explicitly as an
// argument and doesn't expect it in the headers array.
headers = headers.newBuilder().removeAll(CONTENT_TYPE_HEADER_NAME).build();
}
if (bodyPart.hasKey(REQUEST_BODY_KEY_STRING)) {
String bodyValue = bodyPart.getString(REQUEST_BODY_KEY_STRING);
multipartBuilder.addPart(headers, RequestBody.create(partContentType, bodyValue));
} else if (bodyPart.hasKey(REQUEST_BODY_KEY_URI)) {
if (partContentType == null) {
onRequestError(
ExecutorToken,
requestId,
"Binary FormData part needs a content-type header.");
return null;
}
String fileContentUriStr = bodyPart.getString(REQUEST_BODY_KEY_URI);
InputStream fileInputStream =
RequestBodyUtil.getFileInputStream(getReactApplicationContext(), fileContentUriStr);
if (fileInputStream == null) {
onRequestError(
ExecutorToken,
requestId,
"Could not retrieve file for uri " + fileContentUriStr);
return null;
}
multipartBuilder.addPart(headers, RequestBodyUtil.create(partContentType, fileInputStream));
} else {
onRequestError(ExecutorToken, requestId, "Unrecognized FormData part.");
}
}
return multipartBuilder;
}
示例4: constructMultipartBody
import com.squareup.okhttp.MultipartBuilder; //導入方法依賴的package包/類
private
@Nullable
MultipartBuilder constructMultipartBody(
ExecutorToken ExecutorToken,
ReadableArray body,
String contentType,
int requestId) {
MultipartBuilder multipartBuilder = new MultipartBuilder();
multipartBuilder.type(MediaType.parse(contentType));
for (int i = 0, size = body.size(); i < size; i++) {
ReadableMap bodyPart = body.getMap(i);
// Determine part's content type.
ReadableArray headersArray = bodyPart.getArray("headers");
Headers headers = extractHeaders(headersArray, null);
if (headers == null) {
onRequestError(
ExecutorToken,
requestId,
"Missing or invalid header format for FormData part.",
null);
return null;
}
MediaType partContentType = null;
String partContentTypeStr = headers.get(CONTENT_TYPE_HEADER_NAME);
if (partContentTypeStr != null) {
partContentType = MediaType.parse(partContentTypeStr);
// Remove the content-type header because MultipartBuilder gets it explicitly as an
// argument and doesn't expect it in the headers array.
headers = headers.newBuilder().removeAll(CONTENT_TYPE_HEADER_NAME).build();
}
if (bodyPart.hasKey(REQUEST_BODY_KEY_STRING)) {
String bodyValue = bodyPart.getString(REQUEST_BODY_KEY_STRING);
multipartBuilder.addPart(headers, RequestBody.create(partContentType, bodyValue));
} else if (bodyPart.hasKey(REQUEST_BODY_KEY_URI)) {
if (partContentType == null) {
onRequestError(
ExecutorToken,
requestId,
"Binary FormData part needs a content-type header.",
null);
return null;
}
String fileContentUriStr = bodyPart.getString(REQUEST_BODY_KEY_URI);
InputStream fileInputStream =
RequestBodyUtil.getFileInputStream(getReactApplicationContext(), fileContentUriStr);
if (fileInputStream == null) {
onRequestError(
ExecutorToken,
requestId,
"Could not retrieve file for uri " + fileContentUriStr,
null);
return null;
}
multipartBuilder.addPart(headers, RequestBodyUtil.create(partContentType, fileInputStream));
} else {
onRequestError(ExecutorToken, requestId, "Unrecognized FormData part.", null);
}
}
return multipartBuilder;
}
示例5: 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");
}
});
}
示例6: 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;
}