2011-10-18 42 views
-1

我試過這個例子,它可以很好的與示例URL(http://search.yahoo.com/search),但當我這樣做「http://www.ilias.de/docu/ login.php?target = & client_id = docu「,我無法正確地在webview中看到響應。我的意思是,所有文字都是可見的,但圖像不是。並且鏈接不起作用。HTTP POST對webview的響應無法正常工作

我該如何解決這個問題?

public class ilias extends Activity { 
    WebView webView; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     webView = (WebView)findViewById(R.id.webview); 

     BufferedReader bufferedReader = null; 
     HttpClient httpClient = new DefaultHttpClient(); 
     HttpPost request = new HttpPost("http://www.ilias.de/docu/login.php?client_id=docu"); 
     List<NameValuePair> postParameters = new ArrayList<NameValuePair>(); 
     postParameters.add(new BasicNameValuePair("username", "stacked")); //This username 
     postParameters.add(new BasicNameValuePair("password", "overflow")); //works. 
     try { 
      UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParameters); 
      request.setEntity(entity); 

      HttpResponse response= httpClient.execute(request); 

      bufferedReader = new BufferedReader(
        new InputStreamReader(response.getEntity().getContent())); 
      StringBuffer stringBuffer = new StringBuffer(""); 
      String line = ""; 
      String LineSeparator = System.getProperty("line.separator"); 
      while ((line = bufferedReader.readLine()) != null) { 
       stringBuffer.append(line + LineSeparator); 
      } 
      bufferedReader.close(); 

      Toast.makeText(ilias.this, 
       "Finished", 
      Toast.LENGTH_LONG).show(); 

      String webData = stringBuffer.toString(); 

      webData = webData.replace("#", "%23"); 
      webData = webData.replace("%", "%25"); 
      webData = webData.replace("\\", "%27"); 
      webData = webData.replace("?", "%3f"); 

      webView.loadData(webData, 
       "text/html", 
       "UTF-8"); 
     } 
     catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      Toast.makeText(ilias.this, 
          e.toString(), 
          Toast.LENGTH_LONG).show(); 
     } 
     catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      Toast.makeText(ilias.this, 
          e.toString(), 
          Toast.LENGTH_LONG).show(); 
     } 
     finally{ 
      if (bufferedReader != null){ 
       try { 
        bufferedReader.close(); 
       } 
       catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
     } 
    } 
} 

這是我有:http://img64.imageshack.us/img64/4913/deviceyl.png,這就是我想要的:http://img200.imageshack.us/img200/8591/device1g.png

+0

嘗試發佈您的代碼爲我們調試 –

回答

2

我想,你一定要使用
loadDataWithBaseURl("http://www.ilias.de/docu/",webData,"text/html","UTF-8","about:blank");
而不是
webView.loadData(webData, "text/html", "UTF-8");

根據reference,「simple」loadData方法不會從網絡加載內容。

@@@@@@@編輯@@@@@@@

爲了看網頁就像在本地瀏覽器,首先刪除這些行:

webData = webData.replace("#", "%23"); 
    webData = webData.replace("%", "%25"); 
    webData = webData.replace("\\", "%27"); 
    webData = webData.replace("?", "%3f"); 

這是因爲要麼我不理解API文檔,要麼loadDataWithBaseUrl不會解碼字符。

和玩縮放級別(你可以做到這一點編程)

問候 enter image description here

+0

這個工作得更好,但我仍然沒有看到網頁在本地瀏覽器或PC互聯網瀏覽器。 –

+0

@AlexxPerez你能更具體嗎? – mdelolmo

+0

我沒有看到原來的頁面,但看起來有點平等。我需要把它看作原創。您可以測試代碼,用戶名和密碼的作品。只需將webview添加到main.xml和權限互聯網,你會看到。 –