Example usage for com.squareup.okhttp FormEncodingBuilder FormEncodingBuilder

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

Introduction

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

Prototype

FormEncodingBuilder

Source Link

Usage

From source file:com.liferay.mobile.android.auth.CookieSignIn.java

License:Open Source License

protected String getBody(String username, String password) throws IOException {

    RequestBody formBody = new FormEncodingBuilder().add("login", username).add("password", password).build();

    Buffer buffer = new Buffer();
    formBody.writeTo(buffer);//ww  w. j a v a 2  s  .  c o m

    return buffer.readUtf8();
}

From source file:com.liferay.mobile.sdk.auth.CookieSignIn.java

License:Open Source License

protected static String getBody(BasicAuthentication auth) throws IOException {

    RequestBody formBody = new FormEncodingBuilder().add("login", auth.username())
            .add("password", auth.password()).build();

    Buffer buffer = new Buffer();
    formBody.writeTo(buffer);/*from   w  ww . java  2 s  .c o  m*/

    return buffer.readUtf8();
}

From source file:com.liuguangqiang.asyncokhttp.RequestParams.java

License:Apache License

public RequestBody toRequestBody() {
    FormEncodingBuilder builder = new FormEncodingBuilder();

    for (HashMap.Entry<String, String> entry : params.entrySet()) {
        builder.add(entry.getKey(), entry.getValue());
    }//  ww  w  .j a va 2s.  com

    return builder.build();

}

From source file:com.mcxiaoke.next.http.NextRequest.java

License:Apache License

protected RequestBody getRequestBody() throws IOException {
    if (!supportBody()) {
        return null;
    }/*from   w ww  .  j ava2s.  c  o  m*/
    if (body != null) {
        return RequestBody.create(HttpConsts.MEDIA_TYPE_OCTET_STREAM, body);
    }
    RequestBody requestBody;
    if (hasParts()) {
        final MultipartBuilder multipart = new MultipartBuilder();
        for (final BodyPart part : parts()) {
            if (part.getBody() != null) {
                multipart.addFormDataPart(part.getName(), part.getFileName(), part.getBody());
            }
        }
        for (Map.Entry<String, String> entry : form().entrySet()) {
            final String key = entry.getKey();
            final String value = entry.getValue();
            multipart.addFormDataPart(key, value == null ? "" : value);
        }
        requestBody = multipart.type(MultipartBuilder.FORM).build();
    } else if (hasForms()) {
        final FormEncodingBuilder bodyBuilder = new FormEncodingBuilder();
        for (Map.Entry<String, String> entry : form().entrySet()) {
            final String key = entry.getKey();
            final String value = entry.getValue();
            bodyBuilder.add(key, value == null ? "" : value);
        }
        requestBody = bodyBuilder.build();
    } else {
        requestBody = null;
    }
    return requestBody;
}

From source file:com.meyersj.tamalenow.location.UpdateLocationService.java

License:Open Source License

public void postLocation(Location location) {
    String date = Utils.dateFormat.format(new Date());
    Log.d(TAG, date);//from  w w  w . ja  v a2s. c o m
    RequestBody formBody = new FormEncodingBuilder().add("vendor_id", "test_vendor")
            .add("tstamp", Utils.dateFormat.format(new Date()))
            .add("lat", String.valueOf(location.getLatitude()))
            .add("lon", String.valueOf(location.getLongitude())).build();

    Request request = new Request.Builder().url(app.getAPIBase() + "/" + Endpoints.LOCATION).post(formBody)
            .build();

    httpClient.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Request request, IOException e) {
            Log.d(TAG, "failure");
        }

        @Override
        public void onResponse(Response response) throws IOException {
            if (!response.isSuccessful())
                throw new IOException("Unexpected code " + response);
            Log.d(TAG, response.body().string());
        }
    });

}

From source file:com.near.chimerarevo.fragments.CommentsFragment.java

License:Apache License

@SuppressLint("SetJavaScriptEnabled")
private void loadDisqusOAuth() {
    if (getActivity().getSharedPreferences(Constants.PREFS_TAG, Context.MODE_PRIVATE)
            .getString(Constants.KEY_REFRESH_TOKEN, "").length() > 1) {

        RequestBody formBody = new FormEncodingBuilder().add("grant_type", "refresh_token")
                .add("client_id", Constants.DISQUS_API_KEY).add("client_secret", Constants.DISQUS_API_SECRET)
                .add("refresh_token",
                        getActivity().getSharedPreferences(Constants.PREFS_TAG, Context.MODE_PRIVATE)
                                .getString(Constants.KEY_REFRESH_TOKEN, ""))
                .build();//  ww w  .jav  a2  s. co m

        Request request = new Request.Builder().url(Constants.DISQUS_TOKEN_URL).post(formBody).tag(FRAGMENT_TAG)
                .build();

        if (mDialog == null)
            mDialog = ProgressDialogUtils.getInstance(getActivity(), R.string.text_login);
        else
            mDialog = ProgressDialogUtils.modifyInstance(mDialog, R.string.text_login);

        mDialog.show();
        OkHttpUtils.getInstance().newCall(request).enqueue(new PostAccessTokenCallback());

        return;
    }

    final Dialog dialog = new Dialog(getActivity());

    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.webview_layout);
    dialog.setCancelable(true);

    dialog.setOnCancelListener(new OnCancelListener() {
        @Override
        public void onCancel(DialogInterface arg0) {
            isDialogOpen = false;
            mFab.show();
        }
    });
    dialog.setOnDismissListener(new OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface arg0) {
            isDialogOpen = false;
            mFab.show();
        }
    });

    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT)
        CookieManager.getInstance().removeAllCookies(null);
    else {
        CookieSyncManager.createInstance(getActivity());
        CookieManager.getInstance().removeAllCookie();
    }

    WebView wv = (WebView) dialog.findViewById(R.id.webview);
    wv.getSettings().setJavaScriptEnabled(true);
    wv.getSettings().setSaveFormData(false);
    wv.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            dialog.findViewById(R.id.progressContainer).setVisibility(View.GONE);
            dialog.findViewById(R.id.webViewContainer).setVisibility(View.VISIBLE);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            dialog.findViewById(R.id.progressContainer).setVisibility(View.VISIBLE);
            dialog.findViewById(R.id.webViewContainer).setVisibility(View.GONE);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            boolean state = super.shouldOverrideUrlLoading(view, url);
            if (url.contains(Constants.SITE_URL)) {
                String code = url.split("code=")[1];

                RequestBody formBody = new FormEncodingBuilder().add("grant_type", "authorization_code")
                        .add("client_id", Constants.DISQUS_API_KEY)
                        .add("client_secret", Constants.DISQUS_API_SECRET)
                        .add("redirect_uri", Constants.SITE_URL).add("code", code).build();

                Request request = new Request.Builder().url(Constants.DISQUS_TOKEN_URL).post(formBody)
                        .tag(FRAGMENT_TAG).build();

                if (mDialog == null)
                    mDialog = ProgressDialogUtils.getInstance(getActivity(), R.string.text_login);
                else
                    mDialog = ProgressDialogUtils.modifyInstance(mDialog, R.string.text_login);

                dialog.dismiss();
                mDialog.show();

                OkHttpUtils.getInstance().newCall(request).enqueue(new PostAccessTokenCallback());
            }
            return state;
        }

        @Override
        public void onReceivedSslError(WebView view, @NonNull SslErrorHandler handler, SslError error) {
            handler.proceed();
        }

    });
    wv.loadUrl(URLUtils.getDisqusAuthUrl());

    isDialogOpen = true;
    mFab.hide();
    dialog.show();
}

From source file:com.near.chimerarevo.fragments.CommentsFragment.java

License:Apache License

private void createCommentDialog(final int parent) {
    CustomDialog.Builder builder;//from ww w.j  a va2 s. c  o m

    if (parentId == -1)
        builder = new CustomDialog.Builder(getActivity(), getResources().getString(R.string.text_addcomment),
                getResources().getString(R.string.text_sendcomment));
    else
        builder = new CustomDialog.Builder(getActivity(), getResources().getString(R.string.text_replycomment),
                getResources().getString(R.string.text_sendcomment));

    builder.positiveColorRes(android.R.color.holo_blue_bright);
    builder.titleColorRes(android.R.color.holo_red_light);
    builder.titleAlignment(BaseDialog.Alignment.CENTER);
    builder.negativeText(getResources().getString(R.string.text_cancel));
    builder.negativeColorRes(android.R.color.holo_red_dark);

    final CustomDialog dialog = builder.build();
    dialog.setCustomView(
            LayoutInflater.from(getActivity()).inflate(R.layout.dialog_comment_layout, null, false));
    dialog.setCancelable(false);

    dialog.setClickListener(new CustomDialog.ClickListener() {
        @Override
        public void onConfirmClick() {
            EditText text = (EditText) dialog.findViewById(R.id.comment_edittext);

            isDialogOpen = false;

            if (checkEditText(text, getResources())) {
                SnackbarUtils
                        .showMultiShortSnackbar(getActivity(),
                                getResources().getString(R.string.text_textnotempty), CommentsFragment.this)
                        .dismissOnActionClicked(true).actionLabel(getResources().getString(R.string.text_close))
                        .actionColor(getResources().getColor(android.R.color.holo_red_dark))
                        .show(getActivity());

                return;
            }

            String commentText = text.getText().toString() + "\n\n"
                    + getResources().getString(R.string.text_sentfromapp);

            FormEncodingBuilder feb = new FormEncodingBuilder().add("access_token", access_token)
                    .add("api_key", Constants.DISQUS_API_KEY).add("api_secret", Constants.DISQUS_API_SECRET)
                    .add("message", commentText).add("thread", thread_id);

            if (parent != -1)
                feb.add("parent", String.valueOf(parent));

            Request request = new Request.Builder().url(Constants.DISQUS_POST_COMMENT).post(feb.build())
                    .tag(FRAGMENT_TAG).build();

            if (mDialog == null)
                mDialog = ProgressDialogUtils.getInstance(getActivity(), R.string.text_sending_comment);
            else
                mDialog = ProgressDialogUtils.modifyInstance(mDialog, R.string.text_sending_comment);

            mDialog.show();

            OkHttpUtils.getInstance().newCall(request).enqueue(new PostCommentCallback());
        }

        @Override
        public void onCancelClick() {
            isDialogOpen = false;
            mFab.show();
        }
    });

    isDialogOpen = true;
    mFab.hide();
    dialog.show();
}

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

/**
 * ?//  ww w .  j a v  a2s .c  o m
 * */
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

/**
 * ???/*from   w w  w  .  java 2s .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  ww  w .j  a  va  2 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;
}