Example usage for com.squareup.okhttp MultipartBuilder MultipartBuilder

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

Introduction

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

Prototype

public MultipartBuilder() 

Source Link

Document

Creates a new multipart builder that uses a random boundary token.

Usage

From source file:org.tinymediamanager.ui.dialogs.FeedbackDialog.java

License:Apache License

/**
 * Instantiates a new feedback dialog.//from  www  . j av  a2s  .co m
 */
public FeedbackDialog() {
    super(BUNDLE.getString("Feedback"), "feedback"); //$NON-NLS-1$
    setBounds(100, 100, 450, 320);

    getContentPane().setLayout(new FormLayout(
            new ColumnSpec[] { FormFactory.RELATED_GAP_COLSPEC, ColumnSpec.decode("max(400px;min):grow"),
                    FormFactory.RELATED_GAP_COLSPEC, },
            new RowSpec[] { FormFactory.RELATED_GAP_ROWSPEC, RowSpec.decode("fill:max(250px;min):grow"),
                    FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC,
                    FormFactory.RELATED_GAP_ROWSPEC, }));

    JPanel panelContent = new JPanel();
    getContentPane().add(panelContent, "2, 2, fill, fill");
    panelContent.setLayout(new FormLayout(
            new ColumnSpec[] { FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC,
                    FormFactory.RELATED_GAP_COLSPEC, ColumnSpec.decode("default:grow"),
                    FormFactory.RELATED_GAP_COLSPEC, },
            new RowSpec[] { FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC,
                    FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC,
                    FormFactory.PARAGRAPH_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC,
                    FormFactory.NARROW_LINE_GAP_ROWSPEC, RowSpec.decode("default:grow"), }));

    JLabel lblName = new JLabel(BUNDLE.getString("Feedback.name")); //$NON-NLS-1$
    panelContent.add(lblName, "2, 2, right, default");

    tfName = new JTextField();
    panelContent.add(tfName, "4, 2, fill, default");
    tfName.setColumns(10);

    JLabel lblEmailoptional = new JLabel(BUNDLE.getString("Feedback.email")); //$NON-NLS-1$
    panelContent.add(lblEmailoptional, "2, 4, right, default");

    tfEmail = new JTextField();
    panelContent.add(tfEmail, "4, 4, fill, default");
    tfEmail.setColumns(10);

    // pre-fill dialog
    if (Globals.isDonator()) {
        Properties p = License.decrypt();
        tfEmail.setText(p.getProperty("email"));
        tfName.setText(p.getProperty("user"));
    }

    JLabel lblFeedback = new JLabel(BUNDLE.getString("Feedback.message")); //$NON-NLS-1$
    panelContent.add(lblFeedback, "2, 6, 3, 1");

    JScrollPane scrollPane = new JScrollPane();
    panelContent.add(scrollPane, "2, 8, 3, 1, fill, fill");

    textArea = new JTextArea();
    scrollPane.setViewportView(textArea);
    textArea.setLineWrap(true);
    textArea.setWrapStyleWord(true);

    JPanel panelButtons = new JPanel();
    panelButtons.setLayout(new EqualsLayout(5));
    getContentPane().add(panelButtons, "2, 4, fill, fill");

    JButton btnSend = new JButton(BUNDLE.getString("Feedback")); //$NON-NLS-1$
    btnSend.setIcon(IconManager.APPLY);
    btnSend.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            // check if feedback is provided
            if (StringUtils.isEmpty(textArea.getText())) {
                JOptionPane.showMessageDialog(null, BUNDLE.getString("Feedback.message.empty")); //$NON-NLS-1$
                return;
            }

            // send feedback
            OkHttpClient client = TmmHttpClient.getHttpClient();
            String url = "https://script.google.com/macros/s/AKfycbxTIhI58gwy0UJ0Z1CdmZDdHlwBDU_vugBmQxcKN9aug4nfgrgZ/exec";

            try {
                StringBuilder message = new StringBuilder("Feedback from ");
                message.append(tfName.getText());
                message.append("\nEmail:");
                message.append(tfEmail.getText());
                message.append("\n");
                message.append("\nis Donator?: ");
                message.append(Globals.isDonator());
                message.append("\nVersion: ");
                message.append(ReleaseInfo.getRealVersion());
                message.append("\nBuild: ");
                message.append(ReleaseInfo.getRealBuildDate());
                message.append("\nOS: ");
                message.append(System.getProperty("os.name"));
                message.append(" ");
                message.append(System.getProperty("os.version"));
                message.append("\nJDK: ");
                message.append(System.getProperty("java.version"));
                message.append(" ");
                message.append(System.getProperty("java.vendor"));
                message.append("\nUUID: ");
                message.append(System.getProperty("tmm.uuid"));
                message.append("\n\n");
                message.append(textArea.getText());

                MultipartBuilder multipartBuilder = new MultipartBuilder();
                multipartBuilder.type(MultipartBuilder.FORM);

                multipartBuilder.addPart(Headers.of("Content-Disposition", "form-data; name=\"message\""),
                        RequestBody.create(null, message.toString()));
                multipartBuilder.addPart(Headers.of("Content-Disposition", "form-data; name=\"sender\""),
                        RequestBody.create(null, tfEmail.getText()));

                Request request = new Request.Builder().url(url).post(multipartBuilder.build()).build();
                client.newCall(request).execute();
            } catch (IOException e) {
                LOGGER.error("failed sending feedback: " + e.getMessage());
                JOptionPane.showMessageDialog(null,
                        BUNDLE.getString("Feedback.send.error") + "\n" + e.getMessage()); //$NON-NLS-1$
                return;
            }

            JOptionPane.showMessageDialog(null, BUNDLE.getString("Feedback.send.ok")); //$NON-NLS-1$
            setVisible(false);
        }
    });
    panelButtons.add(btnSend);

    JButton btnCacnel = new JButton(BUNDLE.getString("Button.cancel")); //$NON-NLS-1$
    btnCacnel.setIcon(IconManager.CANCEL);
    btnCacnel.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            setVisible(false);
        }
    });
    panelButtons.add(btnCacnel);
}

From source file:studio.imedia.vehicleinspection.activity.PersonalInfoActivity.java

/**
 * ?//  w w  w. j a v  a2  s.  co m
 *
 * @param urlSB
 */
private void uploadAvatar(StringBuffer urlSB, File avatarFile) {
    String url = urlSB.toString();
    String filename = avatarFile.getName();
    File file = avatarFile;
    String avatarPath = null;

    // TODO 
    //        RequestBody fileBody = RequestBody.create(MediaType.parse("application/octet-stream"), file);
    RequestBody fileBody = RequestBody.create(Constant.Media.TYPE_JPEG, file);

    RequestBody requestBody = new MultipartBuilder().type(MultipartBuilder.FORM)
            .addPart(Headers.of("Content-Disposition", "form-data; name=\"uploadFile\""), fileBody).build();

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

    mClient.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Request request, IOException e) {
            mHandler.sendEmptyMessage(CONNECT_FAIL);
        }

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

            String jsonStr = response.body().string();
            Log.d("files", jsonStr);
            try {
                int status = new JSONObject(jsonStr).getInt("status");
                Log.d("files", "the status is " + status);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    });

    // TODO ??
    //        Message msg = new Message();
    //        msg.what = MSG_UPLOAD_AVATAR_SUCCESS;
    //        msg.obj = avatarPath;
    //        mHandler.sendMessage(msg);
}