本文整理汇总了Java中com.squareup.okhttp.RequestBody.create方法的典型用法代码示例。如果您正苦于以下问题:Java RequestBody.create方法的具体用法?Java RequestBody.create怎么用?Java RequestBody.create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.squareup.okhttp.RequestBody
的用法示例。
在下文中一共展示了RequestBody.create方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: serialize
import com.squareup.okhttp.RequestBody; //导入方法依赖的package包/类
/**
* Serialize the given Java object into request body according to the object's
* class and the request Content-Type.
*
* @param obj The Java object
* @param contentType The request Content-Type
* @return The serialized request body
* @throws ApiException If fail to serialize the given object
*/
public RequestBody serialize(Object obj, String contentType) throws ApiException {
if (obj instanceof byte[]) {
// Binary (byte array) body parameter support.
return RequestBody.create(MediaType.parse(contentType), (byte[]) obj);
} else if (obj instanceof File) {
// File body parameter support.
return RequestBody.create(MediaType.parse(contentType), (File) obj);
} else if (isJsonMime(contentType)) {
String content;
if (obj != null) {
content = json.serialize(obj);
} else {
content = null;
}
return RequestBody.create(MediaType.parse(contentType), content);
} else {
throw new ApiException("Content type \"" + contentType + "\" is not supported");
}
}
示例2: a
import com.squareup.okhttp.RequestBody; //导入方法依赖的package包/类
private void a(boolean z, String str, String str2, Map<String, Object> map, cw cwVar,
OnFailureCallBack onFailureCallBack) {
try {
RequestBody create;
Builder c = c(str);
if (z) {
create = RequestBody.create(a, a((Map) map));
} else {
create = RequestBody.create(a, c.a((Map) map).toString());
c.removeHeader("Authorization");
}
c.url(str2).post(create);
a(c.build(), cwVar, onFailureCallBack);
} catch (GeneralSecurityException e) {
if (onFailureCallBack != null) {
this.d.post(new bv(this, onFailureCallBack));
}
}
}
示例3: createFileSync
import com.squareup.okhttp.RequestBody; //导入方法依赖的package包/类
public Object createFileSync(String parentId, String filePath) {
String auth = getAuthStr();
File file = new File(filePath);
String mediaType = getFileMime(file);
long fileLength = file.length();
MediaType type = MediaType.parse(mediaType);
RequestBody data = RequestBody.create(type, file);
DriveFile driveFile = null;
Object resultObj = null;
try {
Response<DriveFile> result = RestClient.getInstance().getApiService().
uploadGooogleFile(GOOGLE_DRIVE_MEDIA_PARAM, GOOGLE_DRIVE_FILE_FIELDS, mediaType, fileLength, auth, data).execute();
if (result != null) {
driveFile = result.body();
}
String fileId = null;
if (driveFile != null) {
fileId = driveFile.getId();
}
if (fileId != null && parentId != null) {
driveFile = setFileFolderSync(parentId, fileId, file.getName());
}
resultObj = driveFile;
} catch (Exception e) {
resultObj = e;
Log.e(TAG, "createFileSync", e);
}
return resultObj;
}
示例4: buildRequestBody
import com.squareup.okhttp.RequestBody; //导入方法依赖的package包/类
@Override
public RequestBody buildRequestBody()
{
MultipartBuilder builder = new MultipartBuilder()
.type(MultipartBuilder.FORM);
addParams(builder, params);
if (files != null)
{
RequestBody fileBody = null;
for (int i = 0; i < files.length; i++)
{
Pair<String, File> filePair = files[i];
String fileKeyName = filePair.first;
File file = filePair.second;
String fileName = file.getName();
fileBody = RequestBody.create(MediaType.parse(guessMimeType(fileName)), file);
builder.addPart(Headers.of("Content-Disposition",
"form-data; name=\"" + fileKeyName + "\"; filename=\"" + fileName + "\""),
fileBody);
}
}
return builder.build();
}
示例5: getRequestBody
import com.squareup.okhttp.RequestBody; //导入方法依赖的package包/类
private static RequestBody getRequestBody(HashMap<String, Object> map) {
RequestBody body;
StringBuilder str = new StringBuilder();
Set<String> keySet = map.keySet();
try {
for (String key : keySet) {
if (str.length() > 0) {
str.append('&');
}
str.append(URLEncoder.encode(key, "UTF-8"))
.append('=')
.append(URLEncoder.encode(map.get(key).toString(), "UTF-8"));
}
if (str.length() == 0) {
throw new IllegalStateException("Form encoded body must have at least one part.");
}
body = RequestBody.create(MEDIA_TYPE_URLENCODE, str.toString().getBytes("utf-8"));
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
return body;
}
示例6: exchange
import com.squareup.okhttp.RequestBody; //导入方法依赖的package包/类
/**
* Normal Transaction
*
* Make a normal exchange and receive with {@code withdrawal} address. The exchange pair is
* determined from the {@link CoinType}s of {@code refund} and {@code withdrawal}.
*/
public ShapeShiftNormalTx exchange(AbstractAddress withdrawal, AbstractAddress refund)
throws ShapeShiftException, IOException {
JSONObject requestJson = new JSONObject();
try {
requestJson.put("withdrawal", withdrawal.toString());
requestJson.put("pair", getPair(refund.getType(), withdrawal.getType()));
requestJson.put("returnAddress", refund.toString());
if (apiPublicKey != null) requestJson.put("apiKey", apiPublicKey);
} catch (JSONException e) {
throw new ShapeShiftException("Could not create a JSON request", e);
}
String apiUrl = getApiUrl(NORMAL_TX_API);
RequestBody body = RequestBody.create(MEDIA_TYPE_JSON, requestJson.toString());
Request request = new Request.Builder().url(apiUrl).post(body).build();
ShapeShiftNormalTx reply = new ShapeShiftNormalTx(getMakeApiCall(request));
if (!reply.isError) checkAddress(withdrawal, reply.withdrawal);
return reply;
}
示例7: buildMultipartFormRequest
import com.squareup.okhttp.RequestBody; //导入方法依赖的package包/类
private Request buildMultipartFormRequest(String url, File[] files,
String[] fileKeys, Param[] params) {
params = validateParam(params);
MultipartBuilder builder = new MultipartBuilder()
.type(MultipartBuilder.FORM);
for (Param param : params) {
builder.addPart(Headers.of("Content-Disposition", "form-data; name=\"" + param.key + "\""),
RequestBody.create(null, param.value));
}
if (files != null) {
RequestBody fileBody = null;
for (int i = 0; i < files.length; i++) {
File file = files[i];
String fileName = file.getName();
fileBody = RequestBody.create(MediaType.parse(guessMimeType(fileName)), file);
//TODO 根据文件名设置contentType
builder.addPart(Headers.of("Content-Disposition",
"form-data; name=\"" + fileKeys[i] + "\"; filename=\"" + fileName + "\""),
fileBody);
}
}
RequestBody requestBody = builder.build();
return new Request.Builder()
.url(url)
.post(requestBody)
.build();
}
示例8: post
import com.squareup.okhttp.RequestBody; //导入方法依赖的package包/类
Call post(String url, String json, Callback callback) {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.addHeader("Content-Type","application/json")
.addHeader("Authorization","key=AIzaSyAkeFnKc_r6TJSO7tm5OVnzkbni6dEk4Lw")
.url(url)
.post(body)
.build();
Call call = client.newCall(request);
call.enqueue(callback);
return call;
}
示例9: asyncPost
import com.squareup.okhttp.RequestBody; //导入方法依赖的package包/类
public void asyncPost(String url, byte[] body, int offset, int size, StringMap headers,
ProgressHandler progressHandler, CompletionHandler completionHandler,
CancellationHandler c) {
RequestBody rbody;
RequestBody rbody2;
if (this.converter != null) {
url = this.converter.convert(url);
}
if (body == null || body.length <= 0) {
rbody = RequestBody.create(null, new byte[0]);
} else {
rbody = RequestBody.create(MediaType.parse("application/octet-stream"), body, offset,
size);
}
if (progressHandler != null) {
rbody2 = new CountingRequestBody(rbody, progressHandler, c);
} else {
rbody2 = rbody;
}
asyncSend(new Builder().url(url).post(rbody2), headers, completionHandler);
}
示例10: asyncMultipartPost
import com.squareup.okhttp.RequestBody; //导入方法依赖的package包/类
public void asyncMultipartPost(String url, PostArgs args, ProgressHandler progressHandler,
CompletionHandler completionHandler, CancellationHandler c) {
RequestBody file;
if (args.file != null) {
file = RequestBody.create(MediaType.parse(args.mimeType), args.file);
} else {
file = RequestBody.create(MediaType.parse(args.mimeType), args.data);
}
asyncMultipartPost(url, args.params, progressHandler, args.fileName, file,
completionHandler, c);
}
示例11: createFolderSync
import com.squareup.okhttp.RequestBody; //导入方法依赖的package包/类
public DriveFile createFolderSync(String parentId, String dirName) {
String auth = getAuthStr();
UpdateDriveFile driveFolder = new UpdateDriveFile();
driveFolder.setMimeType(GOOGLE_DRIVE_FOLDER_MIME);
driveFolder.setTitle(dirName);
if (!TextUtils.isEmpty(parentId)) {
List<DriveFolder> parentsList = new ArrayList<DriveFolder>();
DriveFolder parent = new DriveFolder();
parent.setId(parentId);
parentsList.add(parent);
driveFolder.setParents(parentsList);
}
MediaType contentType = MediaType.parse(CONTENT_TYPE_JSON);
String driveFolderStr = gson.toJson(driveFolder);
RequestBody data = RequestBody.create(contentType, driveFolderStr);
DriveFile folderObj = null;
try {
Response<DriveFile> result = RestClient.getInstance().getApiService().
createFolder(CONTENT_TYPE_JSON, GOOGLE_DRIVE_FILE_FIELDS, auth, data).execute();
if (result != null) {
folderObj = result.body();
}
} catch(Exception e) {
Log.e(TAG, "createFolderSync", e);
}
return folderObj;
}
示例12: setFileFolderSync
import com.squareup.okhttp.RequestBody; //导入方法依赖的package包/类
public DriveFile setFileFolderSync(String parentId, String fileId, String fileName) {
UpdateDriveFile updateFileInfo = new UpdateDriveFile();
if (!TextUtils.isEmpty(fileName)) {
updateFileInfo.setTitle(fileName);
}
if (!TextUtils.isEmpty(parentId)) {
List<DriveFolder> parentsList = new ArrayList<DriveFolder>();
DriveFolder parent = new DriveFolder();
parent.setId(parentId);
parentsList.add(parent);
updateFileInfo.setParents(parentsList);
}
String driveFileStr = gson.toJson(updateFileInfo);
MediaType contentType = MediaType.parse(CONTENT_TYPE_JSON);
String auth = getAuthStr();
DriveFile driveFile = null;
try {
RequestBody data = RequestBody.create(contentType, driveFileStr);
Response<DriveFile> result = RestClient.getInstance().getApiService().
setFileFolder(fileId, GOOGLE_DRIVE_FILE_FIELDS, auth, data).execute();
if (result != null) {
driveFile = result.body();
}
} catch (Exception e) {
Log.e(TAG, "setFileFolderSync", e);
}
return driveFile;
}
示例13: getEmptyBody
import com.squareup.okhttp.RequestBody; //导入方法依赖的package包/类
/**
* Creates a empty RequestBody if required by the http method spec, otherwise use null
*/
public static RequestBody getEmptyBody(String method) {
if (method.equals("POST") || method.equals("PUT") || method.equals("PATCH")) {
return RequestBody.create(null, ByteString.EMPTY);
} else {
return null;
}
}
示例14: doPostHttpRequest2
import com.squareup.okhttp.RequestBody; //导入方法依赖的package包/类
/**
* 根据url地址和json数据获取数据
*
* @param url
* @param json
* @return
* @throws IOException
*/
public static String doPostHttpRequest2(String url, String json)
throws IOException {
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, json);
Request request = new Request.Builder().url(url).post(body)
.addHeader("content-type", "application/json").build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) {
System.out.println("服务端错误:" + response);
throw new IOException("Unexpected code " + response);
}
return response.body().string();
}
示例15: post
import com.squareup.okhttp.RequestBody; //导入方法依赖的package包/类
public Response post(String url, byte[] body, StringMap headers, String contentType) throws QiniuException {
RequestBody rbody = null;
if (body != null && body.length > 0) {
MediaType t = MediaType.parse(contentType);
rbody = RequestBody.create(t, body);
}
return post(url, rbody, headers);
}