0

这是我的代码。我正在尝试从 android (API 10) 发布关于其余 Api 的帖子

        HttpPost httpPost = new HttpPost(
                    "http://www.reactomews.oicr.on.ca:8080/ReactomeRESTfulAPI/RESTfulWS/queryHitPathways");

        httpPost.addHeader("Accept", "application/json");
        httpPost.addHeader("Content-Type"," text/plain; charset=UTF-8");
        httpPost.addHeader("","PPP2R1A,CEP192,AKAP9,CENPJ,CEP290,DYNC1H1");
                    try {
            HttpResponse response = client.execute(httpPost);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if (statusCode == 200) {
                HttpEntity entity = response.getEntity();
                InputStream content = entity.getContent();
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(content));
                String line;
                while ((line = reader.readLine()) != null) {
                    builder.append(line);
                }
            } else {
                System.out.println(statusCode);
                Log.e(Gsearch.class.toString(), "Failed to download file");
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return builder.toString();

    }

我只是不知道在最后一个 addHeader 方法中添加什么(作为第一个字符串)!我尝试了“名称”、“ID”等。它也没有在 API 中列出。API 文档在这里:http://reactomews.oicr.on.ca:8080/ReactomeRESTfulAPI/ReactomeRESTFulAPI.html 我尝试使用 firebug 在浏览器中查看发布请求,但它显示 post data = "PPP2R1A,CEP192,AKAP9, CENPJ、CEP290、DYNC1H1" .

现在我在那里使用“body”,我得到一个长度为 0 的 json 响应。但是,如果我从文档链接尝试浏览器,我会得到一个 json 响应。所以错误在 addHeader 部分。

4

1 回答 1

1

问题是您假设数据应该是标题的一部分,因为它不应该。如果我通过随机 webproxy 从 API 运行示例请求,我可以看到以下标头:

POST            /ReactomeRESTfulAPI/RESTfulWS/queryHitPathways HTTP/1.1
Host            reactomews.oicr.on.ca:8080
Content-Length  41
Accept          application/json
Origin          http://reactomews.oicr.on.ca:8080
User-Agent      Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31
Content-type    text/plain
Referer         http://reactomews.oicr.on.ca:8080/ReactomeRESTfulAPI/ReactomeRESTFulAPI.html
Accept-Encoding gzip,deflate,sdch
Accept-Language nl-NL,nl;q=0.8,en-US;q=0.6,en;q=0.4
Accept-Charset  ISO-8859-1,utf-8;q=0.7,*;q=0.3

换句话说:没有任何"PPP2R1A,CEP192,AKAP9,CENPJ,CEP290,DYNC1H1"字符串存在。相反,该数据是帖子正文的一部分,或者您可以设置为 post 方法的“实体”。

这样的事情可能应该这样做:

// creating of HttpPost omitted
httpPost.addHeader("Accept", "application/json");
httpPost.addHeader("Content-Type"," text/plain; charset=UTF-8");
StringEntity entity = new StringEntity("PPP2R1A,CEP192,AKAP9,CENPJ,CEP290,DYNC1H1");
httpPost.setEntity(entity);
// execute post and get result etc.
于 2013-04-28T19:33:34.350 回答