HTTP Request class for Android

Here is a class that can be used to make HTTP get and HTTP post requests. I haven’t commented everything but the usage should be pretty clear from the method signatures.

Note: This is for Android 1.5.

You will also need this class along with it.

  1. import java.util.Hashtable;
  2.  
  3. public class HttpData {
  4.     public String content;
  5.     public Hashtable<String, String> cookies = new Hashtable<String, String>();
  6.     public Hashtable<String, String> headers = new Hashtable<String, String>();
  7. }

Published by 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.

Join the Conversation

5 Comments

  1. Hi Gez,

    Any Android application that needs to communicate with a website or a web application can use it. I wrote the class so I can use it to log users into their accounts and get some information from there.

    Regarding usage examples, you can use it like this:

    HttpData result;
    String url = “http://moazzam-khan.com”;
    result = HttpRequest.get();
    Log.v(“TAG”, result.content) ;

    /*
    The line above will print the HTML of the home page of this site.
    */
    String data = “user=someUser&pass=whatever”;
    result = HttpRequest.post(url, data);
    /*
    The line above is like submitting an HTML form
    result.content will contain the body of server’s response
    result.cookies will contain cookies that the server wants you to set.
    resultant.headers will contain all headers sent (including cookies)
    */

    I modified the class so it can upload files too. I will try to post it in a few days with some more comments in the code 🙂

    Hope that helps

  2. Thanks for the post.

    When do you think you will able to blog about uploading files?

    Also How would the server side code look if you were using spring mvc

    -AP

Leave a comment

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