【问题标题】:JSON response Error Code 201,but it should be 200 in Android HTTP POSTJSON响应错误代码201,但在Android HTTP POST中应该是200
【发布时间】:2011-03-29 08:57:44
【问题描述】:

因为我试图发布评级活动的结果。从用户那里读取评分值后,我通过 HTTP POST 调用 Web 服务并在其中设置 JSON 对象。由于我的网络服务器需要身份验证,我也能够验证这一点。

但真正的问题是响应代码应该是 200 但我收到 201。请告诉我哪里出错了!

代码:

        public static String sendJson(final int rating, final String url) {
    Thread t = new Thread() {
        public void run() {
            Looper.prepare(); // For Preparing Message Pool for the child
             HttpResponse response;

            // proxy
            final String PROXY = "xxx.xxx.xxx.xxx";
            // proxy host
            final HttpHost PROXY_HOST = new HttpHost(PROXY, 8080);
            HttpParams httpParameters = new BasicHttpParams();
            mHttpClient = new DefaultHttpClient(httpParameters);
            HttpConnectionParams.setConnectionTimeout(mHttpClient.getParams(),
                 10000); //Timeout Limit
            mHttpClient.getParams().setParameter(
                    ConnRoutePNames.DEFAULT_PROXY, PROXY_HOST);
            System.out.println("Sending proxy request: " + mHttpClient);
            // mHttpClient = new DefaultHttpClient();

            JSONObject json = new JSONObject();
            try {
                String reqUrl = new String(aBaseUrl + url);                  
                HttpPost post = new HttpPost(reqUrl);
                post.getParams().setParameter(
                        ConnRoutePNames.DEFAULT_PROXY, PROXY_HOST);
                post.addHeader(BasicScheme
                        .authenticate(new UsernamePasswordCredentials(
                                userName, password), "UTF-8", false));
                System.out.println("Sending Post proxy request: " + post);
                json.put("rating", rating);

                // json.put("password", pwd);
                // StringEntity se = new StringEntity( "JSON: " +json.toString());

                StringEntity se = new StringEntity(json.toString());
                System.out.println("JSON VALUE 2222 " + json.toString());
                se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
                // post.setEntity(new
                // ByteArrayEntity(json.toString().getBytes("UTF8")));

                post.setEntity(se);
                response = mHttpClient.execute(post);
                /* Checking response */

                statusCode = response.getStatusLine().getStatusCode();
                System.out.println("Http Execute finish " + statusCode);

                if(statusCode==200)
                {
                    HttpEntity entity = response.getEntity();
                    String getResponseText = EntityUtils.toString(entity);
                    System.out.println(" Post Response Text from Server : "
                            + getResponseText);


                }

                if (response != null) {
                    // InputStream in = response.getEntity().getContent();
                    // //Get the data in the entity
                    // HttpEntity entity = response.getEntity();
                    // String getResponseText =
                    // EntityUtils.toString(entity);
                    // System.out.println(" Post Response Text from Server : "
                    // + getResponseText);

                }
            } catch (Exception e) {
                e.printStackTrace();
                // createDialog("Error", "Cannot Estabilish Connection");
            }
            Looper.loop(); // Loop in the message queue
        }
    };
    t.start();
    return url;
}

【问题讨论】:

    标签: android json http-post


    【解决方案1】:

    201 是正常的响应代码,意思是“已创建”。您的网络服务报告成功,这是正常的。 HTTP 规范定义了这个代码。见http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

    【讨论】:

    • 是的,非常感谢!但根据我的网络服务规范,我必须收到 200。如果我从 Android 客户端做的一切正确,请告诉我?
    • 作为 Stackoverflow 的一项规则,我确实接受你的回答;-)。但只需要对上述问题进行最终确认!
    • 如我所见,一切正常。如果唯一的问题是响应代码,我认为您应该更改您的 if 语句以同时检查 201 代码
    • 您的 WebServices 是如何创建的?它的后端是什么? “我的网络服务规范”是什么意思?可以展示一下吗?
    • Web 服务是 REST 和 JAVA。这是服务器期望的格式:bold 请求标头 POST / XXXXXXXXalbum/rest/v1.0/pictures/12345678 Content-Type: application/json Request body { "rating":3 } Response header StatusCode : 200 好
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-08
    • 2010-12-24
    • 2021-10-21
    • 1970-01-01
    • 2014-12-07
    • 2021-11-16
    相关资源
    最近更新 更多