use of okhttp3.Callback in project Android by Tracman-org.
the class LoginActivity method authenticateWithTracmanServer.
private void authenticateWithTracmanServer(final Request request) throws Exception {
// Needed to support TLS 1.1 and 1.2
// TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(
// TrustManagerFactory.getDefaultAlgorithm());
// trustManagerFactory.init((KeyStore) null);
// TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
// if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) {
// throw new IllegalStateException("Unexpected default trust managers:"
// + Arrays.toString(trustManagers));
// }
// X509TrustManager trustManager = (X509TrustManager) trustManagers[0];
OkHttpClient client = new OkHttpClient.Builder().build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
//Log.e(TAG, "Failed to connect to Tracman server!");
showError(R.string.server_connection_error);
e.printStackTrace();
}
@Override
public void onResponse(Call call, Response res) throws IOException {
if (!res.isSuccessful()) {
showError(R.string.login_no_user_error);
res.body().close();
throw new IOException("Unexpected code: " + res);
} else {
//Log.d(TAG, "Response code: " + res.code());
String userString = res.body().string();
System.out.println("Full response: " + userString);
String userID, userName, userSK;
try {
JSONObject user = new JSONObject(userString);
userID = user.getString("_id");
userName = user.getString("name");
userSK = user.getString("sk32");
//Log.v(TAG, "User retrieved with ID: " + userID);
} catch (JSONException e) {
//Log.e(TAG, "Unable to parse user JSON: ", e);
//Log.e(TAG, "JSON String used: " + userString);
userID = null;
userName = null;
userSK = null;
}
// Save user as loggedInUser
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("loggedInUser", userString);
editor.putString("loggedInUserId", userID);
editor.putString("loggedInUserName", userName);
editor.putString("loggedInUserSk", userSK);
editor.commit();
startActivityForResult(new Intent(LoginActivity.this, SettingsActivity.class), SIGN_OUT);
}
}
});
}
use of okhttp3.Callback in project Tusky by Vavassor.
the class AccountActivity method follow.
private void follow(final String id) {
Callback<Relationship> cb = new Callback<Relationship>() {
@Override
public void onResponse(Call<Relationship> call, Response<Relationship> response) {
if (response.isSuccessful()) {
Relationship relationship = response.body();
if (relationship.following) {
followState = FollowState.FOLLOWING;
} else if (relationship.requested) {
followState = FollowState.REQUESTED;
Snackbar.make(container, R.string.state_follow_requested, Snackbar.LENGTH_LONG).show();
} else {
followState = FollowState.NOT_FOLLOWING;
broadcast(TimelineReceiver.Types.UNFOLLOW_ACCOUNT, id);
}
updateButtons();
} else {
onFollowFailure(id);
}
}
@Override
public void onFailure(Call<Relationship> call, Throwable t) {
onFollowFailure(id);
}
};
Assert.expect(followState != FollowState.REQUESTED);
switch(followState) {
case NOT_FOLLOWING:
{
mastodonAPI.followAccount(id).enqueue(cb);
break;
}
case FOLLOWING:
{
mastodonAPI.unfollowAccount(id).enqueue(cb);
break;
}
}
}
use of okhttp3.Callback in project Tusky by Vavassor.
the class BaseActivity method disablePushNotifications.
protected void disablePushNotifications() {
Callback<ResponseBody> callback = new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
if (response.isSuccessful()) {
pushNotificationClient.unsubscribeToTopic(getPushNotificationTopic());
} else {
onDisablePushNotificationsFailure();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
onDisablePushNotificationsFailure();
}
};
String deviceToken = pushNotificationClient.getDeviceToken();
Session session = new Session(getDomain(), getAccessToken(), deviceToken);
tuskyApi.unregister(session).enqueue(callback);
}
use of okhttp3.Callback in project Tusky by Vavassor.
the class ComposeActivity method uploadMedia.
private void uploadMedia(final QueuedMedia item) {
item.readyStage = QueuedMedia.ReadyStage.UPLOADING;
final String mimeType = getContentResolver().getType(item.uri);
MimeTypeMap map = MimeTypeMap.getSingleton();
String fileExtension = map.getExtensionFromMimeType(mimeType);
final String filename = String.format("%s_%s_%s.%s", getString(R.string.app_name), String.valueOf(new Date().getTime()), randomAlphanumericString(10), fileExtension);
byte[] content = item.content;
if (content == null) {
InputStream stream;
try {
stream = getContentResolver().openInputStream(item.uri);
} catch (FileNotFoundException e) {
Log.d(TAG, Log.getStackTraceString(e));
return;
}
content = inputStreamGetBytes(stream);
IOUtils.closeQuietly(stream);
if (content == null) {
return;
}
}
RequestBody requestFile = RequestBody.create(MediaType.parse(mimeType), content);
MultipartBody.Part body = MultipartBody.Part.createFormData("file", filename, requestFile);
item.uploadRequest = mastodonAPI.uploadMedia(body);
item.uploadRequest.enqueue(new Callback<Media>() {
@Override
public void onResponse(Call<Media> call, retrofit2.Response<Media> response) {
if (response.isSuccessful()) {
onUploadSuccess(item, response.body());
} else {
Log.d(TAG, "Upload request failed. " + response.message());
onUploadFailure(item, call.isCanceled());
}
}
@Override
public void onFailure(Call<Media> call, Throwable t) {
Log.d(TAG, t.getMessage());
onUploadFailure(item, false);
}
});
}
use of okhttp3.Callback in project Tusky by Vavassor.
the class BaseActivity method enablePushNotifications.
protected void enablePushNotifications() {
Callback<ResponseBody> callback = new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
if (response.isSuccessful()) {
pushNotificationClient.subscribeToTopic(getPushNotificationTopic());
pushNotificationClient.connect(BaseActivity.this);
} else {
onEnablePushNotificationsFailure(response.message());
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
onEnablePushNotificationsFailure(t.getMessage());
}
};
String deviceToken = pushNotificationClient.getDeviceToken();
Session session = new Session(getDomain(), getAccessToken(), deviceToken);
tuskyApi.register(session).enqueue(callback);
}
Aggregations