开发者

JSON response Error Code 201,but it should be 200 in Android HTTP POST

开发者 https://www.devze.com 2023-02-20 06:24 出处:网络
As iam trying to post the result of rating activity. After reading the rating value from the user I am calling the webservice thro HTTP POST and setting JSON object in it. As My webserver needs authen

As iam trying to post the result of rating activity. After reading the rating value from the user I am calling the webservice thro HTTP POST and setting JSON object in it. As My webserver needs authentication, iam able to verify that as well.

But real problem is the response code should be 200 but iam receiving 201. Please suggest me where iam going wrong!

Code:

        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: " + mHttpClien开发者_JAVA百科t);
            // 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;
}


The 201 is normal respoinse code that means "Created". Your webservice is reporting a success with it, and it is normal. HTTP spec defines this code. See http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

0

精彩评论

暂无评论...
验证码 换一张
取 消