Search in sources :

Example 1 with Callback

use of retrofit2.Callback in project lottie-android by airbnb.

the class AnimationFragment method loadUrl.

private void loadUrl(String url) {
    Request request;
    try {
        request = new Request.Builder().url(url).build();
    } catch (IllegalArgumentException e) {
        onLoadError();
        return;
    }
    if (client == null) {
        client = new OkHttpClient();
    }
    client.newCall(request).enqueue(new Callback() {

        @Override
        public void onFailure(Call call, IOException e) {
            onLoadError();
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            if (!response.isSuccessful()) {
                onLoadError();
            }
            try {
                JSONObject json = new JSONObject(response.body().string());
                LottieComposition.Factory.fromJson(getResources(), json, new OnCompositionLoadedListener() {

                    @Override
                    public void onCompositionLoaded(LottieComposition composition) {
                        setComposition(composition, "Network Animation");
                    }
                });
            } catch (JSONException e) {
                onLoadError();
            }
        }
    });
}
Also used : Call(okhttp3.Call) OnCompositionLoadedListener(com.airbnb.lottie.OnCompositionLoadedListener) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) JSONException(org.json.JSONException) IOException(java.io.IOException) Response(okhttp3.Response) Callback(okhttp3.Callback) JSONObject(org.json.JSONObject) LottieComposition(com.airbnb.lottie.LottieComposition)

Example 2 with Callback

use of retrofit2.Callback in project Pokemap by omkarmoghe.

the class GoogleManager method refreshToken.

public void refreshToken(String refreshToken, final RefreshListener listener) {
    HttpUrl url = HttpUrl.parse(OAUTH_TOKEN_ENDPOINT).newBuilder().addQueryParameter("client_id", CLIENT_ID).addQueryParameter("client_secret", SECRET).addQueryParameter("refresh_token", refreshToken).addQueryParameter("grant_type", "refresh_token").build();
    Callback<GoogleService.TokenResponse> googleCallback = new Callback<GoogleService.TokenResponse>() {

        @Override
        public void onResponse(Call<GoogleService.TokenResponse> call, Response<GoogleService.TokenResponse> response) {
            if (response != null && response.body() != null) {
                listener.refreshSuccessful(response.body().getIdToken(), response.body().getRefreshToken());
            } else {
                listener.refreshFailed("Failed on requesting the id token");
            }
        }

        @Override
        public void onFailure(Call<GoogleService.TokenResponse> call, Throwable t) {
            t.printStackTrace();
            listener.refreshFailed("Failed on requesting the id token");
        }
    };
    if (mGoogleService != null) {
        Call<GoogleService.TokenResponse> call = mGoogleService.requestToken(url.toString());
        call.enqueue(googleCallback);
    }
}
Also used : Response(retrofit2.Response) Call(retrofit2.Call) Callback(retrofit2.Callback) HttpUrl(okhttp3.HttpUrl)

Example 3 with Callback

use of retrofit2.Callback in project Pokemap by omkarmoghe.

the class NianticManager method loginPTC.

private void loginPTC(final String username, final String password, NianticService.LoginValues values, final LoginListener loginListener) {
    HttpUrl url = HttpUrl.parse(LOGIN_URL).newBuilder().addQueryParameter("lt", values.getLt()).addQueryParameter("execution", values.getExecution()).addQueryParameter("_eventId", "submit").addQueryParameter("username", username).addQueryParameter("password", password).build();
    OkHttpClient client = mClient.newBuilder().followRedirects(false).followSslRedirects(false).build();
    NianticService service = new Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create()).client(client).build().create(NianticService.class);
    Callback<NianticService.LoginResponse> loginCallback = new Callback<NianticService.LoginResponse>() {

        @Override
        public void onResponse(Call<NianticService.LoginResponse> call, Response<NianticService.LoginResponse> response) {
            String location = response.headers().get("location");
            if (location != null && location.split("ticket=").length > 0) {
                String ticket = location.split("ticket=")[1];
                requestToken(ticket, loginListener);
            } else {
                Log.e(TAG, "PTC login failed via loginPTC(). There was no location header in response.");
                loginListener.authFailed("Pokemon Trainer Club Login Failed");
            }
        }

        @Override
        public void onFailure(Call<NianticService.LoginResponse> call, Throwable t) {
            t.printStackTrace();
            Log.e(TAG, "PTC login failed via loginPTC(). loginCallback.onFailure() threw: " + t.getMessage());
            loginListener.authFailed("Pokemon Trainer Club Login Failed");
        }
    };
    Call<NianticService.LoginResponse> call = service.login(url.toString());
    call.enqueue(loginCallback);
}
Also used : Call(retrofit2.Call) OkHttpClient(okhttp3.OkHttpClient) HttpUrl(okhttp3.HttpUrl) Response(retrofit2.Response) Retrofit(retrofit2.Retrofit) Callback(retrofit2.Callback)

Example 4 with Callback

use of retrofit2.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);
            }
        }
    });
}
Also used : Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) SharedPreferences(android.content.SharedPreferences) JSONException(org.json.JSONException) Intent(android.content.Intent) IOException(java.io.IOException) Response(okhttp3.Response) Callback(okhttp3.Callback) ResultCallback(com.google.android.gms.common.api.ResultCallback) JSONObject(org.json.JSONObject)

Example 5 with Callback

use of retrofit2.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;
            }
    }
}
Also used : Response(retrofit2.Response) Call(retrofit2.Call) Callback(retrofit2.Callback) Relationship(com.keylesspalace.tusky.entity.Relationship)

Aggregations

Callback (okhttp3.Callback)159 IOException (java.io.IOException)135 Response (okhttp3.Response)119 Call (okhttp3.Call)118 Request (okhttp3.Request)99 Call (retrofit2.Call)59 Retrofit (retrofit2.Retrofit)57 Test (org.junit.Test)56 Response (retrofit2.Response)38 Callback (retrofit2.Callback)37 RequestBody (okhttp3.RequestBody)36 ResponseBody (okhttp3.ResponseBody)34 OkHttpClient (okhttp3.OkHttpClient)33 File (java.io.File)29 GsonBuilder (com.google.gson.GsonBuilder)27 CountDownLatch (java.util.concurrent.CountDownLatch)26 Context (android.content.Context)24 List (java.util.List)23 RetrofitAPI (com.gladysinc.gladys.Utils.RetrofitAPI)21 SelfSigningClientBuilder (com.gladysinc.gladys.Utils.SelfSigningClientBuilder)21