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);
}
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;
}
}
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();
}
}
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());
}
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);
}
}
Aggregations