在android中发送JSON请求POST

时间:2014-10-29 22:55:56

标签: android json rest

我正在尝试将json post请求发送到:http://5.101.97.65:3000/en/api/sessions

发布申请结构:

{user: {email: 'value', password: 'value '} }

响应应该是这样的:

{"success":true Or False,"info":"...","data":{"auth_token":"...."}}

对于这个例子,它应该为“success”键返回true值:

{"user":{"email":"user@example.com","password":"changeme"}}

这是我的代码:

final String myUrl ="http://5.101.97.65:3000/api/sessions";
        String result = null;
        String params = "{\"user\":{\"email\":\"user@example.com\",\"password\":\"changeme\"}}";
        try {
            restHandler rh = new restHandler();
            result = rh.getResponse(myUrl, 2, params);
            JSONObject rs = new JSONObject(result);
            if (rs.getBoolean("success"))
                return "good";
            else
                return "baad";
        }
        catch (Exception e){
            e.printStackTrace();
            return "baaad";
        }

restHandler 类(仅限函数getResponse):

public String getResponse (String url, int method, String params) {
        try {
            // http client
            HttpClient httpClient = new DefaultHttpClient(new BasicHttpParams());
            HttpEntity httpEntity = null;
            HttpResponse httpResponse = null;

            // Checking http request method type
            if (method == 2) {
                HttpPost httpPost = new HttpPost(url);
                if (params != null)
                {
                    httpPost.setHeader("Content-type", "application/json");
                    httpPost.setEntity(new StringEntity(params, "UTF8"));
                }
                httpResponse = httpClient.execute(httpPost);

            } else if (method == 1) {
                // code for httpGet not needed here
            }

            httpEntity = httpResponse.getEntity();
            response = EntityUtils.toString(httpEntity);
            httpClient = null;
            httpEntity = null;
            httpResponse = null;


        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return response;
    }

我总是得到的问题是错误页面的HTML代码甚至不是json响应。那我的代码有什么问题?

1 个答案:

答案 0 :(得分:2)

问题在于,当我发送帖子请求时总是被发送为PlainText,所以我改变了这个:

HttpClient httpClient = new DefaultHttpClient(new BasicHttpParams());

对此:

HttpClient httpClient = new DefaultHttpClient();

然后我补充道:

httpPost.addHeader("Accept", "application/json");

解决问题422 unprocessable entity的问题。

最终代码(仅限更新部分)

HttpClient httpClient = new DefaultHttpClient();
        HttpEntity httpEntity = null;
        HttpResponse httpResponse = null;

        // Checking http request method type
        if (method == POST) {
            HttpPost httpPost = new HttpPost(url);
            if (params != null)
            {
                httpPost.setHeader("Content-type", "application/json");
                httpPost.addHeader("Accept", "application/json");
                httpPost.setEntity(new StringEntity(params));

            }