Sunday 22 July 2012

Working with HttpsUrlConnection


Refer this doc over HttpsUrlConnection.
To create key and import certificate use Portecle tool.
To create key using Keytool, follow the doc.


String query = "emailId=" + URLEncoder.encode("xxx@gmail.com", HTTP.UTF_8) + "&pwd=" + URLEncoder.encode("password", HTTP.UTF_8);


HttpsURLConnection urlConnection = null;
try{
    KeyStore keyStore = KeyStore.getInstance("BKS");
    InputStream in =  
    getResources().openRawResource(R.raw.keystore);
    keyStore.load(in, "password".toCharArray());
    TrustManagerFactory tmf = 
    TrustManagerFactory.getInstance("X509");
    tmf.init(keyStore);


    SSLContext context = SSLContext.getInstance("TLS");
    context.init(null, tmf.getTrustManagers(), null);
    System.setProperty("http.keepAlive", "false");
    URL url = new URL("https://www.google.co.in/");
    urlConnection = (HttpsURLConnection) url.openConnection();
     
urlConnection.setSSLSocketFactory(context.getSocketFactory());
    urlConnection.setDoInput(true);
    urlConnection.setDoOutput(true);
    urlConnection.setRequestMethod("POST");
    urlConnection.setRequestProperty("content-type",
    "application/x-www-form-urlencoded");
    urlConnection.setRequestProperty("Content-length", "" +  query.getBytes().length);

     //To post data...
     DataOutputStream output = new DataOutputStream( urlConnection.getOutputStream() );
     output.writeBytes( query );
     output.flush();
     output.close();

     InputStream stream = null;


     if ( urlConnection.getResponseCode() == 201 )
stream = urlConnection.getInputStream();
     else
       //Report error...

     urlConnection.disconnect();


}catch(Exception e){
     urlConnection.disconnect();
}



profile for Vineet Shukla at Stack Overflow, Q&A for professional and enthusiast programmers

Thursday 5 July 2012

WebView Settings


Note: To know the HTML5 browser support for your device, you only need to open the url in your browser: http://html5test.com/


WebSettings settings = mWebView.getSettings();
//To enable the built-in zoom
settings.setBuiltInZoomControls(true);


//To enable JavaScript
settings.setJavaScriptEnabled(true);


//To enable Plugin
settings.setPluginState(PluginState.ON);


Plugin:
plug-ins are commonly used in web browsers to play video, 
scan for viruses, and display new file types. 
Well-known plug-ins examples include 
Adobe Flash Player, QuickTime, and Microsoft Silverlight.


//To enable or disable file access within WebView. 
//File access is enabled by default.
settings.setAllowFileAccess(true);


//To enable dom storage
//DOM Storage is a way to store meaningful amounts of 
//client-side data in a persistent and secure manner.
settings.setDomStorageEnabled(true);


//To enable wide viewport.
settings.setUseWideViewPort(true);


//To enable JavaScript to open windows automatically.
settings.setJavaScriptCanOpenWindowsAutomatically(true);


//To enable twitter, youtube etc.
settings.setUserAgentString("Mozilla/5.0 (Linux; U; Android 2.0; en-us; Droid Build/ESD20) 
AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17");


Detailed Description: WebSettingsWebView
profile for Vineet Shukla at Stack Overflow, Q&A for professional and enthusiast programmers