I
I
Igor Emelyanov2016-02-11 04:07:28
Android
Igor Emelyanov, 2016-02-11 04:07:28

Rewrite request generation from apache.httpcomponents to retrofit2?

There is an old code that, using MultipartEntityBuilder in a POST request, put data, there is a need to do it too, only retrofit2

HttpPost post = new HttpPost(url);
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();

                    for (Map.Entry<String, Object> entry : params.entrySet()) {
                        if (entry.getValue() instanceof File) {
                            multipartEntityBuilder.addPart(entry.getKey(), new FileBody((File) entry.getValue()));
                        } else if (entry.getValue() instanceof String[]) {
                            for (String arrayString : (String[]) entry.getValue()) {
                                multipartEntityBuilder.addPart(entry.getKey() + "[]", new StringBody(arrayString, ContentType.APPLICATION_JSON));
                            }
                        } else if (entry.getValue() instanceof File[]) {
                            for (File file : (File[]) entry.getValue()) {
                                multipartEntityBuilder.addPart(entry.getKey() + "[]", new FileBody(file));
                            }
                        } else if (entry.getValue() instanceof String) {
                            multipartEntityBuilder.addTextBody(entry.getKey(), (String) entry.getValue(), ContentType.APPLICATION_JSON);
                        }
                    }
                    post.setEntity(multipartEntityBuilder.build());
                    response = httpClient.execute(post);

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question