2011-10-04 5 views
5

Nella mia app Android, sto inviando i dati a un URL https servlet da un WebView come illustrato di seguitoAndroid WebView.postUrl() mostrando schermata vuota quando si postano a HTTPS URL

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

L'URL nel codice sopra è un URL servlet per il quale devo pubblicare alcuni dati e da li sto reindirizzando verso qualche altro URL.

Il codice precedente ha funzionato correttamente quando l'URL del servlet è solo HTTP. Ma quando cambiato in HTTPS, sta mostrando uno schermo vuoto.

Ho provato la seguente soluzione per Android HTTPS problema: http://blog.antoine.li/index.php/2010/10/android-trusting-ssl-certificates/

ho rimosso il codice di cui sopra da onCreate() metodo e provato il seguente codice

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

Ora sono in grado di inviare i dati e da ci sta anche reindirizzando. Ma continuo a vedere uno schermo vuoto.

È perché non ho né loadUrl né uno postUrl Vedo uno schermo vuoto?

O dovrei inserire il codice sopra in qualsiasi metodo di WebView?

+0

Prova questa http://stackoverflow.com/a/10970539/1008278 – VenomVendor

risposta

0

Prova questo

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

}

Problemi correlati