Java Code Examples for okhttp3.internal.http.HttpMethod#requiresRequestBody()

The following examples show how to use okhttp3.internal.http.HttpMethod#requiresRequestBody() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OtherRequest.java    From styT with Apache License 2.0 5 votes vote down vote up
@Override
protected RequestBody buildRequestBody() {
    if (requestBody == null && TextUtils.isEmpty(content) && HttpMethod.requiresRequestBody(method)) {
        Exceptions.illegalArgument("requestBody and content can not be null in method:" + method);
    }

    if (requestBody == null && !TextUtils.isEmpty(content)) {
        requestBody = RequestBody.create(MEDIA_TYPE_PLAIN, content);
    }

    return requestBody;
}
 
Example 2
Source File: OtherRequest.java    From stynico with MIT License 5 votes vote down vote up
@Override
protected RequestBody buildRequestBody() {
    if (requestBody == null && TextUtils.isEmpty(content) && HttpMethod.requiresRequestBody(method)) {
        Exceptions.illegalArgument("requestBody and content can not be null in method:" + method);
    }

    if (requestBody == null && !TextUtils.isEmpty(content)) {
        requestBody = RequestBody.create(MEDIA_TYPE_PLAIN, content);
    }

    return requestBody;
}
 
Example 3
Source File: OtherRequest.java    From SmartChart with Apache License 2.0 5 votes vote down vote up
@Override
protected RequestBody buildRequestBody() {
    if (requestBody == null && TextUtils.isEmpty(content) && HttpMethod.requiresRequestBody(method)) {
        Exceptions.illegalArgument("requestBody and content can not be null in method:" + method);
    }

    if (requestBody == null && !TextUtils.isEmpty(content)) {
        requestBody = RequestBody.create(MEDIA_TYPE_PLAIN, content);
    }

    return requestBody;
}
 
Example 4
Source File: OtherRequest.java    From enjoyshop with Apache License 2.0 5 votes vote down vote up
@Override
protected RequestBody buildRequestBody()
{
    if (requestBody == null && TextUtils.isEmpty(content) && HttpMethod.requiresRequestBody(method))
    {
        Exceptions.illegalArgument("requestBody and content can not be null in method:" + method);
    }

    if (requestBody == null && !TextUtils.isEmpty(content))
    {
        requestBody = RequestBody.create(MEDIA_TYPE_PLAIN, content);
    }

    return requestBody;
}
 
Example 5
Source File: OkOtherRequest.java    From FamilyChat with Apache License 2.0 5 votes vote down vote up
@Override
protected RequestBody buildRequestBody()
{
    if (requestBody == null && TextUtils.isEmpty(content) && HttpMethod.requiresRequestBody(method))
        throw new IllegalArgumentException("requestBody and content can not be null in method:" + method);

    if (requestBody == null && !TextUtils.isEmpty(content))
        requestBody = RequestBody.create(MEDIA_TYPE_PLAIN, content);

    return requestBody;
}
 
Example 6
Source File: OtherRequest.java    From okhttputils with Apache License 2.0 5 votes vote down vote up
@Override
protected RequestBody buildRequestBody()
{
    if (requestBody == null && TextUtils.isEmpty(content) && HttpMethod.requiresRequestBody(method))
    {
        Exceptions.illegalArgument("requestBody and content can not be null in method:" + method);
    }

    if (requestBody == null && !TextUtils.isEmpty(content))
    {
        requestBody = RequestBody.create(MEDIA_TYPE_PLAIN, content);
    }

    return requestBody;
}
 
Example 7
Source File: Requester.java    From JDA with Apache License 2.0 5 votes vote down vote up
private void applyBody(Request<?> apiRequest, okhttp3.Request.Builder builder)
{
    String method = apiRequest.getRoute().getMethod().toString();
    RequestBody body = apiRequest.getBody();

    if (body == null && HttpMethod.requiresRequestBody(method))
        body = EMPTY_BODY;

    builder.method(method, body);
}
 
Example 8
Source File: RequesterImpl.java    From omise-java with MIT License 5 votes vote down vote up
/**
 * Carries out the HTTP request
 *
 * @param url    api url path
 * @param body   additional request params (if available)
 * @param method HTTP method
 * @return {@link Response} response object returned by {@link OkHttpClient}
 * @throws IOException I/O exception thrown by {@link OkHttpClient} during the HTTP request
 */
private Response roundTrip(HttpUrl url, RequestBody body, String method) throws IOException {
    if (body == null && HttpMethod.requiresRequestBody(method)) { // params == null
        body = new FormBody.Builder().build();
    }

    okhttp3.Request request = new okhttp3.Request.Builder()
            .method(method, body)
            .url(url)
            .build();

    return httpClient.newCall(request).execute();
}
 
Example 9
Source File: HttpRequest.java    From mica with GNU Lesser General Public License v3.0 4 votes vote down vote up
private Call internalCall(final OkHttpClient client) {
	OkHttpClient.Builder builder = client.newBuilder();
	if (connectTimeout != null) {
		builder.connectTimeout(connectTimeout.toMillis(), TimeUnit.MILLISECONDS);
	}
	if (readTimeout != null) {
		builder.readTimeout(readTimeout.toMillis(), TimeUnit.MILLISECONDS);
	}
	if (writeTimeout != null) {
		builder.writeTimeout(writeTimeout.toMillis(), TimeUnit.MILLISECONDS);
	}
	if (protocols != null && !protocols.isEmpty()) {
		builder.protocols(protocols);
	}
	if (proxy != null) {
		builder.proxy(proxy);
	}
	if (proxySelector != null) {
		builder.proxySelector(proxySelector);
	}
	if (proxyAuthenticator != null) {
		builder.proxyAuthenticator(proxyAuthenticator);
	}
	if (hostnameVerifier != null) {
		builder.hostnameVerifier(hostnameVerifier);
	}
	if (sslSocketFactory != null && trustManager != null) {
		builder.sslSocketFactory(sslSocketFactory, trustManager);
	}
	if (Boolean.TRUE.equals(disableSslValidation)) {
		disableSslValidation(builder);
	}
	if (authenticator != null) {
		builder.authenticator(authenticator);
	}
	if (eventListener != null) {
		builder.eventListener(eventListener);
	}
	if (!interceptors.isEmpty()) {
		builder.interceptors().addAll(interceptors);
	}
	if (cookieJar != null) {
		builder.cookieJar(cookieJar);
	}
	if (followRedirects != null) {
		builder.followRedirects(followRedirects);
	}
	if (followSslRedirects != null) {
		builder.followSslRedirects(followSslRedirects);
	}
	if (retryPolicy != null) {
		builder.addInterceptor(new RetryInterceptor(retryPolicy));
	}
	if (httpLogger != null && logLevel != null && HttpLoggingInterceptor.Level.NONE != logLevel) {
		builder.addInterceptor(getLoggingInterceptor(httpLogger, logLevel));
	} else if (globalLoggingInterceptor != null) {
		builder.addInterceptor(globalLoggingInterceptor);
	}
	// 设置 User-Agent
	requestBuilder.header("User-Agent", userAgent);
	// url
	requestBuilder.url(uriBuilder.build());
	String method = httpMethod;
	Request request;
	if (HttpMethod.requiresRequestBody(method) && requestBody == null) {
		request = requestBuilder.method(method, Util.EMPTY_REQUEST).build();
	} else {
		request = requestBuilder.method(method, requestBody).build();
	}
	return builder.build().newCall(request);
}