0

I used this answer to upload a multipart file and my gradle http dependencies is like this:

 compile('org.apache.httpcomponents:httpmime:4.3.6') {
    exclude module: 'httpclient'
}
compile 'org.apache.httpcomponents:httpclient-android:4.3.5'
compile 'org.apache.httpcomponents:httpcore:4.4.5'

and as the answer said, Java code is like this:

private void upload() {
    try {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPostHC4 uploadFile = new HttpPostHC4("...");
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addTextBody("field1", "yes", ContentType.TEXT_PLAIN);

        // This attaches the file to the POST:
        File f = new File("[/path/to/upload]");
        builder.addBinaryBody(
                "file",
                new FileInputStream(f),
                ContentType.APPLICATION_OCTET_STREAM,
                f.getName()
        );

        HttpEntity multipart = builder.build();
        uploadFile.setEntity(multipart);
        CloseableHttpResponse response = httpClient.execute(uploadFile); //Error
        HttpEntity responseEntity = response.getEntity();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

this problem is 'httpClient.execute(uploadFile);' does not accept uploadFile as parameter! How can I fix it?

Community
  • 1
  • 1
Alireza Akbari
  • 2,153
  • 2
  • 28
  • 53
  • Why would that be a problem? That statement does not make sense to begin with. – greenapps Oct 01 '16 at 08:30
  • `to upload a multipart file ` ?? What is a multipart file? – greenapps Oct 01 '16 at 08:31
  • @greenapps, Android Studio does not allow me to pass `uploadFile` as input! It may caused by version of my httpComponents version. – Alireza Akbari Oct 01 '16 at 08:37
  • `Android Studio does not allow me to pass uploadFile as input` Of course not. I knew that already and told you that that statement does not make sense. Try to understand why. I really cannot imagine that you still hang on such a wrong statement. – greenapps Oct 01 '16 at 12:46
  • @greenapps So why answer was accepted and other tuts use this like that? why mine does not work? why the method gets other inputs for me?? in such condition how can I pass a correct input to the method?? – Alireza Akbari Oct 02 '16 at 04:29
  • Sorry. I understood it wrong. I read `httpClient.execute(upload());'. You confused me by calling a HttpPostHC4 instance uploadFile. How could you. I do not know HC4 so better not comment any more. Why dont you use HttpPost? – greenapps Oct 02 '16 at 06:43
  • @greenapps because in libs I mentioned there is no HttpPost :)) so how can I fix it? if there is problem in my gradle libs, can you please give me the correct ones? – Alireza Akbari Oct 02 '16 at 10:00

0 Answers0