我成功地使用此代码HTTP通过GET方法发送 带有一些参数的请求
void sendRequest(String request)
{
// i.e.: request = "http://example.com/index.php?param1=a¶m2=b¶m3=c";
URL url = new URL(request);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("GET");
connection.setRequestProperty("Content-Type", "text/plain");
connection.setRequestProperty("charset", "utf-8");
connection.connect();
}
Run Code Online (Sandbox Code Playgroud)
现在我可能需要通过POST方法发送参数(即param1,param2,param3),因为它们非常长.我想在该方法中添加一个额外的参数(即String httpMethod).
如何能够尽可能少地更改上面的代码,以便能够通过GET或发送参数POST?
我希望改变
connection.setRequestMethod("GET");
Run Code Online (Sandbox Code Playgroud)
至
connection.setRequestMethod("POST");
Run Code Online (Sandbox Code Playgroud)
本来可以做到的,但参数仍然是通过GET方法发送的.
有HttpURLConnection任何方法可以帮助吗?有没有有用的Java构造?
任何帮助将非常感谢.
我想发送以下JSON文本
{"Email":"aaa@tbbb.com","Password":"123456"}
Run Code Online (Sandbox Code Playgroud)
到Web服务并阅读响应.我知道如何阅读JSON.问题是上述JSON对象必须以变量名发送jason.
我怎么能从android做到这一点?有哪些步骤,例如创建请求对象,设置内容标题等.
已提供的NOT DUPLICATE.Link是旧版本."http客户端"已在api23中删除
我想发送json对象:
{"emailId":"ashish.bhatt@mobimedia.in","address":"Naya bans","city":"Noida","pincode":"201301","account_number":"91123546374208","bank_name":"Axis Bank","branch_name":"91123546374208","ifsc_code":"UTI0000879"}
Run Code Online (Sandbox Code Playgroud)
到网址:
http://10digimr.mobimedia.in/api/mobile_retailer/update_profile 我该怎么办?通过邮政方式?
方法:
POST /api/mobile_retailer/update_profile
Run Code Online (Sandbox Code Playgroud)
强制关键:
{"emailId","address"}
Run Code Online (Sandbox Code Playgroud)
请求JSON:
{"emailId":"ashish.bhatt@mobimedia.in","address":"Naya bans","city":"Noida","pincode":"201301","account_number":"91123546374208","bank_name":"Axis Bank","branch_name":"91123546374208","ifsc_code":"UTI0000879"}
Run Code Online (Sandbox Code Playgroud)
响应:
{"message":"Mail Send","data":true,"status":200}
Run Code Online (Sandbox Code Playgroud)