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:net.qiujuer.common.okhttp.impl.RequestCallBuilder.java

License:Apache License

protected RequestBody createFormBody(StrParam... strParams) {
    FormEncodingBuilder formEncodingBuilder = new FormEncodingBuilder();
    formEncodingBuilder = buildFormBody(formEncodingBuilder);

    // Add values
    if (strParams != null && strParams.length > 0) {
        for (StrParam strParam : strParams) {
            if (strParam.key != null && strParam.value != null) {
                formEncodingBuilder.add(strParam.key, strParam.value);
                log("buildFormParam: key: " + strParam.key + " value: " + strParam.value);
            } else {
                log("buildFormParam: key: " + (strParam.key != null ? strParam.key : "null") + " value: "
                        + (strParam.value != null ? strParam.value : "null"));
            }// www .  j  a  v a 2  s . c  o m
        }
    }
    return formEncodingBuilder.build();
}

From source file:org.addhen.birudo.data.net.JenkinsClient.java

License:Apache License

public GcmRegistrationStatus sendGcmTokenToServer() throws Exception {
    if (mJenkinsUserEntity == null) {
        return null;
    }//from  www  .  j a  v  a 2 s.com

    // Retrieve crumbInfo
    CrumbInfoEntity crumbInfoEntity = retrieveCrumbInfo();
    if (crumbInfoEntity != null && crumbInfoEntity.isCsrfEnabled()) {
        setHeader(crumbInfoEntity.getCrumbRequestField(), crumbInfoEntity.getCrumb());
    }

    FormEncodingBuilder formEncodingBuilder = new FormEncodingBuilder();

    formEncodingBuilder.add("token", mJenkinsUserEntity.getSenderId());

    String credential = Credentials.basic(mJenkinsUserEntity.getUsername(), mJenkinsUserEntity.getToken());

    setHeader("Authorization", credential);

    setRequestBody(formEncodingBuilder.build());

    // Send to gcm/regsiter
    super.url = String.format("%sgcm/register", mJenkinsUserEntity.getUrl());
    setMethod(HttpMethod.POST);
    execute();

    final int statusCode = getResponse().code();
    GcmRegistrationStatus status = new GcmRegistrationStatus();
    status.setStatusCode(statusCode);
    if (statusCode != 200) {
        log("Sending registering token failed with satus code %s", statusCode);
        status.setStatus(false);
    } else {
        status.setStatus(true);
    }

    return status;
}

From source file:org.addhen.smssync.data.net.AppHttpClient.java

License:Open Source License

/**
 * Get HTTP Entity populated with data in a format specified by the current sync scheme
 *///from   w  w w .  ja  v  a 2 s .c  om
private void setHttpEntity(SyncScheme.SyncDataFormat format) throws Exception {
    RequestBody body;
    switch (format) {
    case JSON:
        body = RequestBody.create(JSON, DataFormatUtil.makeJSONString(getParams()));
        log("setHttpEntity format JSON");
        break;
    case XML:
        body = RequestBody.create(XML, DataFormatUtil.makeXMLString(getParams(), "payload", UTF_8.name()));
        log("setHttpEntity format XML");
        break;
    case URLEncoded:
        log("setHttpEntity format URLEncoded");
        FormEncodingBuilder formEncodingBuilder = new FormEncodingBuilder();
        List<HttpNameValuePair> params = getParams();
        for (HttpNameValuePair pair : params) {
            formEncodingBuilder.add(pair.getName(), pair.getValue());
        }
        body = formEncodingBuilder.build();
        break;
    default:
        throw new Exception("Invalid data format");
    }
    setRequestBody(body);
}

From source file:org.addhen.smssync.data.net.MessageHttpClient.java

License:Open Source License

/**
 * Get HTTP Entity populated with data in a format specified by the current sync scheme
 *///from  w ww. j a  v a 2 s.  c  om
private void setHttpEntity(SyncScheme.SyncDataFormat format) throws Exception {
    RequestBody body;
    switch (format) {
    case JSON:
        body = RequestBody.create(JSON, DataFormatUtil.makeJSONString(getParams()));
        log("setHttpEntity format JSON");
        mFileManager.appendAndClose("setHttpEntity format JSON");
        break;
    case XML:
        body = RequestBody.create(XML, DataFormatUtil.makeXMLString(getParams(), "payload", UTF_8.name()));
        log("setHttpEntity format XML");
        mFileManager.appendAndClose(mContext.getString(R.string.http_entity_format, "XML"));
        break;
    case URLEncoded:
        log("setHttpEntity format URLEncoded");
        FormEncodingBuilder formEncodingBuilder = new FormEncodingBuilder();
        List<HttpNameValuePair> params = getParams();
        for (HttpNameValuePair pair : params) {
            formEncodingBuilder.add(pair.getName(), pair.getValue());
        }
        mFileManager.appendAndClose(mContext.getString(R.string.http_entity_format, "URLEncoded"));
        body = formEncodingBuilder.build();
        break;
    default:
        mFileManager.appendAndClose(mContext.getString(R.string.invalid_data_format));
        throw new Exception("Invalid data format");
    }

    setRequestBody(body);

}

From source file:org.addhen.smssync.net.MessageSyncHttpClient.java

License:Open Source License

/**
 * Get HTTP Entity populated with data in a format specified by the current sync scheme
 *///from ww  w  .j a  v  a 2  s  . c  om
private void setHttpEntity(SyncDataFormat format) throws Exception {
    RequestBody body;
    switch (format) {
    case JSON:
        body = RequestBody.create(JSON, DataFormatUtil.makeJSONString(getParams()));
        log("setHttpEntity format JSON");
        Util.logActivities(context, "setHttpEntity format JSON");
        break;
    case XML:
        //TODO: Make parent node URL specific as well
        body = RequestBody.create(XML, DataFormatUtil.makeXMLString(getParams(), "payload", HTTP.UTF_8));
        log("setHttpEntity format XML");
        Util.logActivities(context, context.getString(R.string.http_entity_format, "XML"));
        break;
    case YAML:
        body = RequestBody.create(YAML, DataFormatUtil.makeYAMLString(getParams()));
        log("setHttpEntity format YAML");
        Util.logActivities(context, context.getString(R.string.http_entity_format, "YAML"));
        break;
    case URLEncoded:
        log("setHttpEntity format URLEncoded");
        FormEncodingBuilder formEncodingBuilder = new FormEncodingBuilder();
        List<NameValuePair> params = getParams();
        for (NameValuePair pair : params) {
            formEncodingBuilder.add(pair.getName(), pair.getValue());
        }
        Util.logActivities(context, context.getString(R.string.http_entity_format, "URLEncoded"));
        body = formEncodingBuilder.build();
        break;
    default:
        Util.logActivities(context, context.getString(R.string.invalid_data_format));
        throw new Exception("Invalid data format");
    }

    setRequestBody(body);

}

From source file:org.catrobat.catroid.web.ServerCalls.java

License:Open Source License

public String httpFormUpload(String url, Map<String, String> postValues) throws WebconnectionException {
    FormEncodingBuilder formEncodingBuilder = new FormEncodingBuilder();

    if (postValues != null) {
        for (Map.Entry<String, String> entry : postValues.entrySet()) {
            formEncodingBuilder.add(entry.getKey(), entry.getValue());
        }//from   w ww  .  j a  v  a 2  s.  c o  m
    }

    Request request = new Request.Builder().url(url).post(formEncodingBuilder.build()).build();

    try {
        Response response = okHttpClient.newCall(request).execute();
        return response.body().string();
    } catch (IOException ioException) {
        Log.e(TAG, Log.getStackTraceString(ioException));
        throw new WebconnectionException(WebconnectionException.ERROR_NETWORK,
                "Connection could not be established!");
    }
}

From source file:org.floens.chan.core.http.DeleteHttpCall.java

License:Open Source License

@Override
public void setup(Request.Builder requestBuilder) {
    FormEncodingBuilder formBuilder = new FormEncodingBuilder();
    formBuilder.add(Integer.toString(reply.no), "delete");
    if (onlyImageDelete) {
        formBuilder.add("onlyimgdel", "on");
    }//from  w ww.j av  a2 s .  com
    formBuilder.add("mode", "usrdel");
    formBuilder.add("pwd", reply.password);

    requestBuilder.url(ChanUrls.getDeleteUrl(reply.board));
    requestBuilder.post(formBuilder.build());
}

From source file:org.floens.chan.core.http.PassHttpCall.java

License:Open Source License

@Override
public void setup(Request.Builder requestBuilder) {
    FormEncodingBuilder formBuilder = new FormEncodingBuilder();

    formBuilder.add("act", "do_login");

    formBuilder.add("id", token);
    formBuilder.add("pin", pin);

    requestBuilder.url(ChanUrls.getPassUrl());
    requestBuilder.post(formBuilder.build());
}

From source file:org.floens.chan.core.manager.ReplyManager.java

License:Open Source License

public void postPass(String token, String pin, final PassListener passListener) {
    FormEncodingBuilder formBuilder = new FormEncodingBuilder();

    formBuilder.add("act", "do_login");

    formBuilder.add("id", token);
    formBuilder.add("pin", pin);

    Request.Builder request = new Request.Builder().url(ChanUrls.getPassUrl()).post(formBuilder.build());

    makeOkHttpCall(request, new Callback() {
        @Override/*from w ww.  j  a v a  2  s  . co  m*/
        public void onFailure(Request request, IOException e) {
            final PassResponse res = new PassResponse();
            res.isError = true;
            res.message = context.getString(R.string.pass_error);
            runUI(new Runnable() {
                public void run() {
                    passListener.onResponse(res);
                }
            });
        }

        @Override
        public void onResponse(Response response) throws IOException {
            if (!response.isSuccessful()) {
                onFailure(response.request(), null);
                return;
            }
            String responseString = response.body().string();
            response.body().close();

            final PassResponse res = new PassResponse();
            if (responseString.contains("Your device is now authorized")) {
                List<String> cookies = response.headers("Set-Cookie");
                String passId = null;
                for (String cookie : cookies) {
                    try {
                        List<HttpCookie> parsedList = HttpCookie.parse(cookie);
                        for (HttpCookie parsed : parsedList) {
                            if (parsed.getName().equals("pass_id") && !parsed.getValue().equals("0")) {
                                passId = parsed.getValue();
                            }
                        }
                    } catch (IllegalArgumentException ignored) {
                    }
                }
                if (passId != null) {
                    res.passId = passId;
                    res.message = "Success! Your device is now authorized.";
                } else {
                    res.isError = true;
                    res.message = "Could not get pass id";
                }
            } else {
                res.isError = true;
                if (responseString.contains("Your Token must be exactly 10 characters")) {
                    res.message = "Incorrect token";
                } else if (responseString.contains("You have left one or more fields blank")) {
                    res.message = "You have left one or more fields blank";
                } else if (responseString.contains("Incorrect Token or PIN")) {
                    res.message = "Incorrect Token or PIN";
                } else {
                    res.unknownError = true;
                }
            }

            runUI(new Runnable() {
                public void run() {
                    passListener.onResponse(res);
                }
            });
        }
    });
}

From source file:org.floens.chan.core.manager.ReplyManager.java

License:Open Source License

public void postDelete(final SavedReply reply, boolean onlyImageDelete, final DeleteListener listener) {
    FormEncodingBuilder formBuilder = new FormEncodingBuilder();
    formBuilder.add(Integer.toString(reply.no), "delete");
    if (onlyImageDelete) {
        formBuilder.add("onlyimgdel", "on");
    }/*from ww  w .  jav  a2s  .co  m*/
    formBuilder.add("mode", "usrdel");
    formBuilder.add("pwd", reply.password);

    Request.Builder request = new Request.Builder().url(ChanUrls.getDeleteUrl(reply.board))
            .post(formBuilder.build());

    makeOkHttpCall(request, new Callback() {
        @Override
        public void onFailure(Request request, IOException e) {
            final DeleteResponse res = new DeleteResponse();
            res.isNetworkError = true;
            runUI(new Runnable() {
                @Override
                public void run() {
                    listener.onResponse(res);
                }
            });
        }

        @Override
        public void onResponse(Response response) throws IOException {
            if (!response.isSuccessful()) {
                onFailure(response.request(), null);
                return;
            }
            String responseString = response.body().string();
            response.body().close();

            final DeleteResponse res = new DeleteResponse();
            res.responseData = responseString;

            if (responseString.contains("You must wait longer before deleting this post")) {
                res.isUserError = true;
                res.isTooSoonError = true;
            } else if (responseString.contains("Password incorrect")) {
                res.isUserError = true;
                res.isInvalidPassword = true;
            } else if (responseString.contains("You cannot delete a post this old")) {
                res.isUserError = true;
                res.isTooOldError = true;
            } else if (responseString.contains("Updating index")) {
                res.isSuccessful = true;
            }

            runUI(new Runnable() {
                @Override
                public void run() {
                    listener.onResponse(res);
                }
            });
        }
    });
}