Search in sources :

Example 61 with MediaType

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();
        }
    }
}
Also used : MediaType(okhttp3.MediaType) ResponseBody(okhttp3.ResponseBody)

Example 62 with MediaType

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);
}
Also used : RestRequest(com.salesforce.androidsdk.rest.RestRequest) MultipartBody(okhttp3.MultipartBody) MediaType(okhttp3.MediaType)

Example 63 with MediaType

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();
    }
}
Also used : JSONObject(org.json.JSONObject) MultipartBody(okhttp3.MultipartBody) MediaType(okhttp3.MediaType) URI(java.net.URI) File(java.io.File)

Example 64 with MediaType

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();
}
Also used : Buffer(okio.Buffer) Response(okhttp3.Response) Request(okhttp3.Request) MediaType(okhttp3.MediaType) RequestBody(okhttp3.RequestBody)

Example 65 with MediaType

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);
}
Also used : MediaType(okhttp3.MediaType) RequestBody(okhttp3.RequestBody)

Aggregations

MediaType (okhttp3.MediaType)297 RequestBody (okhttp3.RequestBody)186 Request (okhttp3.Request)179 Response (okhttp3.Response)158 IOException (java.io.IOException)137 ResponseBody (okhttp3.ResponseBody)71 OkHttpClient (okhttp3.OkHttpClient)68 Charset (java.nio.charset.Charset)50 Buffer (okio.Buffer)50 Headers (okhttp3.Headers)48 JSONObject (org.json.JSONObject)38 BufferedSource (okio.BufferedSource)36 MultipartBody (okhttp3.MultipartBody)34 HttpUrl (okhttp3.HttpUrl)30 Map (java.util.Map)24 BufferedSink (okio.BufferedSink)23 File (java.io.File)22 InputStream (java.io.InputStream)21 ArrayList (java.util.ArrayList)21 Test (org.junit.Test)21