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:clienteconecta.pila.java

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    if (!this.jTextField1.getText().equals("") && esNumero(this.jTextField1.getText())) {
        String parametro = this.jTextField1.getText();
        RequestBody formBody = new FormEncodingBuilder().add("parametro", parametro).build();
        String salida = getString("push", formBody);
        String cmd = "cmd /c dot -Tpng C:\\Users\\Ottoniel\\Desktop\\ytemporal\\pila.dot > C:\\Users\\Ottoniel\\Desktop\\ytemporal\\pila.png";
        try {// w ww .j a va2s  .  c  o m
            Process child = Runtime.getRuntime().exec(cmd);
        } catch (IOException ex) {
            Logger.getLogger(lista.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.jTextField1.setText(""); // TODO add your handling code here:
    } //GEN-LAST:event_jButton1ActionPerformed
}

From source file:clienteconecta.pila.java

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
    String parametro = " xdd";
    RequestBody formBody = new FormEncodingBuilder().add("parametro", parametro).build();
    String salida = getString("pop", formBody);
    System.out.println(salida);/*from  w ww  . j ava2 s .  c o  m*/
    String cmd = "cmd /c dot -Tpng C:\\Users\\Ottoniel\\Desktop\\ytemporal\\pila.dot > C:\\Users\\Ottoniel\\Desktop\\ytemporal\\pila.png";
    try {
        Process child = Runtime.getRuntime().exec(cmd);
    } catch (IOException ex) {
        Logger.getLogger(lista.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:cn.finalteam.okhttpfinal.RequestParams.java

License:Apache License

protected RequestBody getRequestBody() {
    RequestBody body = null;/*  ww  w . j a  v a2s .  c  om*/
    if (jsonBody != null) {
        body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonBody.toJSONString());
    } else if (requestBody != null) {
        body = requestBody;
    } else if (fileParams.size() > 0) {
        boolean hasData = false;
        MultipartBuilder builder = new MultipartBuilder();
        builder.type(MultipartBuilder.FORM);
        for (ConcurrentHashMap.Entry<String, String> entry : urlParams.entrySet()) {
            builder.addFormDataPart(entry.getKey(), entry.getValue());
            hasData = true;
        }

        for (ConcurrentHashMap.Entry<String, FileWrapper> entry : fileParams.entrySet()) {
            FileWrapper file = entry.getValue();
            if (file != null) {
                hasData = true;
                builder.addFormDataPart(entry.getKey(), file.getFileName(),
                        RequestBody.create(file.getMediaType(), file.getFile()));
            }
        }
        if (hasData) {
            body = builder.build();
        }
    } else {
        FormEncodingBuilder builder = new FormEncodingBuilder();
        boolean hasData = false;
        for (ConcurrentHashMap.Entry<String, String> entry : urlParams.entrySet()) {
            builder.add(entry.getKey(), entry.getValue());
            hasData = true;
        }
        if (hasData) {
            body = builder.build();
        }
    }

    return body;
}

From source file:cn.wochu.wh.net.OkHttpClientManager.java

License:Apache License

@SuppressWarnings("static-access")
private Request buildPostRequest(String url, Param[] params) {
    if (params == null) {
        params = new Param[0];
    }//from w  w  w .  ja va  2  s  .  co  m
    FormEncodingBuilder builder = new FormEncodingBuilder();
    for (Param param : params) {
        builder.add(param.key, param.value);
    }
    RequestBody requestBody = builder.build().create(MediaType.parse("application/json; charset=utf-8"),
            "json");
    return new Request.Builder().url(url).post(requestBody).build();
}

From source file:cn.wochu.wh.net.OkHttpClientManager.java

License:Apache License

private Request buildPostRequest(String url, String params) {

    FormEncodingBuilder builder = new FormEncodingBuilder();

    RequestBody requestBody = builder.build().create(MediaType.parse("application/json; charset=utf-8"),
            params);/*from  www.j  a  v  a  2 s .  co m*/
    return new Request.Builder().url(url).post(requestBody).build();
}

From source file:cn.wochu.wh.net.OkHttpClientManager.java

License:Apache License

private Request buildPostRequest(String url, Param[] params, String content) {
    if (params == null) {
        params = new Param[0];
    }/*from   w  ww  . java 2s. com*/
    FormEncodingBuilder builder = new FormEncodingBuilder();
    for (Param param : params) {
        builder.add(param.key, param.value);
    }
    RequestBody requestBody = builder.build().create(MediaType.parse("application/json; charset=utf-8"),
            content);
    return new Request.Builder().url(url).post(requestBody).build();
}

From source file:com.ae.apps.pnrstatus.service.NetworkService.java

License:Open Source License

public String doPostRequest(final String targetUrl, final Map<String, String> headers,
        final Map<String, String> params) throws StatusException {
    //RequestBody requestBody = RequestBody.create(WEB_FORM, "");
    try {/*from  w  ww. j av  a  2  s.  com*/
        Request.Builder requestBuilder = new Request.Builder().url(targetUrl);
        if (null != headers) {
            for (String key : headers.keySet()) {
                requestBuilder.addHeader(key, String.valueOf(headers.get(key)));
            }
        }
        //--
        FormEncodingBuilder formEncodingBuilder = new FormEncodingBuilder();
        if (null != params) {
            for (String key : params.keySet()) {
                formEncodingBuilder.add(key, String.valueOf(params.get(key)));
            }
        }

        RequestBody formBody = formEncodingBuilder.build();
        Request request = requestBuilder.url(targetUrl).post(formBody).build();

        Response response = client.newCall(request).execute();
        return response.body().string();
    } catch (IOException ex) {
        throw new StatusException(ex.getMessage(), StatusException.ErrorCodes.URL_ERROR);
    }
}

From source file:com.anony.okhttp.sample.PostForm.java

License:Apache License

public void run() throws Exception {
    RequestBody formBody = new FormEncodingBuilder().add("search", "Jurassic Park").build();
    Request request = new Request.Builder().url("https://en.wikipedia.org/w/index.php").post(formBody).build();

    Response response = client.newCall(request).execute();
    if (!response.isSuccessful())
        throw new IOException("Unexpected code " + response);

    System.out.println(response.body().string());
}

From source file:com.appstarter.utils.WebUtils.java

License:Apache License

public static String doHttpPost(String url, List<NameValuePair> params) throws AppStarterException {
    if (BuildConfig.DEBUG) {
        Log.d(TAG, "doHttpPost - url: " + debugRequest(url, params));
    }/*  www . j av a  2 s. c o m*/

    String ret = "";

    OkHttpClient client = new OkHttpClient();
    client.setConnectTimeout(15, TimeUnit.SECONDS); // connect timeout
    client.setReadTimeout(15, TimeUnit.SECONDS); // socket timeout

    FormEncodingBuilder builder = new FormEncodingBuilder();
    for (NameValuePair nvp : params) {
        builder.add(nvp.getName(), nvp.getValue());
    }
    RequestBody formBody = builder.build();

    try {
        Request request = new Request.Builder().url(new URL(url))
                //               .header("User-Agent", "OkHttp Headers.java")
                //               .addHeader("Accept", "application/json; q=0.5")
                .post(formBody).build();

        Response response = client.newCall(request).execute();
        if (!response.isSuccessful()) {
            String debugMessage = "doHttpPost - OkHttp.Response is not successful - " + response.message()
                    + " (" + response.code() + ")";
            throw new AppStarterException(AppStarterException.ERROR_SERVER, debugMessage);
        }
        ret = response.body().string();
    } catch (IOException e) {
        throw new AppStarterException(e, AppStarterException.ERROR_NETWORK_GET);
    }

    return ret;
}

From source file:com.codemodlabs.coordinate.Token.java

License:Open Source License

private void getTokenFromServer(Callback callback) throws IOException {

    class GetTokenTask extends AsyncTask {

        @Override/*from w  w w. ja  va2s .  c o m*/
        protected Object doInBackground(Object[] objects) {
            try {
                final String access_code = ((Callback) objects[0]).access_code;
                String url_string = "https://login.uber.com/oauth/token";

                OkHttpClient client = new OkHttpClient();

                // Ignore invalid SSL endpoints.
                client.setHostnameVerifier(new HostnameVerifier() {
                    @Override
                    public boolean verify(String s, SSLSession sslSession) {
                        return true;
                    }
                });

                RequestBody formBody = new FormEncodingBuilder().add("client_secret", client_secret)
                        .add("client_id", client_id).add("grant_type", "authorization_code")
                        .add("redirect_uri", redirect_url).add("code", access_code).build();
                Request request = new Request.Builder().url(url_string).post(formBody).build();

                Response response = client.newCall(request).execute();
                String response_body = response.body().string();

                Gson gson = new Gson();
                token = gson.fromJson(response_body, Token.class);
                return token;
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Object result) {
            super.onPostExecute(result);
            Token token = (Token) result;

            Intent resultIntent = new Intent();
            resultIntent.putExtra("access_token", token.access_token);
            resultIntent.putExtra("expires_in", token.expires_in);
            resultIntent.putExtra("token_type", token.token_type);
            resultIntent.putExtra("refresh_token", token.refresh_token);
            resultIntent.putExtra("scope", token.scope);

            setResult(2, resultIntent);
            finish();
        }
    }
    new GetTokenTask().execute(callback);
}