Adding body of call to POST using HttpURLConnection

冷暖自知 提交于 2019-12-01 13:51:27

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
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!