0

我正在尝试对返回 JSON 文件的服务器执行 GET 请求。但是我在 HTTP statusLine / 422 中遇到错误。任何人都知道为什么。下面我展示了我的表现

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

2 回答 2

1

通常您不会在请求中指定Content-Type标头。GET此标头告诉服务器如何解释消息中包含的实体。即使GET不能包含正文,服务器端也可能期望 JSON 实体。尝试删除Content-Type标题。

我尝试了您巧妙更改的 URL 并使其正常工作。但是,当我指定不同的令牌查询参数时,我确实得到了 422。由于状态行缺少一个短语,我假设 Ruby 应用程序正在生成它。

于 2013-02-08T02:22:41.433 回答
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();
    }
}`
于 2013-02-09T03:44:27.840 回答