MultipartEntity:如何调整参数以上传到RoR

时间:2016-09-12 05:18:06

标签: android ruby-on-rails parameters multipartentity

Android工作室

Hello People,

我想更改从我的Android应用程序发布的参数,以便ruby on rails可以接受正确的表单。实际上我的Android应用程序发送此表单:

Parameters: {"picture"=>#<ActionDispatch::Http::UploadedFile:0x53c1b10 @tempfile=#<Tempfile:C:/Users/Clemens/AppData/Local/Temp/RackMultipart20160911-9268-qqwltt.jpg>, @original_
filename="IMG_20160911_122254.jpg", @content_type="application/octet-stream", @headers="Content-Disposition: form-data; name=\"document\"; filename=\"IMG_20160911_122254.jpg\"\r\nCo
ntent-Type: application/octet-stream\r\nContent-Transfer-Encoding: binary\r\n">}

但我只是想要一个小小的改变,把“用户”放在这之前{“user”=&gt; {“pictures”=&gt;

 Parameters: { user{"picture"=>#<ActionDispatch::Http::UploadedFile:0x9599268 @tempfile=#<Tempfile:C:/Users/Clemens/AppData/Local/Temp/RackMultipart20160911-22652-1gxvlth.jpg>, @original_filename="Koala.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"document[file]\"; filename=\"Koala.jpg\"\r\nContent-Type: image/jpeg\r\n">}, "commit"=>"Create Document"}

有人有想法吗?我真的很感谢你!这是我的代码:

@Override
        protected String doInBackground(Void... params) {
            return uploadFile();
        }

      //  @SuppressWarnings("deprecation")
        private String uploadFile() {
            String responseString = null;

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(Config.FILE_UPLOAD_URL);

            try {
                AndroidMultiPartEntity entity = new AndroidMultiPartEntity(
                        new ProgressListener() {

                            @Override
                            public void transferred(long num) {
                                publishProgress((int) ((num / (float) totalSize) * 100));
                            }
                        });

                File sourceFile = new File(filePath);

           entity.addPart("picture", new FileBody(sourceFile));

                // Extra parameters if you want to pass to server
              /////////  entity.addPart("website",
              ///////         new StringBody("www.androidhive.info"));
              ////////  entity.addPart("email", new StringBody("abc@gmail.com"));

                totalSize = entity.getContentLength();
                httppost.setEntity(entity);


                // Making server call
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity r_entity = response.getEntity();

                int statusCode = response.getStatusLine().getStatusCode();
                if (statusCode == 200 ) {
                    // Server response
                   // responseString = EntityUtils.toString(r_entity);
                    responseString = "Foto erfolgreich hochgeladen";

                } else {
                    responseString = "Error: Http Status Code: "
                            + statusCode;
                }

            } catch (ClientProtocolException e) {
                responseString = e.toString();
            } catch (IOException e) {
                responseString = e.toString();
            }

            return responseString;

        }

0 个答案:

没有答案