androidjsonresthttp-postresponse.addheader

Error in HttpPost.addheader in rest api call for android


This is my code . I am trying to do a post on the rest Api from android (API 10)

        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();

    }

I just don't know what to add (as the first string)in the last addHeader method ! I tried "name", "ID" etc. Its not listed in the API too. The API documentation is here : http://reactomews.oicr.on.ca:8080/ReactomeRESTfulAPI/ReactomeRESTFulAPI.html I tried to use firebug to see the post request in browser but it says post data = "PPP2R1A,CEP192,AKAP9,CENPJ,CEP290,DYNC1H1" .

Right now I am using "body" in there and I am getting a json respnse of length 0 . But if i try on browser from the documentation link , I get a json response. so the error is in the addHeader part.


Solution

  • The problem is your assumption that the data should be part of the header, because it shouldn't. If I run the sample request from the API through a random webproxy, I can see the following headers:

    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
    

    In other words: none of the "PPP2R1A,CEP192,AKAP9,CENPJ,CEP290,DYNC1H1" strings are there. In stead, that data is part of the post body, or the 'entity' that you can set to the post method.

    Something like this should probably do it:

    // 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.