HttpGet请求机构android

时间:2013-03-04 13:06:52

标签: java android json http-get

我尝试通过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);

3 个答案:

答案 0 :(得分:3)

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

<强>更新

我错了。事实上,协议允许您将实体放入请求对象中。可以使用此类代替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的来源here

答案 1 :(得分: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);    
}

答案 2 :(得分:1)

这是一个很好的例子,如何做到这一点,它正在发挥作用。

Apache HttpClient GET with body

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