java – 请求body android的HttpGet
作者:互联网
我尝试通过HttpGet在服务器上发出请求.但是在消息体中应该是一个json对象.下面的代码无法正常工作,因为body_id和sercret_key不会在正文消息中的服务器上发送.我该怎么做?
的JSONObject:
{
"unit_id": 12345,
"secret_key": "sdfadfsa6as987654754"
}
我的代码:
private HttpResponse makeRequest(int id, String secretKey) throws Exception {
BasicHttpParams params = new BasicHttpParams();
params.setParameter("id", id);
params.setParameter("secret_key", secretKey);
httpget.setHeader("Accept", "application/json");
httpget.setParams(params);
httpget.setHeader("Content-type", "application/json");
// Handles what is returned from the page
return httpclient.execute(httpget);
}
编辑:在php中,这个请求是这样的
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://0101.apiary.io/api/reservations/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\n \"unit_id\": 12345,\n \"secret_key\": \"sdfadfsa6as987654754\"\n}");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
解决方法:
基本上,您不能使用HTTP / GET请求在正文(JSON或任何内容)中发送行数据.该协议根本不允许您这样做.显然,您还必须使用POST在Android中执行此操作.
标签:http-get,android,java,json 来源: https://codeday.me/bug/20190825/1723361.html