简体   繁体   English

html代码中的webView.loadData不起作用

[英]webView.loadData from html code doesn't work

i have a full screen web view, where: 我有一个全屏的Web视图,其中:
1) i send an xml file to a php script 1)我将xml文件发送到php脚本
2) the php script build the page using javascript and echo back the whole code 2)php脚本使用javascript构建页面并回显整个代码
3) the webview load ( should load ) the result. 3)webview加载(应加载)结果。

Reading the result from the script, i can see the entire html code, but the web view doesn't load it. 从脚本读取结果,我可以看到整个html代码,但是Web视图不会加载它。 here is the logcat with the result page: http://pastebin.com/yjKK10SY 这是带有结果页面的logcat: http : //pastebin.com/yjKK10SY

and this is how i handle the request / response client side: 这就是我处理请求/响应客户端的方式:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview);

    final String url = "http://www.mysite.meh/google_api/test.php";
    String selected = getIntent().getStringExtra("com.example.simplerun.filename");
    final File file = new File("/storage/sdcard0/SimpleRun/"+selected + ".xml");
    Log.i("FILE SIZE: ", "" + file.length());

    final WebView mWebView = (WebView) findViewById(R.id.webview);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    mWebView.getSettings().setSaveFormData(true);
    mWebView.setWebViewClient(new MyWebViewClient());

    Thread thread = new Thread()
    {
        public void run()
        {

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url);

    try {
      MultipartEntity entity = new MultipartEntity();

      entity.addPart("userfile", new FileBody(file));
      httppost.setEntity(entity);
      HttpResponse response = httpclient.execute(httppost);
      String res = EntityUtils.toString(response.getEntity());
        Log.i("RESPONSE: ", res);
        //String uri = Uri.encode(res);
        mWebView.loadData(res, "text/html; charset=UTF-8", null);
    } catch (ClientProtocolException e) {
    } catch (IOException e) {
    }
        }
    };
    thread.start();

    try
    {
        thread.join();

    }
    catch(InterruptedException e)
    {
        e.printStackTrace();
    }



    /*
    Thread thread = new Thread()
    {
        public void run()
        {
            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(url);
            client.getParams().setParameter("userfile", file);

            try
            {
                HttpResponse response = client.execute(post);
                String res = EntityUtils.toString(response.getEntity());
                Log.i("RESPONSE: ", res);
                mWebView.loadData(res, "text/html", HTTP.UTF_8);

            }
            catch(ClientProtocolException e)
            {
                e.printStackTrace();
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
        }
    };
    thread.start();

    try
    {
        thread.join();
    }
    catch(InterruptedException e)
    {
        e.printStackTrace();
    }

    */

    /*
    WebView mWebView = (WebView) findViewById(R.id.webview);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    mWebView.getSettings().setSaveFormData(true);
    mWebView.setWebViewClient(new MyWebViewClient());
    */
}

private class MyWebViewClient extends WebViewClient {
    @Override
    // show the web page in webview but not in web browser
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }

}

} }

when the activity start, the screen remain blank, where i'm doing wrong? 活动开始时,屏幕保持空白,我做错了什么?

Try this 尝试这个

webView.loadData(res, "text/html", "UTF-8") instead.

Also, i'll rather suggest you to use AsyncTask. 另外,我宁愿建议您使用AsyncTask。 Create it as inner class. 将其创建为内部类。 Do all your http operations in your doInBackground() method of AsyncTask. 在AsyncTask的doInBackground()方法中执行所有http操作。 And then call webView.loadData(...) in onPostExecute() method of AsyncTask. 然后在AsyncTask的onPostExecute()方法中调用webView.loadData(...)。

Ofcourse u'll have to declare your Webview instance as an instance variable in your outer-class (instead of defining it inside onCreate) so as to make it accessible in the inner AsyncTask class. 当然,您必须将您的Webview实例声明为您的外部类中的实例变量(而不是在onCreate中定义它),以便使其在内部AsyncTask类中可访问。

我会建议loadDataWithBaseURL

webView.loadDataWithBaseURL(null, result, "text/html", "utf-8", "about:blank");

You access the web in a background thread, so you need to make sure you output to the UI thread: 您在后台线程中访问Web,因此需要确保输出到UI线程:

runOnUiThread(new Runnable() {
    public void run() {
        mWebView.loadData(res, "text/html; charset=UTF-8", null);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM