0

我有一个全屏网络视图,其中:
1)我将 xml 文件发送到 php 脚本
2)php 脚本使用 javascript 构建页面并回显整个代码
3)webview 加载(应该加载)结果。

从脚本读取结果,我可以看到整个 html 代码,但 web 视图没有加载它。这是带有结果页面的 logcat:http: //pastebin.com/yjKK10SY

这就是我处理请求/响应客户端的方式:

@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;
    }

}

}

活动开始时,屏幕保持空白,我在哪里做错了?

4

3 回答 3

2

尝试这个

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

另外,我宁愿建议您使用 AsyncTask。将其创建为内部类。在 AsyncTask 的 doInBackground() 方法中执行所有 http 操作。然后在 AsyncTask 的 onPostExecute() 方法中调用 webView.loadData(...)。

当然,您必须将您的 Webview 实例声明为外部类中的实例变量(而不是在 onCreate 中定义它),以便使其在内部 AsyncTask 类中可访问。

于 2013-10-04T15:19:25.127 回答
1

我会推荐loadDataWithBaseURL

webView.loadDataWithBaseURL(null, result, "text/html", "utf-8", "about:blank");
于 2013-10-04T14:25:08.880 回答
0

您在后台线程中访问网络,因此您需要确保输出到 UI 线程:

runOnUiThread(new Runnable() {
    public void run() {
        mWebView.loadData(res, "text/html; charset=UTF-8", null);
    }
});
于 2013-11-04T18:47:35.443 回答