What is the preferred method for uploading to a server from Android?












1















I've been trying to find a way to upload a video from an Android device to an API, but I haven't found a good way to do it. It seems most of the information I've found online is fairly out of date (a lot of it being from last year). Most of them are using a method like this: http://getablogger.blogspot.com/2008/01/android-how-to-post-file-to-php-server.html



What's the easiest/preferred way to upload something to an API with a multipart POST?










share|improve this question























  • If my answer was helpful, I'd appreciate it if you accepted it. Otherwise, please let me know how I can be of more help.

    – Konklone
    Jan 4 '10 at 18:10
















1















I've been trying to find a way to upload a video from an Android device to an API, but I haven't found a good way to do it. It seems most of the information I've found online is fairly out of date (a lot of it being from last year). Most of them are using a method like this: http://getablogger.blogspot.com/2008/01/android-how-to-post-file-to-php-server.html



What's the easiest/preferred way to upload something to an API with a multipart POST?










share|improve this question























  • If my answer was helpful, I'd appreciate it if you accepted it. Otherwise, please let me know how I can be of more help.

    – Konklone
    Jan 4 '10 at 18:10














1












1








1


1






I've been trying to find a way to upload a video from an Android device to an API, but I haven't found a good way to do it. It seems most of the information I've found online is fairly out of date (a lot of it being from last year). Most of them are using a method like this: http://getablogger.blogspot.com/2008/01/android-how-to-post-file-to-php-server.html



What's the easiest/preferred way to upload something to an API with a multipart POST?










share|improve this question














I've been trying to find a way to upload a video from an Android device to an API, but I haven't found a good way to do it. It seems most of the information I've found online is fairly out of date (a lot of it being from last year). Most of them are using a method like this: http://getablogger.blogspot.com/2008/01/android-how-to-post-file-to-php-server.html



What's the easiest/preferred way to upload something to an API with a multipart POST?







java android http






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 25 '09 at 4:53









Kyle SlatteryKyle Slattery

17.7k82635




17.7k82635













  • If my answer was helpful, I'd appreciate it if you accepted it. Otherwise, please let me know how I can be of more help.

    – Konklone
    Jan 4 '10 at 18:10



















  • If my answer was helpful, I'd appreciate it if you accepted it. Otherwise, please let me know how I can be of more help.

    – Konklone
    Jan 4 '10 at 18:10

















If my answer was helpful, I'd appreciate it if you accepted it. Otherwise, please let me know how I can be of more help.

– Konklone
Jan 4 '10 at 18:10





If my answer was helpful, I'd appreciate it if you accepted it. Otherwise, please let me know how I can be of more help.

– Konklone
Jan 4 '10 at 18:10












2 Answers
2






active

oldest

votes


















0














I have an Android app I'm developing against the Campfire chat service's "API". The code here uploads a file through multipart POST:



http://github.com/klondike/android-campfire/blob/master/src/com/github/klondike/java/campfire/Room.java#L175



Everything after the "dos.close()" line is related to checking the response to detect whether the post was successful.



Not everything in there is necessary for every multi-part post; for example, the X-Requested-With header is specific to Campfire, the User-Agent is optional, and the Cookie is because I have to stay logged in. Also, the "OH MY GOD" comment about spacing is probably Campfire-specific.



I've heard that the latest version of the HttpClient library from Apache has more convenient built-in multi-part support, but the last sync Google performed against it to Android didn't include those features, so here I am doing it manually.



Hope that's of some help.






share|improve this answer































    0














    You could use the HttpClient from the Apache Software Foundation. It is part of the Android API:



    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("www.somewebpage.com/site-that-can-handle-post");

    try {
    MultipartEntity entity = new MultipartEntity();
    entity.addPart("timestamp", new StringBody("1311789946"));
    entity.addPart("image", new FileBody(new File("/foo/bar/video.mpeg")));
    httppost.setEntity(entity);
    HttpResponse response = httpclient.execute(httppost);
    } catch (Exception e) {
    Log.v(MyActivity.TAG, "doh!", e);
    }


    Hope that helps. :)






    share|improve this answer


























    • Link redirected to other page -> HttpClient

      – Rumit Patel
      Nov 23 '18 at 10:05











    • Thanks, updated the link.

      – Håvard Geithus
      Nov 23 '18 at 12:22












    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f1794714%2fwhat-is-the-preferred-method-for-uploading-to-a-server-from-android%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I have an Android app I'm developing against the Campfire chat service's "API". The code here uploads a file through multipart POST:



    http://github.com/klondike/android-campfire/blob/master/src/com/github/klondike/java/campfire/Room.java#L175



    Everything after the "dos.close()" line is related to checking the response to detect whether the post was successful.



    Not everything in there is necessary for every multi-part post; for example, the X-Requested-With header is specific to Campfire, the User-Agent is optional, and the Cookie is because I have to stay logged in. Also, the "OH MY GOD" comment about spacing is probably Campfire-specific.



    I've heard that the latest version of the HttpClient library from Apache has more convenient built-in multi-part support, but the last sync Google performed against it to Android didn't include those features, so here I am doing it manually.



    Hope that's of some help.






    share|improve this answer




























      0














      I have an Android app I'm developing against the Campfire chat service's "API". The code here uploads a file through multipart POST:



      http://github.com/klondike/android-campfire/blob/master/src/com/github/klondike/java/campfire/Room.java#L175



      Everything after the "dos.close()" line is related to checking the response to detect whether the post was successful.



      Not everything in there is necessary for every multi-part post; for example, the X-Requested-With header is specific to Campfire, the User-Agent is optional, and the Cookie is because I have to stay logged in. Also, the "OH MY GOD" comment about spacing is probably Campfire-specific.



      I've heard that the latest version of the HttpClient library from Apache has more convenient built-in multi-part support, but the last sync Google performed against it to Android didn't include those features, so here I am doing it manually.



      Hope that's of some help.






      share|improve this answer


























        0












        0








        0







        I have an Android app I'm developing against the Campfire chat service's "API". The code here uploads a file through multipart POST:



        http://github.com/klondike/android-campfire/blob/master/src/com/github/klondike/java/campfire/Room.java#L175



        Everything after the "dos.close()" line is related to checking the response to detect whether the post was successful.



        Not everything in there is necessary for every multi-part post; for example, the X-Requested-With header is specific to Campfire, the User-Agent is optional, and the Cookie is because I have to stay logged in. Also, the "OH MY GOD" comment about spacing is probably Campfire-specific.



        I've heard that the latest version of the HttpClient library from Apache has more convenient built-in multi-part support, but the last sync Google performed against it to Android didn't include those features, so here I am doing it manually.



        Hope that's of some help.






        share|improve this answer













        I have an Android app I'm developing against the Campfire chat service's "API". The code here uploads a file through multipart POST:



        http://github.com/klondike/android-campfire/blob/master/src/com/github/klondike/java/campfire/Room.java#L175



        Everything after the "dos.close()" line is related to checking the response to detect whether the post was successful.



        Not everything in there is necessary for every multi-part post; for example, the X-Requested-With header is specific to Campfire, the User-Agent is optional, and the Cookie is because I have to stay logged in. Also, the "OH MY GOD" comment about spacing is probably Campfire-specific.



        I've heard that the latest version of the HttpClient library from Apache has more convenient built-in multi-part support, but the last sync Google performed against it to Android didn't include those features, so here I am doing it manually.



        Hope that's of some help.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 25 '09 at 5:55









        KonkloneKonklone

        2,8001926




        2,8001926

























            0














            You could use the HttpClient from the Apache Software Foundation. It is part of the Android API:



            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("www.somewebpage.com/site-that-can-handle-post");

            try {
            MultipartEntity entity = new MultipartEntity();
            entity.addPart("timestamp", new StringBody("1311789946"));
            entity.addPart("image", new FileBody(new File("/foo/bar/video.mpeg")));
            httppost.setEntity(entity);
            HttpResponse response = httpclient.execute(httppost);
            } catch (Exception e) {
            Log.v(MyActivity.TAG, "doh!", e);
            }


            Hope that helps. :)






            share|improve this answer


























            • Link redirected to other page -> HttpClient

              – Rumit Patel
              Nov 23 '18 at 10:05











            • Thanks, updated the link.

              – Håvard Geithus
              Nov 23 '18 at 12:22
















            0














            You could use the HttpClient from the Apache Software Foundation. It is part of the Android API:



            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("www.somewebpage.com/site-that-can-handle-post");

            try {
            MultipartEntity entity = new MultipartEntity();
            entity.addPart("timestamp", new StringBody("1311789946"));
            entity.addPart("image", new FileBody(new File("/foo/bar/video.mpeg")));
            httppost.setEntity(entity);
            HttpResponse response = httpclient.execute(httppost);
            } catch (Exception e) {
            Log.v(MyActivity.TAG, "doh!", e);
            }


            Hope that helps. :)






            share|improve this answer


























            • Link redirected to other page -> HttpClient

              – Rumit Patel
              Nov 23 '18 at 10:05











            • Thanks, updated the link.

              – Håvard Geithus
              Nov 23 '18 at 12:22














            0












            0








            0







            You could use the HttpClient from the Apache Software Foundation. It is part of the Android API:



            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("www.somewebpage.com/site-that-can-handle-post");

            try {
            MultipartEntity entity = new MultipartEntity();
            entity.addPart("timestamp", new StringBody("1311789946"));
            entity.addPart("image", new FileBody(new File("/foo/bar/video.mpeg")));
            httppost.setEntity(entity);
            HttpResponse response = httpclient.execute(httppost);
            } catch (Exception e) {
            Log.v(MyActivity.TAG, "doh!", e);
            }


            Hope that helps. :)






            share|improve this answer















            You could use the HttpClient from the Apache Software Foundation. It is part of the Android API:



            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("www.somewebpage.com/site-that-can-handle-post");

            try {
            MultipartEntity entity = new MultipartEntity();
            entity.addPart("timestamp", new StringBody("1311789946"));
            entity.addPart("image", new FileBody(new File("/foo/bar/video.mpeg")));
            httppost.setEntity(entity);
            HttpResponse response = httpclient.execute(httppost);
            } catch (Exception e) {
            Log.v(MyActivity.TAG, "doh!", e);
            }


            Hope that helps. :)







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 23 '18 at 12:22

























            answered Jul 27 '11 at 17:13









            Håvard GeithusHåvard Geithus

            3,01452644




            3,01452644













            • Link redirected to other page -> HttpClient

              – Rumit Patel
              Nov 23 '18 at 10:05











            • Thanks, updated the link.

              – Håvard Geithus
              Nov 23 '18 at 12:22



















            • Link redirected to other page -> HttpClient

              – Rumit Patel
              Nov 23 '18 at 10:05











            • Thanks, updated the link.

              – Håvard Geithus
              Nov 23 '18 at 12:22

















            Link redirected to other page -> HttpClient

            – Rumit Patel
            Nov 23 '18 at 10:05





            Link redirected to other page -> HttpClient

            – Rumit Patel
            Nov 23 '18 at 10:05













            Thanks, updated the link.

            – Håvard Geithus
            Nov 23 '18 at 12:22





            Thanks, updated the link.

            – Håvard Geithus
            Nov 23 '18 at 12:22


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f1794714%2fwhat-is-the-preferred-method-for-uploading-to-a-server-from-android%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()