Asked  7 Months ago    Answers:  5   Viewed   66 times

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();
    }
 }

 Answers

55

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.

Tuesday, June 1, 2021
 
Zulakis
answered 7 Months ago
12

Just try with below code this will use to send simple post data using volley, just replace URL and parameter data.

StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            Toast.makeText(MainActivity.this,response,Toast.LENGTH_LONG).show();
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Toast.makeText(MainActivity.this,error.toString(),Toast.LENGTH_LONG).show();
                        }
                    }){
                @Override
                protected Map<String,String> getParams(){
                    Map<String,String> params = new HashMap<String, String>();
                    params.put(KEY_USERNAME,username);
                    params.put(KEY_PASSWORD,password);
                    params.put(KEY_EMAIL, email);
                    return params;
                }

            };

            getRequestOtpPage().addToRequestQueue(stringRequest);
Friday, August 6, 2021
 
phi
answered 4 Months ago
phi
88

The API format means the the Segments property must be a collection of otherType (your model only has a single object). In addition the Sources property is also a collection of string.

Change you model to

public class SearchForFlight
{
    public SearchForFlight()
    {
        Segments = new List<otherType>();
        Sources = new List<string>();
    }
    ....
    public string PreferedLines { get; set; }
    public List<otherType> Segments { get; set; }
    [JsonIgnore]
    public IEnumerable<SelectListItem> FlightCabinClassList { get; set; }
    [JsonIgnore]
    public IEnumerable<SelectListItem> JourneyList { get; set; }
    public List<string> Sources { get; set; }
}

and then assuming your only wanting to edit one Segments and one Sources, then in the GET method, add one object to each collection

SearchForFlight model = new SearchForFlight();
model.Segments.Add(new otherType());
model.Sources.Add(string.Empty);
....
return View(model);

and in the view, use for loops to generate the html for the collections

@for(int i = 0; i < Model.Segments.Count; i++)
{
    @Html.LabelFor(m => m.Segments[i].Origin)
    @Html.TextBoxFor(m => m.Segments[i].Origin)
    @Html.ValidationMesageFor(m => m.Segments[i].Origin)
    .... // other properties of otherType
}

Note that this will generate id attributes such as id="Segments_0__Origin" so your script would need to be

Segments:
{
    Origin: $("#Segments_0__Origin").val(),
    Destination: $("#Segments_0__Destination").val(),
    ....

however there is no need to generate you javascript object manually, and your ajax can be simply

$.ajax({
    ....
    data: $('form').serialize(),
    ....

and do not set the contentType option so it uses the default application/x-www-form-urlencoded; charset=UTF-8

Tuesday, August 31, 2021
 
Martijn Pieters
answered 4 Months ago
12

This is how I did it:

  WebClient wc = new WebClient();
        string baseSiteString = wc.DownloadString("https://auth-agdit.herokuapp.com");
        string csrfToken = Regex.Match(baseSiteString, "<meta name="csrf-token" content="(.*?)" />").Groups[1].Value;
        string cookie = wc.ResponseHeaders[HttpResponseHeader.SetCookie];
        Console.WriteLine("CSRF Token: {0}", csrfToken);
        Console.WriteLine("Cookie: {0}", cookie);

        wc.Headers.Add(HttpRequestHeader.Cookie, cookie);
        wc.Headers.Add(HttpRequestHeader.ContentType, "application/json; charset=utf-8");
        wc.Headers.Add(HttpRequestHeader.Accept, "application/json, text/javascript, */*; q=0.01");
        wc.Headers.Add("X-CSRF-Token", csrfToken);
        wc.Headers.Add("X-Requested-With", "XMLHttpRequest");


        string dataString = @"{""user"":{""email"":""email_here"",""password"":""password_here""}}";
     //   string dataString = @"{""user"":{""email"":"""+uEmail+@""",""password"":"""+uPassword+@"""}}";
        byte[] dataBytes = Encoding.UTF8.GetBytes(dataString);
        byte[] responseBytes = wc.UploadData(new Uri("https://auth-agdit.herokuapp.com/api/v1/sessions.json"), "POST", dataBytes);
        string responseString = Encoding.UTF8.GetString(responseBytes);
        Console.WriteLine(responseString);
Monday, October 25, 2021
 
Godfather
answered 2 Months ago
57

You have runOnUiThread which runs on ui thread and you have this

  JSONObject json = jsonParser.makeHttpRequest(
                        url_product_detials, "GET", params);

From you comments above the code getting product details by making HTTP request. So you are making http request on the ui thread. You will get NetworkOnMainThreadException.

public final void runOnUiThread (Runnable action)

Added in API level 1

Runs the specified action on the UI thread. If the current thread is the UI thread, then the action is executed immediately. If the current thread is not the UI thread, the action is posted to the event queue of the UI thread.

Parameters

action the action to run on the UI thread

So remove the runOnUiThread and update ui in onPreExecute and onPostExecute. Make HTTp request in doInbackground.

Tuesday, October 26, 2021
 
Silver Light
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share