【问题标题】:Adding body of call to POST using HttpURLConnection使用 HttpURLConnection 将调用正文添加到 POST
【发布时间】:2012-07-04 09:31:16
【问题描述】:

我正在尝试使用 HTTPURLConnection 对服务器 api 进行 POST 方法调用。我的通话正文的 JSON 版本是:

{
    "grant_type"        : "password",
    "username"          : "perry.marange@zmod.co",
    "password"          : "password"
}

如何使用 httpURLConnection 添加这部分?

【问题讨论】:

    标签: android http-post httpurlconnection


    【解决方案1】:

    我认为您可以使用Android Rest-Client 将数据发送到网络服务

    从此链接https://github.com/TylerSmithNet/RESTclient-Android/tree/master/RESTclient-example/src/com/tylersmith/restclientRequestMethod.javaRestClient.java上课

    并像这样在您的项目中使用它们

    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
    }
    

    【讨论】:

    • @user1476075 如果它解决了你的问题,你能把它标记为答案吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-05
    • 2016-12-18
    • 2022-08-16
    • 1970-01-01
    • 1970-01-01
    • 2016-01-23
    • 2021-07-02
    相关资源
    最近更新 更多