Search in sources :

Example 6 with RequestBody

use of okhttp3.RequestBody in project Reader by TheKeeperOfPie.

the class Reddit method tokenAuthBlocking.

public String tokenAuthBlocking() throws ExecutionException, InterruptedException {
    RequestBody requestBody = new FormBody.Builder().add(QUERY_GRANT_TYPE, QUERY_REFRESH_TOKEN).add(QUERY_REFRESH_TOKEN, accountManager.getPassword(account)).build();
    Request request = Reddit.withRequestBasicAuth().url(Reddit.ACCESS_URL).post(requestBody).build();
    try {
        return okHttpClientDefault.newCall(request).execute().body().string();
    } catch (IOException e) {
        return null;
    }
}
Also used : FormBody(okhttp3.FormBody) Request(okhttp3.Request) IOException(java.io.IOException) RequestBody(okhttp3.RequestBody)

Example 7 with RequestBody

use of okhttp3.RequestBody in project Reader by TheKeeperOfPie.

the class Reddit method getApplicationWideTokenBlocking.

private void getApplicationWideTokenBlocking() {
    if ("".equals(preferences.getString(AppSettings.DEVICE_ID, ""))) {
        preferences.edit().putString(AppSettings.DEVICE_ID, UUID.randomUUID().toString()).apply();
    }
    RequestBody requestBody = new FormBody.Builder().add(QUERY_GRANT_TYPE, INSTALLED_CLIENT_GRANT).add(QUERY_DEVICE_ID, preferences.getString(AppSettings.DEVICE_ID, UUID.randomUUID().toString())).build();
    Request request = new Request.Builder().url(ACCESS_URL).post(requestBody).build();
    try {
        String response = okHttpClientDefault.newCall(request).execute().body().string();
        // Check if an account was set while fetching a token
        if (account == null) {
            JSONObject jsonObject = new JSONObject(response);
            tokenAuth = jsonObject.getString(QUERY_ACCESS_TOKEN);
            timeExpire = System.currentTimeMillis() + jsonObject.getLong(QUERY_EXPIRES_IN) * SEC_TO_MS;
        }
    } catch (JSONException | IOException e) {
        e.printStackTrace();
    }
}
Also used : JSONObject(org.json.JSONObject) FormBody(okhttp3.FormBody) Request(okhttp3.Request) JSONException(org.json.JSONException) IOException(java.io.IOException) RequestBody(okhttp3.RequestBody)

Example 8 with RequestBody

use of okhttp3.RequestBody in project MVCHelper by LuckyJayce.

the class PostFileMethod method buildRequset.

@Override
protected Request.Builder buildRequset(String url, Map<String, Object> params) {
    MultipartBody.Builder builder = new MultipartBody.Builder();
    if (params != null) {
        for (Entry<String, ?> entry : params.entrySet()) {
            builder.addFormDataPart(entry.getKey(), String.valueOf(entry.getValue()));
        }
    }
    if (httpbodys != null) {
        for (Entry<String, Data2<String, RequestBody>> entry : httpbodys.entrySet()) {
            String key = entry.getKey();
            Data2<String, RequestBody> body = entry.getValue();
            builder.addFormDataPart(key, body.getValue1(), body.getValue2());
        }
    }
    RequestBody formBody = builder.build();
    if (listener != null) {
        formBody = new CountingRequestBody(formBody, listener);
    }
    return new Request.Builder().url(url).post(formBody);
}
Also used : Data2(com.shizhefei.mvc.data.Data2) MultipartBody(okhttp3.MultipartBody) RequestBody(okhttp3.RequestBody)

Example 9 with RequestBody

use of okhttp3.RequestBody in project MVCHelper by LuckyJayce.

the class PostMethod method buildRequset.

@Override
protected Request.Builder buildRequset(String url, Map<String, Object> params) {
    FormBody.Builder builder = new FormBody.Builder();
    if (params != null) {
        for (Entry<String, ?> entry : params.entrySet()) {
            builder.add(entry.getKey(), String.valueOf(entry.getValue()));
        }
    }
    RequestBody formBody = builder.build();
    return new Request.Builder().url(url).post(formBody);
}
Also used : FormBody(okhttp3.FormBody) RequestBody(okhttp3.RequestBody)

Example 10 with RequestBody

use of okhttp3.RequestBody in project MVCHelper by LuckyJayce.

the class PutMethod method buildRequset.

@Override
protected Request.Builder buildRequset(String url, Map<String, Object> params) {
    FormBody.Builder builder = new FormBody.Builder();
    if (params != null) {
        for (Entry<String, ?> entry : params.entrySet()) {
            builder.add(entry.getKey(), String.valueOf(entry.getValue()));
        }
    }
    RequestBody formBody = builder.build();
    return new Request.Builder().url(url).put(formBody);
}
Also used : FormBody(okhttp3.FormBody) RequestBody(okhttp3.RequestBody)

Aggregations

RequestBody (okhttp3.RequestBody)159 Request (okhttp3.Request)129 Response (okhttp3.Response)68 IOException (java.io.IOException)63 Test (org.junit.Test)53 Call (okhttp3.Call)30 ResponseBody (okhttp3.ResponseBody)30 MultipartBody (okhttp3.MultipartBody)27 FormBody (okhttp3.FormBody)25 MediaType (okhttp3.MediaType)24 Callback (okhttp3.Callback)22 Buffer (okio.Buffer)20 MockResponse (okhttp3.mockwebserver.MockResponse)18 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)16 TestClients.clientRequest (keywhiz.TestClients.clientRequest)15 BufferedSink (okio.BufferedSink)14 HttpUrl (okhttp3.HttpUrl)11 Body (retrofit2.http.Body)11 Headers (okhttp3.Headers)10 List (java.util.List)9