繁体   English   中英

在Android中发送JSON请求POST

[英]Sending JSON request POST in android

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

职位要求结构:

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

响应应该是这样的:

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

对于此示例,通常它应为“成功”键返回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响应。 那么我的代码有什么问题呢?

问题是,当我发送Post Request时,它总是以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));

            }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM