试图在Android上的HTTPGet中放置JSON请求

时间:2014-03-09 18:31:26

标签: android androidhttpclient

我的应用正在使用的api期待对搜索结果的GET请求如下所示:

Method: GET
Headers:
{
    "Content-Type": "application/json",
    "X-AUTHORIZATION": "8eb40dba2f0c6d7de8b9c6e1865aa507"
}
Request:
{
    "keyword": "X S"
}
Response:
{
    "status": "success",
    "code": 0,
    "meta": {
        "exec_time": 0.012183904647827
    },
    "message": "",
    "data": [

    ]
}

我在网上找到了一个名为HttpGetWithEntity的课程,如下所示:

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;
    }
}

我正在实施它:

public HttpResponse invokeXAUTHGETJsonService(String url, String token,
            String jsonPost) {

        Log.v("KEYWORD", "KEYWORD GOING UP:"+jsonPost);

        HttpParams params = new BasicHttpParams();

        HttpResponse response = null;

        try {
            HttpGetWithEntity request = new HttpGetWithEntity(url);
            Log.v("UPU", "URL:" + url +" token:"+token);

            request.setHeader("X-AUTHORIZATION", token);
            request.setHeader("Accept", "application/json");
            request.addHeader("Content-Type", "application/json");

            StringEntity se = new StringEntity(jsonPost);
            request.setEntity(se);

            response = client.execute(request);
        } catch (ClientProtocolException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }
        return response;

    }

我检查我发布的json的日志如下:

03-09 14:38:01.034:V / KEYWORD(11739):KEYWORD CREATED:{" keyword":" e"}

问题是,似乎JSON永远不会到达api或被忽略。有谁知道我做错了什么..

感谢

1 个答案:

答案 0 :(得分:2)

GET方法不会将request.entity发送到服务器..

而不是使用“request.setEntity()..”

只需访问请求设置的参数“”关键字“:”X S“”作为parm K-V对并将它们附加到您正在获取的网址:

域/路径?关键字= X%20S

除非你是POST或PUT方法,否则不要使用该实体。