Fabrizio Russo Chiamate HTTPS in POST


2013/03/09   Chiamate HTTPS in POST

Released on GitHub an android custom view to show a radar component (with animation)

The view component exposed two simple methofs to start and stop the animation.
The animation effect simulates the drag effect visible on the typical green phosphor of the radar.

Here a semaple activity to show how start and stop the animation


public static String postUrl(String url, Map params) throws Exception {
	SchemeRegistry schemeRegistry = new SchemeRegistry();
	schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
	schemeRegistry.register(new Scheme("https", new SimpleSSLSocketFactory(), 443));
		
        HttpParams httpParams = new BasicHttpParams();
	httpParams.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 30);
	httpParams.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(30));
	httpParams.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false);
	HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);

	ClientConnectionManager cm = new SingleClientConnManager(httpParams, schemeRegistry);
	HttpClient httpClient = new DefaultHttpClient(cm, httpParams);
		
	HttpPost httpPost = new HttpPost(url);
		
	// Add your data
        List nameValuePairs = new ArrayList(params.size());
        for (Entry entry : params.entrySet()) {
        	String key = entry.getKey();
        	String val = entry.getValue();
        	nameValuePairs.add(new BasicNameValuePair(key, val));
        }
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
		
	HttpResponse response = httpClient.execute(httpPost);
	int returnCode = response.getStatusLine().getStatusCode();
		
	String htmlBody = EntityUtils.toString(response.getEntity());
		
	if(returnCode != HttpStatus.SC_OK) {
		Log.e(Const.LOG_TAG, "- Network error reading URL: " + url);
		Log.e(Const.LOG_TAG, "- Network error: HttpStatusCode : " + response.getStatusLine().getStatusCode());
		return null;
	}
	return htmlBody;
}