JAVA GET response for HTTP POST Request

半腔热情 提交于 2020-01-06 08:32:32

问题


How to get the status code or something better to know if an message was sent? I already do this, but off course it don't work.

            httppost.setEntity(new StringEntity(message.toString()));

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);
            String status = response.getStatusLine().toString();

            Log.d(CLASS_TAG + "showOnScreenMessage", "Message sent! ");
            Log.d(CLASS_TAG + "showOnScreenMessage", "Status : " + status );

            // if response == 201 , the message has be received, Set True to acknowledgment_message 
            if ( status == "201"){
                Log.d(CLASS_TAG + "showOnScreenMessage", "Message received! ");


            }else{
                // Do nothing already false
                Log.d(CLASS_TAG + "showOnScreenMessage", "Message not received! ");
            }

Thanks for your suggestions


回答1:


String compare as it's say above.

status.equals("201")

Or you can use :

response.getStatusLine().getStatusCode() != HttpStatus.SC_OK

All status here




回答2:


Change:

status == "201"

to:

status.equals("201")


来源:https://stackoverflow.com/questions/22486270/java-get-response-for-http-post-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!