4

我正在做一个采用网站 HTML 代码的 Android 应用程序。我有一个Webview应该加载这个 HTML 但是当我运行我的程序时,我没有看到我的“HTML”。

这是我的代码:

    package com.example.getdonnees;

import android.app.Activity;

import android.os.Bundle;

import android.webkit.WebView;

public class MainActivity extends Activity {

    WebView webview;

    Web web = new Web();

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        webview = (WebView)findViewById(R.id.webView1);

        webview.getSettings().setJavaScriptEnabled(true);

                try {

                    webview.loadData(web.getCode(), "text/html; charset=UTF-8", null);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

    }


}



    package com.example.getdonnees;

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.net.URL;

import java.net.URLConnection;

import android.app.Activity;

import android.os.Bundle;

import android.webkit.WebView;

public class Web extends Activity {

    WebView webview;
    String s2 = "";

    public String getCode() throws Exception{
        URL oracle = new URL("http://www.google.com/");
        URLConnection yc = oracle.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(
                                    yc.getInputStream()));
        String inputLine;
        String s1 = "";
        while ((inputLine = in.readLine()) != null)
            s1 = s1 + inputLine;
        in.close();
        System.out.println(s1);
        s2 = "<h1> test </h1>";
        return s1;
    }

}

如果我把webview.loadData("<h1> Test </h1>", "text/html; charset=UTF-8", null);它工作如果我在 getCode() 中返回 s2,它就不起作用。当然,如果我返回 s1,它就不起作用。

我发现这部分会使应用程序崩溃。你知道为什么吗 ?

            try {
            in = new BufferedReader(new InputStreamReader(
                                        yc.getInputStream()));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
4

3 回答 3

3

试试这样..

import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.webkit.WebSettings;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;

    public class AndroidMobileAppSampleActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            WebView mainWebView = (WebView) findViewById(R.id.mainWebView);

            WebSettings webSettings = mainWebView.getSettings();
            webSettings.setJavaScriptEnabled(true);

            mainWebView.setWebViewClient(new MyCustomWebViewClient());
            mainWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

            mainWebView.loadUrl("http://seasonofrejoice.blogspot.com");
        }

        private class MyCustomWebViewClient extends WebViewClient {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        }
    }

添加xml webview ..

<WebView android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:id="@+id/mainWebView">
 </WebView>
于 2013-01-10T11:26:20.520 回答
0

您只需要在WebView不使用Web您创建的类的情况下加载您的网址:

protected WebView webView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        webView = (WebView) findViewById(R.id.myWebView);
        webView.loadUrl("http://www.google.com");
    }

如果你想加载一些 HTML 代码到你的WebView下面:

assets/1)在项目的文件夹中创建一个 HTML 文件:example.html

2)然后像这样调用加载它:

webView.loadUrl("file:///android_asset/example.html");

在您的情况下,您的类Web正在扩展一个Activity,并且在 Android 中您不能实例化一个Activity,您应该重写该方法onCreate(),然后将您的代码放入其中。

编辑:拆分 HTML 代码后,您可以WebView像这样将其加载到您的文件中:

public final static void webViewLoadData(WebView web, String html) {
  StringBuilder buf = new StringBuilder(html.length());
  for (char c : html.toCharArray()) {
    switch (c) {
      case '#':  buf.append("%23"); break;
      case '%':  buf.append("%25"); break;
      case '\'': buf.append("%27"); break;
      case '?':  buf.append("%3f"); break;                
      default:
        buf.append(c); break;
      }
  }
  web.loadData(buf.toString(), "text/html", "utf-8");
}

编辑 2:

public class Web {

    String s2 = "";

    public String getCode() throws Exception{
        URL oracle = new URL("http://www.google.com/");
        URLConnection yc = oracle.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(
                                    yc.getInputStream()));
        String inputLine;
        String s1 = "";
        while ((inputLine = in.readLine()) != null)
            s1 = s1 + inputLine;
        in.close();
        System.out.println(s1);
        s2 = "<h1> test </h1>";
        return s1;
    }

}
于 2013-01-10T11:44:09.563 回答
0

在您的情况下,您的类Web正在扩展一个Activity,并且在 Android 中您无法实例化一个Activity类,您应该重写该方法onCreate()

所以请删除extends Activity out of Web class

public class Web
{
 String s2 = "";
  public String getCode() throws Exception
  {
    URL oracle = new URL("http://www.google.com/");
    URLConnection yc = oracle.openConnection();
    BufferedReader in = new BufferedReader(new InputStreamReader(
                                yc.getInputStream()));
    String inputLine;
    String s1 = "";
    while ((inputLine = in.readLine()) != null)
        s1 = s1 + inputLine;
    in.close();
    System.out.println(s1);
    s2 = "<h1> test </h1>";
    return s1;
   }
 }

正如Houcine举例说明的那样,这是放纵的正确方式webView.loadData()

于 2013-01-10T11:52:26.283 回答