我正在尝试使用 HTTPURLConnection 对服务器 api 进行 POST 方法调用。我的通话正文的 JSON 版本是:
{
"grant_type" : "password",
"username" : "perry.marange@zmod.co",
"password" : "password"
}
如何使用 httpURLConnection 添加这部分?
我正在尝试使用 HTTPURLConnection 对服务器 api 进行 POST 方法调用。我的通话正文的 JSON 版本是:
{
"grant_type" : "password",
"username" : "perry.marange@zmod.co",
"password" : "password"
}
如何使用 httpURLConnection 添加这部分?
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\" : \"perry.marange@zmod.co\"," +
" \"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
}