Example usage for com.squareup.okhttp OkHttpClient OkHttpClient

List of usage examples for com.squareup.okhttp OkHttpClient OkHttpClient

Introduction

In this page you can find the example usage for com.squareup.okhttp OkHttpClient OkHttpClient.

Prototype

public OkHttpClient() 

Source Link

Usage

From source file:com.pacoworks.dereference.dependencies.modules.NetworkModule.java

License:Open Source License

@Provides
@Singleton/*from w  w w  . ja  va  2s  .  co m*/
OkHttpClient provideOkHttp(final Cache cache, LoggerInterceptor loggerInterceptor,
        StethoInterceptor stethoInterceptor) {
    final OkHttpClient okHttpClient = new OkHttpClient();
    okHttpClient.setCache(cache);
    okHttpClient.networkInterceptors().add(loggerInterceptor);
    okHttpClient.networkInterceptors().add(stethoInterceptor);
    okHttpClient.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    okHttpClient.setReadTimeout(DEFAULT_READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    okHttpClient.setWriteTimeout(DEFAULT_WRITE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    return okHttpClient;
}

From source file:com.parse.ParseOkHttpClient.java

License:Open Source License

public ParseOkHttpClient(int socketOperationTimeout, SSLSessionCache sslSessionCache) {

    okHttpClient = new OkHttpClient();

    okHttpClient.setConnectTimeout(socketOperationTimeout, TimeUnit.MILLISECONDS);
    okHttpClient.setReadTimeout(socketOperationTimeout, TimeUnit.MILLISECONDS);

    // Don't handle redirects. We copy the setting from AndroidHttpClient.
    // For detail, check https://quip.com/Px8jAxnaun2r
    okHttpClient.setFollowRedirects(false);

    okHttpClient.setSslSocketFactory(/*from w ww . ja va 2 s .c  o  m*/
            SSLCertificateSocketFactory.getDefault(socketOperationTimeout, sslSessionCache));
}

From source file:com.personal.coine.scorpion.jxnuhelper.biz.impl.UserInfoBizImpl.java

License:Open Source License

@Override
public void loadUserAvadar(final Context context, final Callback callback) {

    final OkHttpClient client = new OkHttpClient();
    new Thread(new Runnable() {
        @Override// ww w . j  ava 2 s .c o  m
        public void run() {
            Request request = new Request.Builder()
                    .url(BmobUser.getCurrentUser(context, MyUser.class).getUserAvadarPath()).build();
            client.newCall(request).enqueue(callback);
        }
    }).start();
}

From source file:com.project.ultimatenote.activity.MainActivity.java

private void initView() {

    mViewPager = (ViewPager) findViewById(R.id.vp);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    navigationView = (NavigationView) findViewById(R.id.nav_view);
    fab = (FloatingActionButton) findViewById(R.id.fab);
    headerImage = (ImageView) findViewById(R.id.nav_icon);
    navHeadText = (TextView) findViewById(R.id.nav_email);
    setSupportActionBar(toolbar);//from  ww w .  j  a  v  a 2s .c o  m
    //?ActionBar
    final ActionBar ab = getSupportActionBar();
    if (ab != null) {
        ab.setHomeAsUpIndicator(R.drawable.ic_show_bar_white);
        ab.setDisplayHomeAsUpEnabled(true);
    }
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar,
            R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    mDrawerLayout.setDrawerListener(toggle);
    toggle.syncState();
    setupDrawerContent(navigationView);
    AVUser currentUser = AVUser.getCurrentUser();
    //??navHeadTextLoginActivity?
    if (currentUser == null) {
        navHeadText.setText("");
        navHeadText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                turnToLogin();
            }
        });
    } else {
        //navHeadText??
        navHeadText.setText(currentUser.getUsername());

        //??
        Picasso picasso = new Picasso.Builder(MyApplication.getInstance())
                .downloader(new OkHttpDownloader(new OkHttpClient())).build();
        picasso.load(ImageUtil.getAvatarUrl(currentUser.getUsername(), 100)).config(Bitmap.Config.RGB_565)
                .resize(ViewUtil.dp2px(100), ViewUtil.dp2px(100)).centerCrop()
                .transform(new CircleTransformation()).into(headerImage);
    }
}

From source file:com.publictransitanalytics.scoregenerator.workflow.Calculation.java

License:Apache License

private static DistanceClient buildOsrmDistanceClient(final PointSequencerFactory pointSequencerFactory,
        final int maxConsidered) {
    final DistanceClient osrmDistanceClient = new OsrmLocalDistanceClient(new OkHttpClient(), "localhost", 5000,
            pointSequencerFactory);//from  w ww .j  a va2  s.c  om

    return new SplitMergeDistanceClient(osrmDistanceClient, maxConsidered);
}

From source file:com.quarterfull.newsAndroid.reader.HttpJsonRequest.java

License:Open Source License

private HttpJsonRequest(Context context) {
    client = new OkHttpClient();

    // set location of the keystore
    MemorizingTrustManager.setKeyStoreFile("private", "sslkeys.bks");

    // register MemorizingTrustManager for HTTPS
    try {/*  w  w  w.  ja v a2 s  . c om*/
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, MemorizingTrustManager.getInstanceList(context), new java.security.SecureRandom());
        // enables TLSv1.1/1.2 for Jelly Bean Devices
        TLSSocketFactory tlsSocketFactory = new TLSSocketFactory(sc);
        client.setSslSocketFactory(tlsSocketFactory);
    } catch (KeyManagementException | NoSuchAlgorithmException e) {
        e.printStackTrace();
    }

    client.setConnectTimeout(10000, TimeUnit.MILLISECONDS);
    client.setReadTimeout(120, TimeUnit.SECONDS);

    // disable hostname verification, when preference is set
    // (this still shows a certification dialog, which requires user interaction!)
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
    if (sp.getBoolean(SettingsActivity.CB_DISABLE_HOSTNAME_VERIFICATION_STRING, false))
        client.setHostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });
    imageClient = client.clone();
    client.interceptors().add(new AuthorizationInterceptor());

    setCredentials(sp.getString(SettingsActivity.EDT_USERNAME_STRING, null),
            sp.getString(SettingsActivity.EDT_PASSWORD_STRING, null),
            sp.getString(SettingsActivity.EDT_OWNCLOUDROOTPATH_STRING, null));
}

From source file:com.quwu.xinwo.home_page.City_WideActivity.java

/**
 * ?/*from  ww  w  .ja  v a 2  s.c  om*/
 * */
public String firstDoPost(String url, String parameter) {
    OkHttpClient mOkHttpClient = new OkHttpClient();
    RequestBody formBody = new FormEncodingBuilder().add("city_region", parameter).add("small_area", "0")
            .add("twolevel_id", "1").add("three_id", "15").add("sortorder_id", "1").add("goods_lprice", "0")
            .add("goods_hprice", "800000").add("state", "1").add("pageNow", String.valueOf(pageNow))
            .add("pageSize", String.valueOf(pageSize)).build();

    //  header(name, value)
    // ?name?value?? addHeader(name, value)
    // ??
    Request request = new Request.Builder().url(url).header("User-Agent", "OkHttp Headers.java")
            .addHeader("Accept", "application/json; q=0.5")
            .addHeader("Accept", "application/vnd.github.v3+json").post(formBody).build();
    Response response;
    try {
        response = mOkHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            String body = response.body().string();
            return body;
        }
    } catch (IOException e) {

        e.printStackTrace();
    } // execute

    return null;
}

From source file:com.quwu.xinwo.home_page.City_WideActivity.java

/**
 * ???/* www.  j  a v a 2  s  .  co  m*/
 * */
public String MyDoPost(String url, String normal_use, String warranty_period, String no_repair,
        String brand_new, String jingdong, String mainland_licensed, String since, String city_region,
        String small_area, String twolevel_id, String three_id, String sortorder_id, String goods_lprice,
        String goods_hprice, String fristlevel, String buy_userid) {
    OkHttpClient mOkHttpClient = new OkHttpClient();
    RequestBody formBody = new FormEncodingBuilder().add("normal_use", normal_use)
            .add("warranty_period", warranty_period).add("no_repair", no_repair).add("brand_new", brand_new)
            .add("jingdong", jingdong).add("mainland_licensed", mainland_licensed).add("since", since)
            .add("city_region", city_region).add("small_area", small_area).add("twolevel_id", twolevel_id)
            .add("three_id", three_id).add("sortorder_id", sortorder_id).add("goods_lprice", goods_lprice)
            .add("goods_hprice", goods_hprice).add("fristlevel", fristlevel).add("buy_userid", buy_userid)
            .add("pageNow", String.valueOf(pageNow)).add("pageSize", String.valueOf(pageSize)).build();

    //  header(name, value)
    // ?name?value?? addHeader(name, value)
    // ??
    Request request = new Request.Builder().url(url).header("User-Agent", "OkHttp Headers.java")
            .addHeader("Accept", "application/json; q=0.5")
            .addHeader("Accept", "application/vnd.github.v3+json").post(formBody).build();
    Response response;
    try {
        response = mOkHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            String body = response.body().string();
            return body;
        }
    } catch (IOException e) {

        e.printStackTrace();
    } // execute

    return null;
}

From source file:com.quwu.xinwo.home_page.Crowd_FundingActivity.java

/**
 * ???/*from w  ww . j  a v a2  s.co m*/
 * */
public static String MyDoPost(String url, String normal_use, String warranty_period, String no_repair,
        String brand_new, String jingdong, String mainland_licensed, String since, String good_region,
        String city_region, String small_area, String twolevel_id, String three_id, String sortorder_id,
        String goods_lprice, String goods_hprice, String buy_userid, int pageNow, int pageSize) {
    OkHttpClient mOkHttpClient = new OkHttpClient();
    RequestBody formBody = new FormEncodingBuilder().add("normal_use", normal_use)
            .add("warranty_period", warranty_period).add("no_repair", no_repair).add("brand_new", brand_new)
            .add("jingdong", jingdong).add("mainland_licensed", mainland_licensed).add("since", since)
            .add("good_region", good_region).add("city_region", city_region).add("small_area", small_area)
            .add("twolevel_id", twolevel_id).add("three_id", three_id).add("sortorder_id", sortorder_id)
            .add("goods_lprice", goods_lprice).add("goods_hprice", goods_hprice).add("buy_userid", buy_userid)
            .add("pageNow", String.valueOf(pageNow)).add("pageSize", String.valueOf(pageSize)).build();

    //  header(name, value)
    // ?name?value?? addHeader(name, value)
    // ??
    Request request = new Request.Builder().url(url).header("User-Agent", "OkHttp Headers.java")
            .addHeader("Accept", "application/json; q=0.5")
            .addHeader("Accept", "application/vnd.github.v3+json").post(formBody).build();
    Response response;
    try {
        response = mOkHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            String body = response.body().string();
            return body;
        }
    } catch (IOException e) {

        e.printStackTrace();
    } // execute

    return null;
}

From source file:com.quwu.xinwo.home_page.DigitalActivity.java

/**
 * ???//from  ww w .  j  a v  a2 s.c  om
 * */
public static String MyDoPost(String url, String normal_use, String warranty_period, String no_repair,
        String brand_new, String jingdong, String mainland_licensed, String since, String good_region,
        String city_region, String small_area, String twolevel_id, String three_id, String sortorder_id,
        String goods_lprice, String goods_hprice, String buy_userid, int pageNow, int pageSize) {
    OkHttpClient mOkHttpClient = new OkHttpClient();
    RequestBody formBody = new FormEncodingBuilder().add("normal_use", normal_use)
            .add("warranty_period", warranty_period).add("no_repair", no_repair).add("brand_new", brand_new)
            .add("jingdong", jingdong).add("mainland_licensed", mainland_licensed).add("since", since)
            .add("good_region", good_region).add("city_region", city_region).add("small_area", small_area)
            .add("twolevel_id", twolevel_id).add("three_id", three_id).add("sortorder_id", sortorder_id)
            .add("goods_lprice", goods_lprice).add("goods_hprice", goods_hprice).add("buy_userid", buy_userid)
            .add("pageNow", String.valueOf(pageNow)).add("pageSize", String.valueOf(pageSize)).build();

    //  header(name, value)
    // ?name?value?? addHeader(name, value)
    // ??
    Request request = new Request.Builder().url(url).header("User-Agent", "OkHttp Headers.java")
            .addHeader("Accept", "application/json; q=0.5")
            .addHeader("Accept", "application/vnd.github.v3+json").post(formBody).build();
    Response response;
    try {
        response = mOkHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            String body = response.body().string();
            return body;
        }
    } catch (IOException e) {
        e.printStackTrace();
    } // execute
    return null;
}