I am creating an Android application and I send data from Android application to servlet through HttpClient. I use HttpPost method.
I read in Android developer site
You don't have to switch, you can continue using apache's library like I described in this answer. There are really no downsides to continue using apache's library - it's only google's marketing.
When I read the already mentioned Google post about best practices doing HTTP requests in newer versions of Android, I thought somebody was kidding me. HttpURLConnection
is really a nightmare to use, compared to almost any other way to communicate with HTTP servers (apart from direct Socket communication).
I didn't find a really slim library for Android to do the heavy lifting, so I wrote my own. You can find it at DavidWebb including a list of alternative libraries which I found (unfortunately) after developing the library.
Your code would look more or less like this:
public void testPostToUrl() throws Exception {
String[] values = new String[3];
Webb webb = Webb.create();
Response<String> response = webb
.post("http://www.example.com/abc.php")
.param("param1", values[0])
.param("param2", values[1])
.param("param3", values[2])
.asString();
assertEquals(200, response.getStatusCode());
assertNotNull(response.getBody());
assertTrue(response.getBody().contains("my expected result"));
}
public void testPostToUrlShorter() throws Exception {
String[] values = new String[3];
Webb webb = Webb.create();
String result = webb
.post("http://www.example.com/abc.php")
.param("param1", values[0])
.param("param2", values[1])
.param("param3", values[2])
.ensureSuccess()
.asString()
.getBody();
assertTrue(result.contains("my expected result"));
}
You should absolutely be using HttpUrlConnection
:
For Gingerbread and better, HttpURLConnection is the best choice... New applications should use HttpURLConnection...
--Google (circa. 2011)
However, there is no easy way just to "switch". The APIs are totally different. You are going to have to rewrite your networking code. There are perfect examples in the documentation on how to submit a GET and POST requests as well as in the SDK sample apps.