我正在尝试使用 HTTPURLConnection 对服务器 api 进行 POST 方法调用。我的通话正文的 JSON 版本是:
{
"grant_type" : "password",
"username" : "[email protected] /cdn-cgi/l/email-protection",
"password" : "password"
}
如何使用 httpURLConnection 添加此部分?
我想你可以使用Android Rest 客户端 https://github.com/TylerSmithNet/RESTclient-Android用于将数据发送到网络服务
上课RequestMethod.java
and RestClient.java
从这个链接https://github.com/TylerSmithNet/RESTclient-Android/tree/master/RESTclient-example/src/com/tylersmith/restclient https://github.com/TylerSmithNet/RESTclient-Android/tree/master/RESTclient-example/src/com/tylersmith/restclient
并像这样在您的项目中使用它们
String webServiceUrl = "your-service-url";
RestClient client = new RestClient(webServiceUrl);
String serverResponse = "";
String inputJsonString = "{" +
" \"grant_type\" : \"password\"," +
" \"username\" : \"[email protected] /cdn-cgi/l/email-protection\"," +
" \"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
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)