Search in sources :

Example 61 with RequestBody

use of okhttp3.RequestBody in project Reader by TheKeeperOfPie.

the class Reddit method tokenAuth.

public Observable<String> tokenAuth() {
    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();
    return load(request);
}
Also used : FormBody(okhttp3.FormBody) Request(okhttp3.Request) RequestBody(okhttp3.RequestBody)

Example 62 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 63 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 64 with RequestBody

use of okhttp3.RequestBody in project Parse-SDK-Android by ParsePlatform.

the class ParseOkHttpClientTest method testGetOkHttpRequest.

@Test
public void testGetOkHttpRequest() throws IOException {
    Map<String, String> headers = new HashMap<>();
    String headerName = "name";
    String headerValue = "value";
    headers.put(headerName, headerValue);
    String url = "http://www.parse.com/";
    String content = "test";
    int contentLength = content.length();
    String contentType = "application/json";
    ParseHttpRequest parseRequest = new ParseHttpRequest.Builder().setUrl(url).setMethod(ParseHttpRequest.Method.POST).setBody(new ParseByteArrayHttpBody(content, contentType)).setHeaders(headers).build();
    ParseOkHttpClient parseClient = new ParseOkHttpClient(10000, null);
    Request okHttpRequest = parseClient.getRequest(parseRequest);
    // Verify method
    assertEquals(ParseHttpRequest.Method.POST.toString(), okHttpRequest.method());
    // Verify URL
    assertEquals(url, okHttpRequest.url().toString());
    // Verify Headers
    assertEquals(1, okHttpRequest.headers(headerName).size());
    assertEquals(headerValue, okHttpRequest.headers(headerName).get(0));
    // Verify Body
    RequestBody okHttpBody = okHttpRequest.body();
    assertEquals(contentLength, okHttpBody.contentLength());
    assertEquals(contentType, okHttpBody.contentType().toString());
    // Can not read parseRequest body to compare since it has been read during
    // creating okHttpRequest
    Buffer buffer = new Buffer();
    okHttpBody.writeTo(buffer);
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    buffer.copyTo(output);
    assertArrayEquals(content.getBytes(), output.toByteArray());
}
Also used : Buffer(okio.Buffer) ParseHttpRequest(com.parse.http.ParseHttpRequest) HashMap(java.util.HashMap) Request(okhttp3.Request) ParseHttpRequest(com.parse.http.ParseHttpRequest) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RequestBody(okhttp3.RequestBody) Test(org.junit.Test)

Example 65 with RequestBody

use of okhttp3.RequestBody in project Android by Tracman-org.

the class LoginActivity method signInWithPassword.

public void signInWithPassword() {
    //Log.d(TAG, "signInWithPassword() called");
    // Get params from form
    EditText emailText = (EditText) findViewById(R.id.login_email);
    String email = emailText.getText().toString();
    EditText passwordText = (EditText) findViewById(R.id.login_password);
    String password = passwordText.getText().toString();
    // Build formdata
    RequestBody formData = new FormBody.Builder().add("email", email).add("password", password).build();
    // Build request
    Request request = new Request.Builder().url(SERVER_ADDRESS + "login/app").post(formData).build();
    // Send formdata to endpoint
    try {
        //Log.v(TAG, "Sending local login POST to server...");
        authenticateWithTracmanServer(request);
    } catch (Exception e) {
    //Log.e(TAG, "Error sending local login to backend:",e);
    }
}
Also used : EditText(android.widget.EditText) FormBody(okhttp3.FormBody) Request(okhttp3.Request) JSONException(org.json.JSONException) IOException(java.io.IOException) RequestBody(okhttp3.RequestBody)

Aggregations

RequestBody (okhttp3.RequestBody)178 Request (okhttp3.Request)141 IOException (java.io.IOException)75 Response (okhttp3.Response)74 Test (org.junit.Test)53 ResponseBody (okhttp3.ResponseBody)32 Call (okhttp3.Call)31 MultipartBody (okhttp3.MultipartBody)28 MediaType (okhttp3.MediaType)27 FormBody (okhttp3.FormBody)25 Callback (okhttp3.Callback)23 Buffer (okio.Buffer)23 MockResponse (okhttp3.mockwebserver.MockResponse)18 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)16 TestClients.clientRequest (keywhiz.TestClients.clientRequest)15 BufferedSink (okio.BufferedSink)15 Headers (okhttp3.Headers)12 HttpUrl (okhttp3.HttpUrl)11 JSONObject (org.json.JSONObject)11 Body (retrofit2.http.Body)11