3

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

JSON对象:

{
"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);
4

3 回答 3

3

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

更新

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

并像下面这样使用它,

HttpClient client = new DefaultHttpClient();
HttpGetWithEntity myGet = new HttpGetWithEntity("Url here");
myGet.setEntity(new StringEntity("This is the body", "UTF8"));
HttpResponse response = client.execute(myGet);

来源HttpGetWithEntity这里

于 2013-09-17T06:25:53.703 回答
2

如果您想将 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);    
}
于 2013-03-04T13:39:32.303 回答
1

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

带有正文的 Apache HttpClient GET

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

于 2013-03-04T14:44:09.820 回答