Android HttpRequest class (version 2.1)

Version of of this class had some bugs so I corrected them and here is the latest version 🙂

Enjoy!

You will also need the class below:

Author: Moazzam

Moazzam is a software developer working in the big apple. He has over a decade of experience in development. While most of his work involves developing web applications, he does development for Android and Windows Mobile in his spare time.

42 thoughts on “Android HttpRequest class (version 2.1)”

  1. Hi I didn’t find any HttpData Class in the documentation. Do you have a separate class for that?

  2. Sorry about that. I had put the HttpData class in the original version of the code. I added it here, too (in the post)

    Let me know in case you face any other problems with it.

  3. I’m trying to upload an image to a form using this script, and if I attach the image as a file and then try a POST the entire request is empty. Even if I have a Hashmap with a name value pair also attached, attaching an image blanks the whole request.

    Any idea why that might be?

  4. I got it working! Thanks! It was failing I think because I didn’t correctly resolve the issues with the missing backslashes initially.

    Thanks again for this code!

  5. Someone stole your code: sanderborgman.nl/android/android-httprequest-class/

    Anyway, thanks for the code ! Works like a charm.

  6. Missing backslashes:

    String newLine = “rn”;
    ——————————

    should be

    String newLine = “\r\n”;

  7. When multiple form-variable and/or file data submitted, it will be failed due to wrong seperation of “Content-Disposition” block. For perfection, the part

    dos.writeBytes(“–” + boundary + “–” + newLine);

    line in form-data generation block, should be removed.
    BTW, your code is very helpful. Thanks!

  8. Hey great work!
    Just one thing: (in addition to the above mentioned \r\n problem): I found more missing backslashes:

    dos.writeBytes(“Content-Disposition: form-data; ”
    + “name=\”file_”+i+”\”;filename=\””

    dos.writeBytes(“Content-Disposition: form-data;name=\””
    + key+”\”” + newLine + newLine + val);

    I think it’s the page’s ‘code-viewer’ or our browsers that ignores them, so maybe a link a zipped package would work?

    Anyway, thanks to Eclipse I quickly found and corrected the errors, wasn’t that hard. The incorrect \r\n characters I *did* miss at first so thanks tony 😀

  9. HI Moazzam,

    Awesome work!

    Could please explain me file up loader. I’m not getting the code. How you have establish the url to take the file upload service. Do you have any public url by which i can test this code?

  10. Hi David,

    You need two things to upload a file from Android to a web server.
    1 – A script on your server which can handle file uploads
    2 – This Android library in my blog post.

    Every language has its own way of handling file uploads so I would suggest googling for handle file uploads

    HTH

  11. Moazzam,

    Thanks for your reply! Do you have any public server available to test this code? I didn’t found any server otherwise i have to host the new server.

  12. Hi David,

    Unfortunately, I don’t provide free webhosting. I do, however, provide, paid webhosting if you are interested. Let me know if you are interested and I will provide more details

  13. can anyone tell me…, what does ht.put(“var1″,”val1”); mean?
    especially the var1 & val1…
    are they the parameters that we use to call in php file?

    thanks

  14. I have no idea…, where do you plan to send the file?
    do you want to do write bytes to mysql table?
    or just send it to server folder…?
    cause i’ve click for 100 times and it does nothing but
    debug perspective….

  15. excuse me Moazzam, can I get the php code?
    it’s to store to BLOB table…
    cause it’s always 0Bytes in my table
    thank you

  16. now, it’s 20 Bytes…and when I export it to .xls file it contains string of file path
    like “/sdcard/himalaya.jpg” and still not the file…
    how?

  17. thanks Moazzam! your code works!
    but still 0 Bytes in my BLOB…
    I know it works because when I open the task manager…
    it shows amounts of Bytes that have been sent through the internet
    and those bytes (when I calculate) have total size
    roughly same with the image size in my emulator sdcard
    same story happened when I change the file with different size…

    I guess I have problems with my php code…

    last thing i want to know is “do you send the image file in binary representation or else?” cause it will really helps me to make suitable php file to receive the data….
    then, if you send it in binary do i need to convert it to hexadecimal before insert it to my BLOB table?

    really thanks!!! I’m attending an android competition now…
    that’s why i’m really frustrated!!!

  18. Hi Moazzam,

    my project is about an android shop app. it works just like ebay…
    with shop maps and nearest route to the shop…
    the user can upload their goods images, their place address, and price…
    if someone wants to seek for a product or particular thing…
    it will display the categories that related to their search

    i’ve done in signup and signin user by store their account details on mysql server.

    anyway, i’m already lost…
    not because the code…, the proposal is just not so good…
    (what a silly competition)

    o yea…, i’m planning to make the vid tutorial about this code…(if it works to me one day) on youtube…
    would you permit it, Moazzam?

    btw thnx Moazzam…! I hope U will make more android tutorials!
    have a nice day!!!

  19. Hi imsuperdumm,

    That sounds like an interesting project. Best of luck with it. A video tutorial sounds good. Please let me know when you make it. I will put it up on my blog, too.

    And, thanks for the encouragement on making tutorials 😀

    Regards,
    Moazzam

  20. moazzam i am using your code and it works fine if image size is upto 2 mb or in some cases upto 4-5 mbs
    then it take so much time to upload image

    any idea to decrease size or make image upload faster?
    or any other suggestion?

    waiting for your reply.
    Umar

  21. Hi Umar,

    I haven’t played around with the image manipulation library in Android but you should be able to find something to compress the image – maybe convert the image to a jpeg?

  22. can you please guide how to use progress bar to show upload progress in this tutorial?

    and which is suitable way to upload files? background service or normal thread?

  23. Im wondering why you have

    Log.e(“ERROR”, line);

    in public static HttpData post(String sUrl, String data) {

    it always throws an error for me…

  24. Hi moazzam,

    I’m new to android as well as java. I’m using Eclipse. I tried your code and added few lines so I can display it. However, the only string that is being shown is hello android and not the data.content. What is wrong with my code?

    The full code
    http://www.pastie.org/1641261

    The code I added to call your function

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    /* pull a webpage
    *
    */
    HttpData data = HttpRequest.get(“http://www.pgts.com.au/download/txt/wav.txt”);
    System.out.println(data.content); /* <—- this is not displayed on my phone */

    /* added the lines below to test if this procedure is being called and yes it does because
    hello android is displayed. Not sure why "data.content" is not being displayed
    */
    TextView tv = new TextView(this);
    tv.setText("hello android"); /* <—- I can see this in my phone */
    setContentView(tv);
    }

  25. i want to know what this code exactly do after submittng the form… like if i submit the form, what type of data it takes from the server… can u explain me with a example…. i m a newbie

  26. i am new to android,i am unable to unstand the flow and it shows the errors on myeclipse,how to call
    HttpData and HttpRequest class in main activity

  27. Hi Kumar,

    You have to save both classes (if you don’t know where then put them in the same folder as your main activity) and type this at the top of you main activity file:

    Wherever you want to make an HTTP request, just follow the usage examples I have in the comments of the HttpRequest class.

  28. How should we run this code so that it doesn’t block UI thread? Do we start it inside a normal thread or should we do it in Asynctask etc? What about orientation changes? How are you yourself running this?

  29. Hi Moazzam,

    Thanks for putting this useful code. A few things though:

    1) Due to your code block display above, triple double quotes are bot escaped correctly. If you copy from here to Eclipse, you will see those errors. E.g.: #214, there are a few more like this.
    2) Also, there are many warnings in Eclipse.

    Thanks

  30. Hi Moazzam,

    Can you tell me how can this code help in image uploading on android with the proper UI attached.

  31. The library doesn’t care about the UI. You just have to provide it the parameters it needs and it will do what it is supposed to. The UI is something you will have to code 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *