如何使用Android中的多部分实体将图像与数据一起发布

时间:2018-05-07 08:09:30

标签: java android-studio

我正在使用带有数据的MultipartEntity上传图片,但我无法发帖。我必须发布一些细节和图像。我不知道问题在哪里,并且在点击帖子时调试我的代码它不起作用。我不知道如何解决这个问题。

# Ruby
x = [1,2,3,4]
pivot = 2
puts x[0..(pivot-1)] # [1,2]
puts x[pivot..-1] # [3,4]

1 个答案:

答案 0 :(得分:1)

经过长时间的研究,我能够将带有数据的图像发布到服务器。我按照以下链接修改了我的要求。我认为这对任何人都有帮助。It is very useful to capture a picture and also i am able to post image with data by using this link

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(urlString);


        try {
            CustomMultiPartEntity entity=new CustomMultiPartEntity(new CustomMultiPartEntity.ProgressListener() {

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

            entity.addPart("FirstName", new StringBody(Person.getFirstName()));
            entity.addPart("LastName", new StringBody(Person.getLastName()));
            entity.addPart("Email", new StringBody(Person.getEmail()));
            entity.addPart("Password", new StringBody(Person.getPassword()));
            entity.addPart("Mobilenumber", new StringBody(Person.getMobilenumber()));
            entity.addPart("uploadedfile", new FileBody(sourceFile));
            totalSize = entity.getContentLength();
            httppost.setEntity(entity);
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity r_entity = response.getEntity();
            responseString = EntityUtils.toString(r_entity);

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