增加身体呼叫使用为POST的HttpURLConnection [英] Adding body of call to POST using HttpURLConnection

查看:96
本文介绍了增加身体呼叫使用为POST的HttpURLConnection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用的HttpURLConnection进行POST方法调用服务器API。 JSON的版本我的电话的主体是:

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"          : "perry.marange@zmod.co",
    "password"          : "password"
}

我如何添加使用HttpURLConnection的这一部分?

How do I add this part using httpURLConnection?

推荐答案

我想你可以使用的Andr​​oid休息 - 客户端,以将数据发送到一个web服务

i think you can use the Android Rest-Client for sending data to a webservice

上课 RequestMethod.java RestClient.java 从这个链接<一个href="https://github.com/TylerSmithNet/RESTclient-Android/tree/master/RESTclient-example/src/com/tylersmith/restclient" rel="nofollow">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
}

这篇关于增加身体呼叫使用为POST的HttpURLConnection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆