use of com.reprezen.kaizen.oasparser.model3.MediaType in project SalesforceMobileSDK-Android by forcedotcom.
the class RestResponse method consume.
/**
* Fully consume response entity content and closes content stream
* Must be called before returning control to the UI thread
* @throws IOException
*/
public void consume() throws IOException {
if (!consumed && response != null) {
try {
ResponseBody body = response.body();
if (body != null) {
MediaType mType = body.contentType();
responseAsBytes = body.bytes();
responseCharSet = mType == null || mType.charset() == null ? StandardCharsets.UTF_8 : mType.charset();
if (responseAsBytes != null && responseAsBytes.length > 0) {
responseAsString = new String(responseAsBytes, responseCharSet);
}
} else {
responseAsBytes = new byte[0];
responseCharSet = StandardCharsets.UTF_8;
}
} finally {
consumed = true;
response.close();
}
}
}
use of com.reprezen.kaizen.oasparser.model3.MediaType in project SalesforceMobileSDK-Android by forcedotcom.
the class FileRequests method uploadFile.
/**
* Build a request that can upload a new file to the server, this will
* create a new file at version 1.
*
* @param theFile The path of the local file to upload to the server.
* @param name The name of this file.
* @param title The title of this file.
* @param description A description of the file.
* @param mimeType The mime-type of the file, if known.
* @return A RestRequest that can perform this upload.
*/
public static RestRequest uploadFile(File theFile, String name, String title, String description, String mimeType) {
MediaType mediaType = MediaType.parse(mimeType);
MultipartBody.Builder builder = new MultipartBody.Builder();
if (title != null)
builder.addFormDataPart("title", title);
if (description != null)
builder.addFormDataPart("desc", description);
builder.addFormDataPart("fileData", name, RequestBody.create(mediaType, theFile));
return new RestRequest(RestMethod.POST, base("connect/files/users").appendPath("me").toString(), builder.build(), HTTP_HEADERS);
}
use of com.reprezen.kaizen.oasparser.model3.MediaType in project SalesforceMobileSDK-Android by forcedotcom.
the class SalesforceNetworkPlugin method buildRequestBody.
private static RequestBody buildRequestBody(JSONObject params, JSONObject fileParams) throws URISyntaxException {
if (fileParams == null || fileParams.length() == 0) {
return RequestBody.create(RestRequest.MEDIA_TYPE_JSON, params.toString());
} else {
final MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
final Iterator<String> keys = params.keys();
if (keys != null) {
while (keys.hasNext()) {
final String keyStr = keys.next();
if (!TextUtils.isEmpty(keyStr)) {
builder.addFormDataPart(keyStr, params.optString(keyStr));
}
}
}
/*
* File params expected to be of the form:
* {<fileParamNameInPost>: {fileMimeType:<someMimeType>, fileUrl:<fileUrl>, fileName:<fileNameForPost>}}.
*/
final Iterator<String> fileKeys = fileParams.keys();
if (fileKeys != null) {
while (fileKeys.hasNext()) {
final String fileKeyStr = fileKeys.next();
if (!TextUtils.isEmpty(fileKeyStr)) {
final JSONObject fileParam = fileParams.optJSONObject(fileKeyStr);
if (fileParam != null) {
final String mimeType = fileParam.optString(FILE_MIME_TYPE_KEY);
final String name = fileParam.optString(FILE_NAME_KEY);
final URI url = new URI(fileParam.optString(FILE_URL_KEY));
final File file = new File(url);
final MediaType mediaType = MediaType.parse(mimeType);
builder.addFormDataPart(fileKeyStr, name, RequestBody.create(mediaType, file));
}
}
}
}
return builder.build();
}
}
use of com.reprezen.kaizen.oasparser.model3.MediaType in project yyl_example by Relucent.
the class TraceInterceptor method intercept.
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
System.out.println("/==========TraceInterceptor==========");
System.out.println("\nRequest:");
System.out.println(request.method());
System.out.println(request.url());
System.out.println(request.headers());
RequestBody requestBody = request.body();
if (requestBody != null) {
try (Buffer buffer = new Buffer()) {
requestBody.writeTo(buffer);
String content = buffer.readUtf8();
System.out.println(content);
requestBody = RequestBody.create(content, requestBody.contentType());
}
}
request = request.newBuilder().method(request.method(), requestBody).build();
Response response = chain.proceed(request);
MediaType mediaType = response.body().contentType();
String content = response.body().string();
System.out.println("\nResponse:");
System.out.println(response.headers());
System.out.println(content);
System.out.println("==========TraceInterceptor==========/\n\n\n\n\n");
return response.newBuilder().body(ResponseBody.create(content, mediaType)).build();
}
use of com.reprezen.kaizen.oasparser.model3.MediaType in project olvid-android by olvid-io.
the class OkHttpSardine method put.
@Override
public void put(String url, byte[] data, String contentType) throws IOException {
MediaType mediaType = contentType == null ? null : MediaType.parse(contentType);
RequestBody requestBody = RequestBody.create(mediaType, data);
put(url, requestBody);
}
Aggregations