Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5991753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:25:58+00:00 2026-05-22T23:25:58+00:00

I’m trying to figure out how to POST JSON from Android by using HTTPClient.

  • 0

I’m trying to figure out how to POST JSON from Android by using HTTPClient. I’ve been trying to figure this out for a while, I have found plenty of examples online, but I cannot get any of them to work. I believe this is because of my lack of JSON/networking knowledge in general. I know there are plenty of examples out there but could someone point me to an actual tutorial? I’m looking for a step by step process with code and explanation of why you do each step, or of what that step does. It doesn’t need to be a complicated, simple will suffice.

Again, I know there are a ton of examples out there, I’m just really looking for an example with an explanation of what exactly is happening and why it is doing that way.

If someone knows about a good Android book on this, then please let me know.

Thanks again for the help @terrance, here is the code I described below

public void shNameVerParams() throws Exception{
     String path = //removed
     HashMap  params = new HashMap();

     params.put(new String("Name"), "Value"); 
     params.put(new String("Name"), "Value");

     try {
        HttpClient.SendHttpPost(path, params);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T23:25:59+00:00Added an answer on May 22, 2026 at 11:25 pm

    In this answer I am using an example posted by Justin Grammens.

    About JSON

    JSON stands for JavaScript Object Notation. In JavaScript properties can be referenced both like this object1.name and like this object['name'];. The example from the article uses this bit of JSON.

    The Parts
    A fan object with email as a key and foo@bar.com as a value

    {
      fan:
        {
          email : 'foo@bar.com'
        }
    }
    

    So the object equivalent would be fan.email; or fan['email'];. Both would have the same value
    of 'foo@bar.com'.

    About HttpClient Request

    The following is what our author used to make a HttpClient Request. I do not claim to be an expert at all this so if anyone has a better way to word some of the terminology feel free.

    public static HttpResponse makeRequest(String path, Map params) throws Exception 
    {
        //instantiates httpclient to make request
        DefaultHttpClient httpclient = new DefaultHttpClient();
    
        //url with the post data
        HttpPost httpost = new HttpPost(path);
    
        //convert parameters into JSON object
        JSONObject holder = getJsonObjectFromMap(params);
    
        //passes the results to a string builder/entity
        StringEntity se = new StringEntity(holder.toString());
    
        //sets the post request as the resulting string
        httpost.setEntity(se);
        //sets a request header so the page receving the request
        //will know what to do with it
        httpost.setHeader("Accept", "application/json");
        httpost.setHeader("Content-type", "application/json");
    
        //Handles what is returned from the page 
        ResponseHandler responseHandler = new BasicResponseHandler();
        return httpclient.execute(httpost, responseHandler);
    }
    

    Map

    If you are not familiar with the Map data structure please take a look at the Java Map reference. In short, a map is similar to a dictionary or a hash.

    private static JSONObject getJsonObjectFromMap(Map params) throws JSONException {
    
        //all the passed parameters from the post request
        //iterator used to loop through all the parameters
        //passed in the post request
        Iterator iter = params.entrySet().iterator();
    
        //Stores JSON
        JSONObject holder = new JSONObject();
    
        //using the earlier example your first entry would get email
        //and the inner while would get the value which would be 'foo@bar.com' 
        //{ fan: { email : 'foo@bar.com' } }
    
        //While there is another entry
        while (iter.hasNext()) 
        {
            //gets an entry in the params
            Map.Entry pairs = (Map.Entry)iter.next();
    
            //creates a key for Map
            String key = (String)pairs.getKey();
    
            //Create a new map
            Map m = (Map)pairs.getValue();   
    
            //object for storing Json
            JSONObject data = new JSONObject();
    
            //gets the value
            Iterator iter2 = m.entrySet().iterator();
            while (iter2.hasNext()) 
            {
                Map.Entry pairs2 = (Map.Entry)iter2.next();
                data.put((String)pairs2.getKey(), (String)pairs2.getValue());
            }
    
            //puts email and 'foo@bar.com'  together in map
            holder.put(key, data);
        }
        return holder;
    }
    

    Please feel free to comment on any questions that arise about this post or if I have not made something clear or if I have not touched on something that your still confused about… etc whatever pops in your head really.

    (I will take down if Justin Grammens does not approve. But if not then thanks Justin for being cool about it.)

    Update

    I just happend to get a comment about how to use the code and realized that there was a mistake in the return type.
    The method signature was set to return a string but in this case it wasnt returning anything. I changed the signature
    to HttpResponse and will refer you to this link on Getting Response Body of HttpResponse
    the path variable is the url and I updated to fix a mistake in the code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.