java 带有请求正文android的HttpGet

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15202300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 18:50:20  来源:igfitidea点击:

HttpGet with request body android

javaandroidjsonhttp-get

提问by Amsik

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?

我尝试通过 HttpGet 在服务器上发出请求。但在消息正文中应该是一个 json 对象。下面的代码不起作用,因为 unit_id 和 sercret_key 未在正文消息中发送到服务器。我该怎么做?

JSONObject:

JSON 对象:

{
"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 这个请求是这样提出的

<?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);

回答by Subin Sebastian

Basically, you cannot send row data in the body (JSON or anything) with an HTTP/GETrequest. The protocol simply does not allow you to do that.Obviously, you will have to use POSTto do that in Android too. :)

基本上,您不能使用HTTP/GET请求在正文(JSON 或任何内容)中发送行数据。该协议根本不允许您这样做。显然,您也必须在 Android 中使用POST来执行此操作。:)

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.

我错了。事实上,该协议确实允许您将实体放入请求对象中。这个类可以用来代替 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 HttpGetWithEntityis found here

来源HttpGetWithEntity可以在这里找到

回答by Emil Adz

If you wnat to add JSON object to your request it must be a Post request, and this is how you perform it:

如果您想将 JSON 对象添加到您的请求中,它必须是一个 Post 请求,这就是您执行它的方式:

public static String sendComment (String commentString, int taskId, String sessionId, int displayType, String url) throws Exception{

    //creating map object to creat Json object from it
    Map<String, Object> jsonValues = new HashMap<String, Object>();
    jsonValues.put("sessionID", sessionId);
    jsonValues.put("NewTaskComment", commentString);
    jsonValues.put("TaskID" , taskId);
    jsonValues.put("DisplayType" , displayType);
    JSONObject json = new JSONObject(jsonValues);

    //creating a post request.
    DefaultHttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(url + SEND_COMMENT_ACTION);

    //setting json object to post request.
    AbstractHttpEntity entity = new ByteArrayEntity(json.toString().getBytes("UTF8"));
    entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
    post.setEntity(entity);

    //this is your response:
    HttpResponse response = client.execute(post);
    return getContent(response);    
}

回答by Amsik

Here is nice example how to do it and it is working.

这是如何做到这一点的好例子,它正在工作。

Apache HttpClient GET with body

带有正文的 Apache HttpClient GET

But this approach is against RESTFUL api i think.

但是我认为这种方法是针对 RESTFUL api 的。