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:servletRegistrar.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*from  w w  w. j  a v  a 2s.  c om*/
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    String usuario = request.getParameter("usuario");
    String password = request.getParameter("password");
    String empresa = request.getParameter("empresa");
    String departameto = request.getParameter("departamento");
    RequestBody formBody = new FormEncodingBuilder().add("empresa", empresa).add("departamneto", departameto)
            .add("nombre", usuario).add("password", password).add("usuario", usuario).build();
    try {
        URL url = new URL(puerto + "insertarMatriz");
        Request request2 = new Request.Builder().url(url).post(formBody).build();
        respuesta = webClient.newCall(request2).execute();
        System.out.println("1" + respuesta);
        String response_srting = respuesta.body().string();
        System.out.println(response_srting);
        imp = response_srting;
    } catch (MalformedURLException ex) {
        java.util.logging.Logger.getLogger(servletRegistrar.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        java.util.logging.Logger.getLogger(servletRegistrar.class.getName()).log(Level.SEVERE, null, ex);
    }

    //        RequestBody formBody = new FormEncodingBuilder().add("nombre", usu).build();
    //        try {
    //            URL url = new URL(puerto + "prueba");
    //            Request request2 = new Request.Builder().url(url).post(formBody).build();
    //            respuesta = webClient.newCall(request2).execute();
    //            String response_string = respuesta.body().string();
    //            System.out.println(response_string);
    //            imp= response_string;
    //        } catch (MalformedURLException ex) {
    //            java.util.logging.Logger.getLogger(servletRegistrar.class.getName()).log(Level.SEVERE,null,ex);
    //        } catch (IOException ex) {
    //            java.util.logging.Logger.getLogger(servletRegistrar.class.getName()).log(Level.SEVERE,null,ex);  
    //        }

    try (PrintWriter out = response.getWriter()) {
        /* TODO output your page here. You may use following sample code. */
        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet servletRegistrar</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Aparecio " + imp + "</h1>");
        out.println("</body>");
        out.println("</html>");
    }
}

From source file:Servlet4.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request/*from   ww w .j  a v  a2 s  .  c o m*/
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String usuario = String.valueOf(request.getSession().getAttribute("sesionusuario"));
    String empresa = String.valueOf(request.getSession().getAttribute("sesionempresa"));
    String departamento = String.valueOf(request.getSession().getAttribute("sesiondepartamento"));
    RequestBody formBody = new FormEncodingBuilder()

            .add("parametro", "hola")
            //.add("dato3","Roberto")
            .build();
    r = getString("Catalogo", formBody);

    processRequest(request, response);
}

From source file:Servlet3.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request//from  w ww.  j  av a  2 s .co  m
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    LinkedList lin = new LinkedList();
    lin.add(0, "a");
    lin.add(1, "b");
    lin.add(2, "c");
    lin.add(3, "d");
    lin.add(4, "e");
    lin.add(5, "f");
    lin.add(6, "g");
    lin.add(7, "h");
    lin.add(8, "i");
    lin.add(9, "j");
    lin.add(10, "k");
    lin.add(11, "l");
    lin.add(12, "m");
    lin.add(13, "n");
    lin.add(14, "o");
    lin.add(15, "p");
    lin.add(16, "q");
    lin.add(17, "r");
    lin.add(18, "s");
    lin.add(19, "t");
    lin.add(20, "u");
    lin.add(21, "v");
    lin.add(22, "w");
    lin.add(23, "x");
    lin.add(24, "y");
    lin.add(25, "z");
    Random r = new Random();
    String alfanumerico = "";
    String numero = "";
    String numero2 = "";
    int suma1 = 0;
    int suma2 = 0;
    for (int i = 0; i < 7; i++) {

        alfanumerico += lin.get(r.nextInt(lin.size())).toString() + String.valueOf(r.nextInt(9));
    }
    alfanumerico += lin.get(r.nextInt(lin.size())).toString();
    //System.out.println(concatenar);

    char vector[] = alfanumerico.toCharArray();

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

        numero += String.valueOf(lin.indexOf(String.valueOf(vector[i])));
    }

    String caracter[] = numero.split("-");

    suma1 += Integer.parseInt(caracter[0] + caracter[1]);
    suma2 += Integer.parseInt(caracter[2] + caracter[3]) + suma1;
    numero2 = String.valueOf(suma1);
    //System.out.println(suma1);
    //System.out.println(alfanumerico);

    String nombreactivo = request.getParameter("name");
    String descripcion = request.getParameter("area");
    String usuario = String.valueOf(request.getSession().getAttribute("sesionusuario"));
    String empresa = String.valueOf(request.getSession().getAttribute("sesionempresa"));
    String departamento = String.valueOf(request.getSession().getAttribute("sesiondepartamento"));
    String pass = String.valueOf(request.getSession().getAttribute("sesionpass"));

    RequestBody formBody = new FormEncodingBuilder()

            .add("usuario", usuario.trim()).add("empresa", empresa.trim())
            .add("departamento", departamento.trim()).add("identificador", numero2.trim())
            .add("contrasenia", pass.trim()).build();

    concatenar = getString("agregarAVL", formBody);

    //response.sendRedirect("Activos.jsp");
    processRequest(request, response);
    // response(response,con);
}

From source file:Servlet1.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request//w  w w . j  ava  2s .c om
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String usuario = request.getParameter("usuario");
    String pass = request.getParameter("clave");
    String nombre = request.getParameter("completo");
    String empresa = request.getParameter("empresa");
    String departamento = request.getParameter("departamento");

    RequestBody formBody = new FormEncodingBuilder()

            .add("usuario", usuario).add("contrasenia", pass).add("nombre", nombre).add("empresa", empresa)
            .add("departamento", departamento)

            .build();

    r = getString("matrizAgregar", formBody);
    //System.out.println(r + "---");

    //sponse(response,r);
    processRequest(request, response);

}

From source file:Servlet2.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request/*from   w w w  .j av  a 2  s  . com*/
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)

        throws ServletException, IOException {
    String usuario = request.getParameter("name");
    String pass = request.getParameter("pass");
    String empresa = request.getParameter("empresa");
    String departamento = request.getParameter("departamento");

    RequestBody formBody = new FormEncodingBuilder()

            .add("usuario", usuario).add("contrasenia", pass).add("empresa", empresa)
            .add("departamento", departamento)
            //.add("dato3","Roberto")
            .build();
    r = getString("Login", formBody);

    if (r.equalsIgnoreCase("true")) {
        //boolean sesion=true;

        HttpSession sesion = request.getSession(true);
        HttpSession sesion1 = request.getSession(true);
        HttpSession sesion2 = request.getSession(true);
        HttpSession sesion3 = request.getSession(true);
        sesion.setAttribute("sesionusuario", usuario);
        sesion1.setAttribute("sesionempresa", empresa);
        sesion2.setAttribute("sesiondepartamento", departamento);
        sesion3.setAttribute("sesionpass", pass);
        response.sendRedirect("menu.jsp");
        //processRequest(request, response);
    } else {
        HttpSession sesion2 = request.getSession(true);
        sesion2.setAttribute("sesionError", "Usuario o Contrasea Invlida");
        response.sendRedirect("index.jsp");

    }

    //System.out.println(r + "---");

}

From source file:abtlibrary.utils.as24ApiClient.ApiClient.java

License:Apache License

/**
 * Build a form-encoding request body with the given form parameters.
 *
 * @param formParams Form parameters in the form of Map
 * @return RequestBody//from   w  w  w.  ja  v  a2s. c  o  m
 */
public RequestBody buildRequestBodyFormEncoding(Map<String, Object> formParams) {
    FormEncodingBuilder formBuilder = new FormEncodingBuilder();
    for (Entry<String, Object> param : formParams.entrySet()) {
        formBuilder.add(param.getKey(), parameterToString(param.getValue()));
    }
    return formBuilder.build();
}

From source file:alfio.manager.system.MailgunMailer.java

License:Open Source License

private RequestBody prepareBody(Event event, String to, String subject, String text, Optional<String> html,
        Attachment... attachments) throws IOException {

    String from = event.getDisplayName() + " <" + configurationManager
            .getRequiredValue(Configuration.from(event.getOrganizationId(), event.getId(), MAILGUN_FROM)) + ">";

    if (ArrayUtils.isEmpty(attachments)) {
        FormEncodingBuilder builder = new FormEncodingBuilder().add("from", from).add("to", to)
                .add("subject", subject).add("text", text);

        String replyTo = configurationManager.getStringConfigValue(
                Configuration.from(event.getOrganizationId(), event.getId(), MAIL_REPLY_TO), "");
        if (StringUtils.isNotBlank(replyTo)) {
            builder.add("h:Reply-To", replyTo);
        }/*from w w w  .  j  a  va 2  s  . co m*/
        html.ifPresent((htmlContent) -> builder.add("html", htmlContent));
        return builder.build();

    } else {
        // https://github.com/square/okhttp/blob/parent-2.1.0/samples/guide/src/main/java/com/squareup/okhttp/recipes/PostMultipart.java
        MultipartBuilder multipartBuilder = new MultipartBuilder().type(MultipartBuilder.FORM);

        multipartBuilder.addFormDataPart("from", from).addFormDataPart("to", to)
                .addFormDataPart("subject", subject).addFormDataPart("text", text);

        html.ifPresent((htmlContent) -> multipartBuilder.addFormDataPart("html", htmlContent));

        for (Attachment attachment : attachments) {
            byte[] data = attachment.getSource();
            multipartBuilder.addFormDataPart("attachment", attachment.getFilename(), RequestBody
                    .create(MediaType.parse(attachment.getContentType()), Arrays.copyOf(data, data.length)));
        }
        return multipartBuilder.build();
    }
}

From source file:appewtc.masterung.testdrivinglicense.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/* w ww .j  a v  a  2  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:appewtc.masterung.testdrivinglicense.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();/* ww  w.  j av  a 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();
            }

        } // onResponse
    });

}

From source file:appewtc.masterung.testdrivinglicense.SignUpActivity.java

private void upLoadNewUser() {

    OkHttpClient okHttpClient = new OkHttpClient();
    RequestBody requestBody = new FormEncodingBuilder().add("isAdd", "true").add("Name", nameString)
            .add("Surname", surnameString).add("Age", ageString).add("User", userString)
            .add("Password", passwordString).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//w w w  .  j a  v a2s  . c  om
        public void onFailure(Request request, IOException e) {

        }

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

            finish();

        }
    });

}