I have a simple http client that calls https service:
import android.util.Log;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.AbstractHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.List;
/**
*/
public class HttpSimpleClient {
private static final String TAG = HttpSimpleClient.class.getSimpleName();
private static HttpSimpleClient instance;
private HttpSimpleClient(){}
public static HttpSimpleClient instance(){
if (instance==null) {
instance = new HttpSimpleClient();
}
return instance;
}
public <T> T post(URL url,
List<NameValuePair> header,
List<NameValuePair> parameter,
ResponseHandler<T> responseHandler) throws IOException {
return post(url, header, parameter, responseHandler, null, null);
}
public <T> T post(URL url,
List<NameValuePair> header,
List<NameValuePair> parameter,
ResponseHandler<T> responseHandler,
AuthScope proxy,
UsernamePasswordCredentials proxyUser) throws IOException {
HttpClient httpClient = new DefaultHttpClient();
HttpPost request = new HttpPost(url.toString());
if (header!=null) {
for (NameValuePair head : header){
request.setHeader(head.getName(), head.getValue());
}
Log.d(TAG, "Aggiunti header: "+ header.size());
} else {
// Header di default
request.setHeader("Content-Type", "application/x-www-form-urlencoded");
request.setHeader("User-Agent", System.getProperty("http.agent"));
Log.d(TAG, "Aggiunti header di defautl");
}
if (parameter!=null) {
request.setEntity(new UrlEncodedFormEntity(parameter, HTTP.UTF_8));
Log.d(TAG, "Aggiunti parametri: "+ parameter.size());
}
if (proxy!=null) {
if (proxyUser!=null) {
((AbstractHttpClient) httpClient).getCredentialsProvider().setCredentials(proxy, proxyUser);
} else {
// TODO gestire proxy senza credenziali
((AbstractHttpClient) httpClient).getCredentialsProvider().setCredentials(proxy, null);
}
Log.d(TAG, "Impostato Proxy per la connessione");
}
return httpClient.execute(request, responseHandler);
}
public static String httpResponseToString(HttpResponse httpResponse, int bufferSize) throws IOException {
InputStream content = httpResponse.getEntity().getContent();
int numRead;
byte[] buffer = new byte[bufferSize];
ByteArrayOutputStream outString = new ByteArrayOutputStream();
try{
while ((numRead = content.read(buffer)) != -1) {
outString.write(buffer, 0, numRead);
}
} finally {
content.close();
}
return new String(outString.toByteArray());
}
}
And i use it in a AsyncTask:
response = HttpSimpleClient.instance().post(
getServiceURL(), // HTTPS url
mHeader, // List<NameValuePair>
mParameter, // List<NameValuePair>
getResponseHandler() // org.apache.http.client.ResponseHandler
);
Tested on Android api>=10