Reputation: 2132
I am trying to use HTTPURLConnection make a POST method call to a server api. The JSON version of the body of my call is:
{
"grant_type" : "password",
"username" : "[email protected]",
"password" : "password"
}
How do I add this part using httpURLConnection?
Upvotes: 0
Views: 1544
Reputation: 6614
i think you can use the Android Rest-Client for sending data to a webservice
take classes RequestMethod.java
and RestClient.java
from this link https://github.com/TylerSmithNet/RESTclient-Android/tree/master/RESTclient-example/src/com/tylersmith/restclient
and use those in your project like this
String webServiceUrl = "your-service-url";
RestClient client = new RestClient(webServiceUrl);
String serverResponse = "";
String inputJsonString = "{" +
" \"grant_type\" : \"password\"," +
" \"username\" : \"[email protected]\"," +
" \"password\" : \"password\"" +
"}";
client.setJSONString(inputJsonString);
client.addHeader("Content-Type", "appication/json"); // if required
try {
client.execute(RequestMethod.POST);
if (client.getResponseCode() != 200) {
// response error
}
else
{
// the response from server
serverResponse = client.getResponse();
}
} catch (Exception e) {
// handle error
}
Upvotes: 1