【问题标题】:Trying to put JSON request in HTTPGet on android试图将 JSON 请求放在 Android 上的 HTTPGet 中
【发布时间】:2014-03-09 18:31:25
【问题描述】:

我的应用正在使用的 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 或被忽略。有谁知道我做错了什么..

谢谢

【问题讨论】:

    标签: android androidhttpclient


    【解决方案1】:

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

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

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

    域/路径?keyword=X%20S

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

    【讨论】:

    • 天啊..我筋疲力尽了..你说得对..我看到的只是 json,这不是它所期望的..需要睡觉..谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-11
    • 1970-01-01
    • 2018-02-14
    相关资源
    最近更新 更多