Android WebView.postUrl () shows a blank screen when sending to HTTPS URL

In my Android application, I send data to the servlet httpss url WebViewas below

String postData = "fileContents=" + fileCon;
WebView.postUrl(url, EncodingUtils.getBytes(postData, "BASE64"));

The URL of the above code is the URL of the servlet for which I have to post some data, and from there redirect to another URL.

The above code worked fine when the servlet URL is simple HTTP. But when changing to https, a blank screen is displayed.

I tried the following solution for the problem with android https: http://blog.antoine.li/index.php/2010/10/android-trusting-ssl-certificates/

I removed the above code from the method onCreate()and tried the following code

ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("fileContents", fileCon));
DefaultHttpClient client = new MyHttpClient(getApplicationContext());
try {   
    HttpPost request = new HttpPost(url);
    UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
    request.setEntity(formEntity);
    HttpResponse resp = client.execute(request);
} catch(Exception e){
    e.printStackTrace();
}

, . .

, loadUrl, postUrl ?

WebView?

+5
1

    ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
    postParameters.add(new BasicNameValuePair("fileContents", fileCon));
    DefaultHttpClient client = new MyHttpClient(getApplicationContext());
try {   
    HttpPost request = new HttpPost(url);
    UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
    request.setEntity(formEntity);
    HttpResponse resp = client.execute(request);

    //Get data
     HttpEntity entity = resp.getEntity();
     InputStream is = entity.getContent();
     String data = convertStreamToString(is);
     browser=(WebView)findViewById(R.id.myWebView);
     browser.loadData(data,"text/html", "UTF-8");
} catch(Exception e){
    e.printStackTrace();
}

InputStream String:

private static String convertStreamToString(InputStream is) {

BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();

String line = null;
try {
    while ((line = reader.readLine()) != null) {
        sb.append((line + "\n"));
    }
} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        is.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
return sb.toString();

}

0

All Articles