Example usage for com.squareup.okhttp OkHttpClient newCall

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

Introduction

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

Prototype

public Call newCall(Request request) 

Source Link

Document

Prepares the request to be executed at some point in the future.

Usage

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//from w w w. jav  a  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.phattn.vnexpressnews.io.OkHttpStack.java

License:Open Source License

@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
        throws IOException, AuthFailureError {

    OkHttpClient client = mClient.clone();
    int timeoutMs = request.getTimeoutMs();
    client.setConnectTimeout(timeoutMs, TimeUnit.MILLISECONDS);
    client.setReadTimeout(timeoutMs, TimeUnit.MILLISECONDS);
    client.setWriteTimeout(timeoutMs, TimeUnit.MILLISECONDS);

    com.squareup.okhttp.Request.Builder okHttpRequestBuilder = new com.squareup.okhttp.Request.Builder();
    okHttpRequestBuilder.url(request.getUrl());

    Map<String, String> headers = request.getHeaders();
    for (final String name : headers.keySet()) {
        okHttpRequestBuilder.addHeader(name, headers.get(name));
    }/*  www . j  av a2s .com*/
    for (final String name : additionalHeaders.keySet()) {
        okHttpRequestBuilder.addHeader(name, additionalHeaders.get(name));
    }

    setConnectionParametersForRequest(okHttpRequestBuilder, request);

    com.squareup.okhttp.Request okHttpRequest = okHttpRequestBuilder.build();
    Call okHttpCall = client.newCall(okHttpRequest);
    Response okHttpResponse = okHttpCall.execute();

    StatusLine responseStatus = new BasicStatusLine(parseProtocol(okHttpResponse.protocol()),
            okHttpResponse.code(), okHttpResponse.message());
    BasicHttpResponse response = new BasicHttpResponse(responseStatus);
    response.setEntity(entityFromOkHttpResponse(okHttpResponse));

    Headers responseHeaders = okHttpResponse.headers();
    for (int i = 0, length = responseHeaders.size(); i < length; i++) {
        final String name = responseHeaders.name(i);
        final String value = responseHeaders.value(i);
        if (name != null) {
            response.addHeader(new BasicHeader(name, value));
        }
    }

    return response;
}

From source file:com.piusvelte.okoauth.Helper.java

License:Apache License

@NonNull
public void getTokenSecret(@NonNull OkHttpClient client) {
    Request request = new RequestTokenRequestBuilder(mConsumerKey, mConsumerSecret, mCallbackUrl)
            .url(mTokenRequestUrl).build();
    Response response;//from w w w .j ava  2  s . co m

    try {
        response = client.newCall(request).execute();

        if (response.isSuccessful() && response.body() != null) {
            String content = response.body().string();

            if (!TextUtils.isEmpty(content)) {
                String[] keyValuePairs = content.split("&");

                if (keyValuePairs.length > 1) {
                    for (String keyValuePair : keyValuePairs) {
                        String[] keyValuePairParts = keyValuePair.split("=");

                        if (keyValuePairParts.length > 1) {
                            String key = keyValuePairParts[0];
                            String value = keyValuePairParts[1];

                            if (RequestBuilder.OAuthParameter.oauth_token.name().equals(key)) {
                                mToken = value;
                            } else if ("oauth_token_secret".equals(key)) {
                                mTokenSecret = value;
                            } else if ("oauth_callback_confirmed".equals(key)) {
                                mIsOAuth10a = Boolean.TRUE.toString().equals(value);
                            }
                        }
                    }
                }
            }
        } else {
            if (BuildConfig.DEBUG) {
                Log.e(TAG, "unsuccessful getting token" + response.toString());
            }
        }
    } catch (IOException e) {
        if (BuildConfig.DEBUG) {
            Log.e(TAG, "failed getting token", e);
        }
    }
}

From source file:com.piusvelte.okoauth.Helper.java

License:Apache License

public boolean getAccessToken(@NonNull OkHttpClient client, @NonNull String authenticatedUrl) {
    if (!TextUtils.isEmpty(authenticatedUrl)) {
        String verifier = getParamValue(authenticatedUrl, "oauth_verifier");

        if (!TextUtils.isEmpty(verifier)) {
            Request request = new AccessTokenRequestBuilder(mConsumerKey, mConsumerSecret, mToken, mTokenSecret,
                    mIsOAuth10a ? verifier : null).url(mAccessTokenUrl).build();
            Response response;/*ww w  . j  a va 2s .co  m*/

            try {
                response = client.newCall(request).execute();

                if (response.isSuccessful() && response.body() != null) {
                    String content = response.body().string();

                    if (!TextUtils.isEmpty(content)) {
                        String[] keyValuePairs = content.split("&");

                        if (keyValuePairs.length > 1) {
                            for (String keyValuePair : keyValuePairs) {
                                String[] keyValuePairParts = keyValuePair.split("=");

                                if (keyValuePairParts.length > 1) {
                                    String key = keyValuePairParts[0];
                                    String value = keyValuePairParts[1];

                                    if (RequestBuilder.OAuthParameter.oauth_token.name().equals(key)) {
                                        mToken = value;
                                    } else if ("oauth_token_secret".equals(key)) {
                                        mTokenSecret = value;
                                    }
                                }
                            }

                            return true;// TODO actually check mToken and mTokenSecret
                        }
                    }
                }
            } catch (IOException e) {
                if (BuildConfig.DEBUG) {
                    Log.e(TAG, "failed getting access token", e);
                }
            }
        }
    }

    return false;
}

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

/**
 * ?/*from  w w  w.j  a va  2s  .com*/
 * */
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

/**
 * ???//w  w  w .j  ava2s  .c  o 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.  ja  v  a2  s. c o 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   www  .  j a  va  2  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.rsu.nuttanun.testdrivingvlicense.ConfirmScoreActivity.java

public void clickOKConfirm(View view) {

    String urlPHP = "http://swiftcodingthai.com/toey/add_score.php";

    OkHttpClient okHttpClient = new OkHttpClient();
    RequestBody requestBody = new FormEncodingBuilder().add("isAdd", "true").add("id_login", loginStrings[0])
            .add("Date", dateString).add("Score", scoreString).build();
    Request.Builder builder = new Request.Builder();
    Request request = builder.url(urlPHP).post(requestBody).build();
    Call call = okHttpClient.newCall(request);
    call.enqueue(new Callback() {
        @Override/*from   w w  w.j  a  v a2 s  . c o  m*/
        public void onFailure(Request request, IOException e) {

        }

        @Override
        public void onResponse(Response response) throws IOException {

            Intent intent = new Intent(ConfirmScoreActivity.this, ScoreListView.class);
            intent.putExtra("login", loginStrings);
            startActivity(intent);
            finish();
        }
    });

}

From source file:com.rsu.nuttanun.testdrivingvlicense.ScoreListView.java

private void createListView() {

    String urlPHP = "http://swiftcodingthai.com/toey/get_score_where.php";
    OkHttpClient okHttpClient = new OkHttpClient();
    RequestBody requestBody = new FormEncodingBuilder().add("isAdd", "true").add("id_login", loginStrings[0])
            .build();//from w  ww . ja va  2 s  .co  m
    Request.Builder builder = new Request.Builder();
    Request request = builder.url(urlPHP).post(requestBody).build();
    Call call = okHttpClient.newCall(request);
    call.enqueue(new Callback() {
        @Override
        public void onFailure(Request request, IOException e) {

        }

        @Override
        public void onResponse(Response response) throws IOException {

            String strResponse = response.body().string();
            Log.d("10AugV2", "strResponse ==>" + strResponse);

            try {

                JSONArray jsonArray = new JSONArray(strResponse);

                String[] dateStrings = new String[jsonArray.length()];
                String[] scoreStrings = new String[jsonArray.length()];

                for (int i = 0; i < jsonArray.length(); i++) {

                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    dateStrings[i] = jsonObject.getString("Date");
                    scoreStrings[i] = jsonObject.getString("Score") + " ?";

                } //for

                CoaurseAdapter coaurseAdapter = new CoaurseAdapter(ScoreListView.this, 1, dateStrings,
                        scoreStrings);
                listView.setAdapter(coaurseAdapter);

            } catch (Exception e) {
                e.printStackTrace();

            }

        } // onRespose
    });

}