本文整理汇总了Java中com.squareup.okhttp.MediaType.parse方法的典型用法代码示例。如果您正苦于以下问题:Java MediaType.parse方法的具体用法?Java MediaType.parse怎么用?Java MediaType.parse使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.squareup.okhttp.MediaType
的用法示例。
在下文中一共展示了MediaType.parse方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createFileSync
import com.squareup.okhttp.MediaType; //导入方法依赖的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;
}
示例2: save
import com.squareup.okhttp.MediaType; //导入方法依赖的package包/类
public static long save(String collection, long lastRead, String user, OAuthAuthorization oauth) {
try {
collection = "lists." + Long.parseLong(collection);
} catch (NumberFormatException ignored) {
}
try {
final String auth = generateVerifyCredentialsAuthorizationHeader(TWITTER_VERIFY_CREDENTIALS_JSON, oauth);
JSONObject body = new JSONObject();
JSONObject collectionJson = new JSONObject();
collectionJson.put("id", lastRead);
body.put(collection, collectionJson);
MediaType JSON = MediaType.parse("application/json; charset=utf-8");
Request request = new Request.Builder()
.url(TWEETMARKER_API_URL + "?api_key=" + API_KEY + "&username=" + user)
.addHeader("X-Auth-Service-Provider", TWITTER_VERIFY_CREDENTIALS_JSON)
.addHeader("X-Verify-Credentials-Authorization", auth)
.post(RequestBody.create(JSON, body.toString()))
.build();
final Response response = createHttpClientWithoutSSL().newCall(request).execute();
if (response.isSuccessful()) return lastRead;
} catch (JSONException | IOException | KeyManagementException | NoSuchAlgorithmException e) {
Timber.i(e, "");
}
return -1;
}
示例3: createFolderSync
import com.squareup.okhttp.MediaType; //导入方法依赖的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;
}
示例4: setFileFolderSync
import com.squareup.okhttp.MediaType; //导入方法依赖的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;
}
示例5: doPostHttpRequest2
import com.squareup.okhttp.MediaType; //导入方法依赖的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();
}
示例6: postJson
import com.squareup.okhttp.MediaType; //导入方法依赖的package包/类
private String postJson(String url, String json) throws IOException {
MediaType type = MediaType.parse("application/json; charset=utf-8");
RequestBody body = RequestBody.create(type, json);
Request request = new Request.Builder().url(url).post(body).build();
OkHttpClient client = new OkHttpClient();
client.setConnectTimeout(5, TimeUnit.SECONDS);
client.setReadTimeout(5, TimeUnit.SECONDS);
Response response = client.newCall(request).execute();
return response.code() == 200 ? response.body().string() : null;
}
示例7: sendValuesToSlack
import com.squareup.okhttp.MediaType; //导入方法依赖的package包/类
@Background
protected void sendValuesToSlack() {
if (temperatureValue == 0) {
return;
}
if (humidityValue == 0) {
return;
}
try {
JSONObject jsonObject = new JSONObject();
jsonObject.put("text", Helper.formatTemperature(temperatureValue)
+"\n"
+Helper.formatHumidity(humidityValue));
jsonObject.put("channel", slackChannel);
jsonObject.put("username", slackSenderName);
jsonObject.put("icon_emoji", ":cubimal_chick:");
MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8");
Request request = new Request.Builder()
.url(new URL(slackWebhookUrl))
.post(RequestBody.create(MEDIA_TYPE_JSON, jsonObject.toString()))
.build();
new OkHttpClient().newCall(request).execute();
} catch (IOException | JSONException e) {
e.printStackTrace();
}
temperatureValue = 0;
humidityValue = 0;
}
示例8: Ok2RequestBody
import com.squareup.okhttp.MediaType; //导入方法依赖的package包/类
Ok2RequestBody(alexclin.httplite.RequestBody requestBody, String mediaType, ProgressListener listener) {
this.requestBody = requestBody;
if (TextUtils.isEmpty(mediaType)) {
mediaType = requestBody.contentType();
}
this.mediaType = MediaType.parse(mediaType);
this.listener = listener;
}
示例9: ProgressRequestBody
import com.squareup.okhttp.MediaType; //导入方法依赖的package包/类
ProgressRequestBody(RequestBody real, String mediaType, ProgressListener progressListener) {
this.real = real;
this.progressListener = progressListener;
if (TextUtils.isEmpty(mediaType)) {
this.mediaType = real.contentType();
} else {
this.mediaType = MediaType.parse(mediaType);
}
}
示例10: post
import com.squareup.okhttp.MediaType; //导入方法依赖的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);
}
示例11: flush
import com.squareup.okhttp.MediaType; //导入方法依赖的package包/类
@Override
public void flush() throws TTransportException {
Util.closeQuietly(mResponseBody);
mResponseBody = null;
RequestBody requestBody = new RequestBody() {
@Override
public MediaType contentType() {
if (mHeaders != null && mHeaders.containsKey("Content-Type")) {
return MediaType.parse(mHeaders.get("Content-Type"));
} else {
return MEDIA_TYPE_THRIFT;
}
}
@Override
public void writeTo(BufferedSink sink) throws IOException {
sink.write(mByteStore.getData(), 0, mByteStore.getBytesWritten());
}
};
try {
Request.Builder builder = new Request.Builder()
.url(mUrl)
.post(requestBody);
if (mHeaders != null) {
for (String name : mHeaders.keySet()) {
builder.header(name, mHeaders.get(name));
}
}
Response response = mHttpClient.newCall(builder.build()).execute();
if (response.code() != 200) {
throw new TTransportException("HTTP Response code: " + response.code() + ", message " + response.message());
}
mResponseBody = response.body().byteStream();
} catch (Exception e) {
throw new TTransportException(e);
} finally {
try {
mByteStore.reset();
} catch (IOException ignored) {
}
}
}
示例12: constructMultipartBody
import com.squareup.okhttp.MediaType; //导入方法依赖的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;
}
示例13: getContentType
import com.squareup.okhttp.MediaType; //导入方法依赖的package包/类
private MediaType getContentType(HttpHeaders headers) {
String rawContentType = headers.getFirst("Content-Type");
return (StringUtils.hasText(rawContentType) ? MediaType.parse(rawContentType) : null);
}
示例14: constructMultipartBody
import com.squareup.okhttp.MediaType; //导入方法依赖的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;
}