Skip to content Skip to sidebar Skip to footer

How To Send A Name Value Pair Object Or Content Values Object Using Post In Android Api 23 Using Httpurl Connection?

//Here is what i have tried. // I don't know how to append inputurl and Content values and send this to server using HttpURLConnection. public JSONObject getJsonFromUrl(String inp

Solution 1:

if you want to post a JSON object to an url though HttpUrlConnection instance you can write is like this.

    DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream());
    wr.writeBytes(jsonObject.toString());
    wr.flush();
    wr.close();
Where,

jsonObject is the json object which you would be posting to the URL 

the entire code may look like this

URLurl=newURL("this would be your url where you want to POST");

HttpURLConnectionhttpURLConnection= (HttpURLConnection) url.openConnection();
httpURLConnection.setDoOutput(true);
// when you are posting do make sure you assign appropriate header// In this case POST.

httpURLConnection.setRequestMethod("POST");
httpURLConnection.connect();

// like this you can create your JOSN object which you want to sendJsonObjectjsonObject=newJsonObject();
jsonObject.addProperty("email", "dddd@gmail.com");
jsonObject.addProperty("password", "password");

// And this is how you will write to the URLDataOutputStreamwr=newDataOutputStream(httpURLConnection.getOutputStream());
wr.writeBytes(jsonObject.toString());
wr.flush();
wr.close();

Log.d("TAG", "" + IOUtils.toString(httpURLConnection.getInputStream()));

Post a Comment for "How To Send A Name Value Pair Object Or Content Values Object Using Post In Android Api 23 Using Httpurl Connection?"