5

In my android app, I am posting data to a https servlet URL from a WebView as shown below

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

The URL in the above code is a servlet URL for which I have to post some data and from there I am redirecting to some other URL.

The above code worked fine when the servlet URL is just HTTP. But when changed to HTTPS, it is showing a blank screen.

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

I removed the above code from onCreate() method 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();
}

Now I am able to post the data and from there it is redirecting also. But still I am seeing a blank screen.

Is it because I don't have either loadUrl or a postUrl I am seeing a blank screen?

Or should I put the above code in any method of WebView?

HitOdessit
  • 7,198
  • 4
  • 36
  • 59
Sreeram
  • 3,160
  • 6
  • 33
  • 44

1 Answers1

0

Try this

    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 to String method:

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();

}

jzafrilla
  • 1,416
  • 3
  • 18
  • 41