问题描述
我尝试使HTTPGET服务器的请求。但在邮件正文应该是一个JSON对象。低于code未工作,因为unit_id和sercret_key没有在身体上的消息服务器发送。我该怎么办呢?
JSONObject的:
{
unit_id:12345,
SECRET_KEY:sdfadfsa6as987654754
}
我的code:
私人的Htt presponse makeRequest的(INT ID,字符串SecretKey的)抛出异常{
BasicHttpParams PARAMS =新BasicHttpParams();
params.setParameter(ID,身份证); params.setParameter(SECRET_KEYSecretKey的); httpget.setHeader(接受,应用/ JSON); httpget.setParams(PARAMS);
httpget.setHeader(内容类型,应用/ JSON); //手柄什么被从页面返回
返回httpclient.execute(HTTPGET);
}
编辑:在PHP这个请求时这样
< PHP
$ CH = curl_init();
curl_setopt($ CH,CURLOPT_URLhttp://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,阵列(内容类型:应用程序/ JSON));
$响应= curl_exec($ CH);
curl_close($ CH);后续代码var_dump($响应);
基本上,你不能用 HTTP / GET 请求发送在人体内(JSON或任何东西)行数据。 的协议根本不允许你这样做。显然,你将不得不使用发表做,在Android的了。 :)
更新
我是不正确的。逸岸协议确实让你把一个实体到请求对象。这个类可以用来代替Apache的 HTTPGET
。
公共类HttpGetWithEntity扩展HttpEntityEnclosingRequestBase { 公共HttpGetWithEntity(){
超();
} 公共HttpGetWithEntity(URI URI){
超();
setURI(URI);
} 公共HttpGetWithEntity(字符串URI){
超();
setURI(URI.create(URI));
} @覆盖
公共字符串实现getMethod(){
返回HttpGet.METHOD_NAME;
}
}
和使用它像如下,
HttpClient的客户端=新DefaultHttpClient();
HttpGetWithEntity myGet =新HttpGetWithEntity(URL这里);
myGet.setEntity(新StringEntity(这是身体,UTF8));
HTT presponse响应= client.execute(myGet);
为源 HttpGetWithEntity
中找到<一href=\"http://grep$c$c.com/file/repo1.maven.org/maven2/io.searchbox/jest/0.0.4/io/searchbox/client/http/apache/HttpGetWithEntity.java?av=h\"相对=nofollow>这里
I try to make a request on server by HttpGet. But in message body should be a json object. Code below is not working because unit_id and sercret_key are not sent on server in body message. How can I do it?
JSONObject:
{
"unit_id": 12345,
"secret_key": "sdfadfsa6as987654754"
}
My code:
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);
}
Edit: in php this request is made like this
<?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);
Basically, you cannot send row data in the body (JSON or anything) with an HTTP/GET request. The protocol simply does not allow you to do that. Obviously, you will have to use POST to do that in Android too. :)
UPDATE
I was incorrect. Infact the protocol does allow you to put an entity into the request object. This class can be used instead of Apache HttpGet
.
public class HttpGetWithEntity extends HttpEntityEnclosingRequestBase {
public HttpGetWithEntity() {
super();
}
public HttpGetWithEntity(URI uri) {
super();
setURI(uri);
}
public HttpGetWithEntity(String uri) {
super();
setURI(URI.create(uri));
}
@Override
public String getMethod() {
return HttpGet.METHOD_NAME;
}
}
And use it like as follows,
HttpClient client = new DefaultHttpClient();
HttpGetWithEntity myGet = new HttpGetWithEntity("Url here");
myGet.setEntity(new StringEntity("This is the body", "UTF8"));
HttpResponse response = client.execute(myGet);
The source for HttpGetWithEntity
is found here
这篇关于与HTTPGET请求主体的Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!