0

I'm trying to perform a GET request to the server that returns me a JSON file. But I am getting an error in the HTTP statusLine / 422. Anyone know why. Below I show how I'm doing

public void testConverteArquivoJsonEmObjetoJava() {

    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpGet get = new HttpGet(
            "http://safe-sea-4024.ppooiheroku4554566adffasdfasdfalaqwerpcp.com/crimes/mobilelist");

    get.setHeader("Accept", "application/json");
    get.setHeader("Content-type", "application/json");

    get.getParams()
            .setParameter("token",
                    "0V1AYFK12SeCZHYgXbNMew==$tRqPNplipDwtbD0vxWv6GPJIT6Yk5abwca3IJ88888a6JhMs=");

    HttpResponse httpResponse;
    try {
        httpResponse = httpClient.execute(get);
        String jsonDeResposta = EntityUtils.toString(httpResponse
                .getEntity());

        System.out.println();

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Douglas Mesquita
  • 860
  • 1
  • 13
  • 30

2 Answers2

1

Usually you do not specify a Content-Type header with a GET request. This header tells the server how to interpret the entity includes in the message. It is possible that the server side is expecting a JSON entity even though GET cannot include a body. Try removing the Content-Type header.

I tried the URL that you cleverly changed and got it to work fine. However, I did get a 422 when I specified a different token query parameter. Being that the status line is missing a phrase, I would assume that the Ruby application is generating it.

D.Shawley
  • 58,213
  • 10
  • 98
  • 113
  • You can test with this URL [link]http://safe-sea-4024.herokuapp.com/crimes/mobilelist I removed the header and continues with error. Do you think the problem is on the server Ruby? – Douglas Mesquita Feb 08 '13 at 02:32
0

I managed to solve the problem. I was passing the parameter so wrong. According to this post [blog]:How to add parameters to a HTTP GET request in Android? "link". This method is used to that I kind of POST request

this method is correct

 public void testConverteArquivoJsonEmObjetoJava() {
    List<NameValuePair> params = new LinkedList<NameValuePair>();

    params.add(new BasicNameValuePair("token","0V1AYFK12SeCZHYgXbNMew==$="));

    String paramString = URLEncodedUtils.format(params, "utf-8");

    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpGet get = new HttpGet(
            "http://safep.com/crimes/mobilelist" + "?"
                    + paramString);
    HttpResponse httpResponse;
    try {

        httpResponse = httpClient.execute(get);
        String jsonDeResposta = EntityUtils.toString(httpResponse
                .getEntity());

        System.out.println();

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}`
Community
  • 1
  • 1
Douglas Mesquita
  • 860
  • 1
  • 13
  • 30