对webview的HTTP POST响应不能正常工作

时间:2011-10-18 12:31:10

标签: android webview http-post

我已经尝试了这个示例,它可以使用示例网址(http://search.yahoo.com/search),但是当我为“http://www.ilias.de/docu/login.php?target=”执行此操作时& client_id = docu“,我无法正确查看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

1 个答案:

答案 0 :(得分:2)

我认为你必须使用
loadDataWithBaseURl("http://www.ilias.de/docu/",webData,"text/html","UTF-8","about:blank");
而不是 webView.loadData(webData, "text/html", "UTF-8");

根据reference,“简单”loadData方法不会从网络加载内容。

@@@@@@@ EDIT @@@@@@@

为了像在本机浏览器中一样查看页面,请先删除以下行:

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

这是因为,要么我不理解API文档,要么loadDataWithBaseUrl不会对字符进行解码。

使用缩放级别(你可以通过编程方式)

此致 enter image description here