我正在尝试完成一些非常简单的事情,但是我没有找到很好的文档。我有一个webView,我需要在其中加载需要POST数据的页面。似乎是一个简单的过程,但是我找不到在webView中显示结果的方法。

这个过程应该很简单:

查询(带有POST数据)-> Web服务器-> HTML响应-> WebView。

我可以使用DefaultHttpClient提交数据,但是无法在WebView中显示。

有什么建议?

非常感谢


private static final String URL_STRING = "http://www.yoursite.com/postreceiver";

    public void postData() throws IOException, ClientProtocolException {

         List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
         nameValuePairs.add(new BasicNameValuePair("foo", "12345"));
         nameValuePairs.add(new BasicNameValuePair("bar", "23456"));

         HttpClient httpclient = new DefaultHttpClient();
         HttpPost httppost = new HttpPost(URL_STRING);
         httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

         HttpResponse response = httpclient.execute(httppost);
         String data = new BasicResponseHandler().handleResponse(response);
         mWebView.loadData(data, "text/html", "utf-8");
    }

最佳答案

尝试这个:

private static final String URL_STRING = "http://www.yoursite.com/postreceiver";

public void postData() throws IOException, ClientProtocolException {

     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
     nameValuePairs.add(new BasicNameValuePair("foo", "12345"));
     nameValuePairs.add(new BasicNameValuePair("bar", "23456"));

     HttpClient httpclient = new DefaultHttpClient();
     HttpPost httppost = new HttpPost(URL_STRING);
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

     HttpResponse response = httpclient.execute(httppost);

}

我建议将其作为AsyncTask的一部分,然后再更新WebView

08-18 18:08
查看更多