1

Hy guys!

I am working on an android project(java) with another guy working on the server-side(php). In my application I need to call POST and GET methods in order to upload files to server, download files, send Strings, byte[] array etc.

My question is: What is the best library to use in my case?(I think my files will not exceed 3mb)

I am new in android so I tried so far:

1.Android Asynchronous Http Client(com.loopj.android:android-async-http:x.x.x) -we gave up to this because it is not from a "trusted" source

2.AsyncTask+HttpClient+HttpPost -we gave up to this too

3.Volley library -best so far(for strings, image request), but it needs additional libraries to send images to server(org.apache.httpcomponents:httpmime:4.5) -I followed so examples from here but I got exceptions, error, libraries error(duplicates) and never managed to solve one without other showing up. -so I gave up on this too

My question posted for volley library here

4. Now I am thinking about using Retrofit, but dont know it fits my needs: -send strings and all types of primitive data -send image/images to server(together with an Api key) -download image/images from server

Tell me if I am wrong somewhere or if I missed something working with the libraries specified above. I managed to send simple data with all of these, but I didnt managed to send Files(excepting loopj library).

Do you think should I go back to Volley, or starting reading about Retrofit? Volley seems to be the most flexible one, but not for uploading files.

Any reference or advice is welcome! Thanks in advance!

Update: I found a possible solution for my problem: -I convert my file/image to a byte array and encode it to a base64 string -I send the string to server as basic StringRequest with HashMap<String,String>(Using Volley library from Google developers) -The server decode the string a save the file

Community
  • 1
  • 1
Chris
  • 6,105
  • 6
  • 38
  • 55
  • Kindly upload your piece of code that u mentioned in update section Tanx in advance – Adiii Jun 19 '16 at 00:53
  • @Adil You can check this question and follow the answer to encode and decode in Base64 http://stackoverflow.com/questions/7360403/base-64-encode-and-decode-example-code and this to send data with Volley http://stackoverflow.com/questions/36194517/android-send-image-encoded-to-base64-using-volley – Chris Jun 19 '16 at 11:11
  • Tanx dear @Cristian.R – Adiii Jun 19 '16 at 15:53

2 Answers2

2

I think a very good fit for you would be AndroidAsync. You can find more about it on their GitHub repository here: https://github.com/koush/AndroidAsync

As an example for you on how to upload files to server:

AsyncHttpPost post = new AsyncHttpPost("http://myservercom/postform.html");
MultipartFormDataBody body = new MultipartFormDataBody();
body.addFilePart("my-file", new File("/path/to/file.txt");
body.addStringPart("foo", "bar");
post.setBody(body);
AsyncHttpClient.getDefaultInstance().execute(post, new StringCallback() {
    @Override
    public void onCompleted(Exception e, AsyncHttpResponse source, String result) {
        if (e != null) {
            ex.printStackTrace();
            return;
        }
        System.out.println("Server says: " + result);
    }
});

There is also NanoHTTPD which you can find here: https://github.com/NanoHttpd/nanohttpd

I hope this will help you.

Andrei
  • 51
  • 4
  • Thanks a lot for your answer, I didnt knew about those but I can't accept your answer as most helpful because those are not from a trusted source too(loops metioned in question is better than both). My project manager would never lets me to use something like that. I updated my question if you wanna see a posible/temporary solution. – Chris Aug 01 '15 at 09:09
0

You should try HttpURLConnection its really easy to send data to a server.

https://developer.android.com/training/basics/network-ops/connecting.html

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Tiago Couto
  • 351
  • 3
  • 9
  • 1
    Instead of *you should try*, why don't you add a small example and provide that link for reference. That'd be really helpful! – Bhargav Rao Sep 17 '16 at 19:00