I'm trying to send an HTTP/HTTPS post request from my Android client.
Question
Why does my code fail?
So far
I created an apache class/HttpClient call. Everything worked fine:
HttpClient httpClient = new DefaultHttpClient();
I've read that this method has been deprecated, so I've switched to the new recommended method:
HttpClient httpClient = HttpClientBuilder.create().build();
Eclipse didn't have this class, so I had to download Apache HttpClient 4.3.3. I imported it to my project by copying it into the libs
folder and adding them to my build path (imported httpclient, httpclient-cache, httpcore, httpmime, fluent-hc, commons-logging, commons-codec).
Error message
06-05 02:15:26.946: W/dalvikvm(29587): Link of class 'Lorg/apache/http/impl/conn/PoolingHttpClientConnectionManager;' failed
06-05 02:15:26.946: E/dalvikvm(29587): Could not find class 'org.apache.http.impl.conn.PoolingHttpClientConnectionManager', referenced from method org.apache.http.impl.client.HttpClientBuilder.build
Most recent code
static private String insertJson(String json,String url){
HttpClient httpClient = HttpClientBuilder.create().build();
String responseString = "";
try {
HttpPost request = new HttpPost(url);
StringEntity params =new StringEntity(json, "UTF-8");
request.addHeader("content-type", "application/json");
request.setEntity(params);
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
responseString = EntityUtils.toString(entity, "UTF-8");
}catch (Exception ex) {
ex.printStackTrace();
// handle exception here
} finally {
httpClient.getConnectionManager().shutdown();
}
return responseString;
}