0

I have to send an image to a server (I think the best option is using HttpURLConnection) and recieve from it a String answer. In the different docuemnts and web sites that I have read, I have investigated that the best way to do this is using MultipartEntity.

1_ Is it the best solution to do it?

2_ I had an error which says that Android Studio cannot resolve the symbol 'MultipartEntity'. I read that to solve it I have to download external libraries. Which are them and how can I download them?

3_ For this I want to run this process in backGround, but I have a mistake writing the AsyncTask like AsyncTask<String, Void, String> because I want to recieve the parameters like in the answer from this question (Sending files using POST with HttpURLConnection): String urlString, MultipartEntity reqEntity. How can I do to resolve it?

Community
  • 1
  • 1
F. Riggio
  • 51
  • 1
  • 2
  • 13
  • Use Gradle to get libraries. Okhttp can handle sending files. You're probably seeing Apache HTTP which doesn't exist in Android anymore (by default) – OneCricketeer Feb 12 '17 at 19:04
  • If you want to use that class, see here. http://stackoverflow.com/questions/28470486/android-multipartentity-and-dependencies – OneCricketeer Feb 12 '17 at 19:06
  • The problem is that I couldn't resolve it using the default libraries and I read here [http://stackoverflow.com/questions/21240376/multipartentity-cannot-be-resolved-to-a-type] that I can only solve it using external libraries. And there is a link which takes you to gitHub and appears a message number 404 – F. Riggio Feb 12 '17 at 19:07
  • All of them? Apache HTTP components & mime are what you're looking for, I assume – OneCricketeer Feb 12 '17 at 19:13
  • @cricket_007 that page says that the library's name is 'httpmime:4.4'. Does it mean that I can only use it from KitKat? Because I'm trying to do an application which you can run from GingerBread. And ignore my prrevious question. I deleted it – F. Riggio Feb 12 '17 at 19:17
  • 4.4 is the Apache library version. Unrelated to your Android version – OneCricketeer Feb 12 '17 at 19:29
  • Oh, perfect. I will try to do it – F. Riggio Feb 12 '17 at 19:30
  • @cricket_007 I did it and I have 2 dependencies: one is from apache and the other one is from lucee. I have to download only apache's one, don't I? – F. Riggio Feb 12 '17 at 19:43
  • Lucene? That shouldn't be needed – OneCricketeer Feb 12 '17 at 19:44
  • nono. It's lucee. If you want to watch it, I can edit the publication sending a screenshot. But for everybody says I think that the once that I am looking for is Apache haha – F. Riggio Feb 12 '17 at 19:50
  • I don't know what you're trying to download yourself. You just edit the gradle file, that's it – OneCricketeer Feb 12 '17 at 19:52
  • 1
    I downloaded the apache dependency and it worked: I write `MultipartEntityBuilder req;` and it says it is okay – F. Riggio Feb 12 '17 at 20:02
  • @cricket_007 He's trying to download the apache http libraries. They support multipart forms, whereas HttpUrlConnection, while a better library, do not. If you need to do multipart, you need to program it in yourself or use the old DefaultHttpClient, which requires grabbing it as a dependency. Practically speaking its easier to use Apache than do it yourself. – Gabe Sechan Feb 13 '17 at 05:44
  • @GabeSechan I am aware... Did you think I was steering him wrong? I said to use Gradle to download, not a JAR file. That's what I meant. – OneCricketeer Feb 13 '17 at 05:53

1 Answers1

0

I think the best option is using HttpURLConnection

Is it the best solution to do it?

Probably not. It's too low-level for multi-part requests.

I had an error which says that Android Studio cannot resolve the symbol 'MultipartEntity'. I read that to solve it I have to download external libraries. Which are them and how can I download them?

Options include, but not limited to

You can get either using Gradle. For Apache see: Android - MultipartEntity and dependencies, and Okhttp just read their documentation where it says "Gradle"

For this I want to run this process in Background

Can't remember about Apache, but OkHttp already can handle that without an AsyncTask.

Community
  • 1
  • 1
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245