Search in sources :

Example 36 with OkHttpClient

use of com.squareup.okhttp.OkHttpClient in project ETSMobile-Android2 by ApplETS.

the class AppletsApiSponsorRequest method loadDataFromNetwork.

@Override
public SponsorList loadDataFromNetwork() throws Exception {
    String address = context.getString(R.string.applets_api_sponsors);
    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder().url(address).get().build();
    Response response = client.newCall(request).execute();
    String result = response.body().string();
    SponsorList sponsors = new Gson().fromJson(result, SponsorList.class);
    return sponsors;
}
Also used : Response(com.squareup.okhttp.Response) HttpResponse(org.apache.http.HttpResponse) OkHttpClient(com.squareup.okhttp.OkHttpClient) SpringAndroidSpiceRequest(com.octo.android.robospice.request.springandroid.SpringAndroidSpiceRequest) Request(com.squareup.okhttp.Request) Gson(com.google.gson.Gson) SponsorList(ca.etsmtl.applets.etsmobile.model.SponsorList)

Example 37 with OkHttpClient

use of com.squareup.okhttp.OkHttpClient in project ETSMobile-Android2 by ApplETS.

the class AppletsApiCalendarRequest method loadDataFromNetwork.

@Override
public EventList loadDataFromNetwork() throws Exception {
    String url = context.getString(R.string.applets_api_calendar, "ets", startDate, endDate);
    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder().url(url).get().build();
    Response response = client.newCall(request).execute();
    String calendarJson = response.body().string();
    return new Gson().fromJson(calendarJson, EventList.class);
}
Also used : Response(com.squareup.okhttp.Response) HttpResponse(org.apache.http.HttpResponse) OkHttpClient(com.squareup.okhttp.OkHttpClient) SpringAndroidSpiceRequest(com.octo.android.robospice.request.springandroid.SpringAndroidSpiceRequest) Request(com.squareup.okhttp.Request) Gson(com.google.gson.Gson)

Example 38 with OkHttpClient

use of com.squareup.okhttp.OkHttpClient in project ETSMobile-Android2 by ApplETS.

the class AppletsApiSourcesRequest method loadDataFromNetwork.

@Override
public SourceEvenementList loadDataFromNetwork() throws Exception {
    String sourceAddress = context.getString(R.string.applets_api_events_sources);
    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder().url(sourceAddress).get().build();
    Response response = client.newCall(request).execute();
    String result = response.body().string();
    SourceEvenementList sources = new Gson().fromJson(result, SourceEvenementList.class);
    return sources;
}
Also used : Response(com.squareup.okhttp.Response) SourceEvenementList(ca.etsmtl.applets.etsmobile.model.applets_events.SourceEvenementList) OkHttpClient(com.squareup.okhttp.OkHttpClient) SpringAndroidSpiceRequest(com.octo.android.robospice.request.springandroid.SpringAndroidSpiceRequest) Request(com.squareup.okhttp.Request) Gson(com.google.gson.Gson)

Example 39 with OkHttpClient

use of com.squareup.okhttp.OkHttpClient in project ETSMobile-Android2 by ApplETS.

the class ETSMobileAuthenticator method getAuthToken.

@Override
public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException {
    // Extract the username and password from the Account Manager, and ask
    // the server for an appropriate AuthToken.
    final AccountManager am = AccountManager.get(mContext);
    String authToken = am.peekAuthToken(account, authTokenType);
    SecurePreferences securePreferences = new SecurePreferences(mContext);
    Date expirationDate = Utility.getDate(securePreferences, Constants.EXP_DATE_COOKIE, new Date());
    Date now = new Date();
    // Lets give another try to authenticate the user
    if (TextUtils.isEmpty(authToken) || expirationDate.before(now)) {
        final String password = am.getPassword(account);
        final String username = account.name;
        if (password != null) {
            OkHttpClient client = new OkHttpClient();
            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, "{\n  \"Username\": \"" + username + "\",\n  \"Password\": \"" + password + "\"\n}");
            Request request = new Request.Builder().url(mContext.getString(R.string.portail_api_authentification_url)).post(body).addHeader("content-type", "application/json").addHeader("cache-control", "no-cache").build();
            Response httpResponse = null;
            try {
                httpResponse = client.newCall(request).execute();
                if (httpResponse.code() == 200) {
                    authToken = httpResponse.header("Set-Cookie");
                    Utility.saveCookieExpirationDate(authToken, securePreferences);
                    JSONObject jsonResponse = new JSONObject(httpResponse.body().string());
                    int typeUsagerId = jsonResponse.getInt("TypeUsagerId");
                    String domaine = jsonResponse.getString("Domaine");
                    securePreferences.edit().putInt(Constants.TYPE_USAGER_ID, typeUsagerId).commit();
                    securePreferences.edit().putString(Constants.DOMAINE, domaine).commit();
                    ApplicationManager.domaine = domaine;
                    ApplicationManager.typeUsagerId = typeUsagerId;
                    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
                    boolean isTokenSent = sharedPreferences.getBoolean(Constants.IS_GCM_TOKEN_SENT_TO_SERVER, false);
                    if (!isTokenSent) {
                        Intent intent = new Intent(mContext, RegistrationIntentService.class);
                        mContext.startService(intent);
                    }
                } else {
                    Log.e("Erreur Portail", httpResponse.toString());
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
    // If we get an authToken - we return it
    if (!TextUtils.isEmpty(authToken)) {
        final Bundle result = new Bundle();
        result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
        result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
        result.putString(AccountManager.KEY_AUTHTOKEN, authToken);
        return result;
    } else {
        final Bundle result = new Bundle();
        result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
        result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
        result.putString(AccountManager.KEY_AUTHTOKEN, null);
        return result;
    }
// If we get here, then we couldn't access the user's password - so we
// need to re-prompt them for their credentials. We do that by creating
// an intent to display our AuthenticatorActivity.
/*
        final Intent intent = new Intent(mContext, LoginActivity.class);
        intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
        intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, account.type);
        intent.putExtra(Constants.KEY_AUTH_TYPE, authTokenType);
        final Bundle bundle = new Bundle();
        bundle.putParcelable(AccountManager.KEY_INTENT, intent);
        return bundle;
        */
}
Also used : OkHttpClient(com.squareup.okhttp.OkHttpClient) SharedPreferences(android.content.SharedPreferences) Bundle(android.os.Bundle) Request(com.squareup.okhttp.Request) JSONException(org.json.JSONException) Intent(android.content.Intent) IOException(java.io.IOException) Date(java.util.Date) AccountAuthenticatorResponse(android.accounts.AccountAuthenticatorResponse) Response(com.squareup.okhttp.Response) JSONObject(org.json.JSONObject) MediaType(com.squareup.okhttp.MediaType) AccountManager(android.accounts.AccountManager) RequestBody(com.squareup.okhttp.RequestBody)

Example 40 with OkHttpClient

use of com.squareup.okhttp.OkHttpClient in project pictureapp by EyeSeeTea.

the class PushClient method pushData.

/**
     * Pushes data to DHIS Server
     */
private JSONObject pushData(JSONObject data) throws Exception {
    Response response = null;
    final String DHIS_URL = getDhisURL();
    OkHttpClient client = UnsafeOkHttpsClientFactory.getUnsafeOkHttpClient();
    BasicAuthenticator basicAuthenticator = new BasicAuthenticator();
    client.setAuthenticator(basicAuthenticator);
    RequestBody body = RequestBody.create(JSON, data.toString());
    Request request = new Request.Builder().header(basicAuthenticator.AUTHORIZATION_HEADER, basicAuthenticator.getCredentials()).url(DHIS_URL).post(body).build();
    response = client.newCall(request).execute();
    if (!response.isSuccessful()) {
        Log.e(TAG, "pushData (" + response.code() + "): " + response.body().string());
        throw new IOException(response.message());
    }
    return parseResponse(response.body().string());
}
Also used : Response(com.squareup.okhttp.Response) OkHttpClient(com.squareup.okhttp.OkHttpClient) Request(com.squareup.okhttp.Request) IOException(java.io.IOException) RequestBody(com.squareup.okhttp.RequestBody)

Aggregations

OkHttpClient (com.squareup.okhttp.OkHttpClient)47 Request (com.squareup.okhttp.Request)22 Response (com.squareup.okhttp.Response)18 IOException (java.io.IOException)15 RequestBody (com.squareup.okhttp.RequestBody)8 Provides (dagger.Provides)6 Gson (com.google.gson.Gson)5 Cache (com.squareup.okhttp.Cache)5 Singleton (javax.inject.Singleton)5 SpringAndroidSpiceRequest (com.octo.android.robospice.request.springandroid.SpringAndroidSpiceRequest)4 File (java.io.File)4 RestAdapter (retrofit.RestAdapter)4 OkClient (retrofit.client.OkClient)4 SSLContext (javax.net.ssl.SSLContext)3 X509TrustManager (javax.net.ssl.X509TrustManager)3 HttpResponse (org.apache.http.HttpResponse)3 Intent (android.content.Intent)2 SharedPreferences (android.content.SharedPreferences)2 Call (com.squareup.okhttp.Call)2 Dispatcher (com.squareup.okhttp.Dispatcher)2