2011-10-18 117 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和权限互联网,你会看到。 –