简体   繁体   中英

webview doesn't load my html

I am doing an Android application that takes the HTML code of a website. I got a Webview that should load this HTML but when I run my program, I don't see my "HTML".

Here is my code:

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

}

If I put webview.loadData("<h1> Test </h1>", "text/html; charset=UTF-8", null); it works If I return s2 in getCode(), it doesn't work. And naturally, if I return s1, it doesn't work.

I discovered this part crashes the app. Do you know why ?

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

TRy like this..

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

Add the xml webview..

<WebView android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:id="@+id/mainWebView">
 </WebView>

you need just to load your url in your WebView without using the class Web that you have create :

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");
    }

And if you want to load some HTML Code into your WebView follow this :

1) create an HTML File into assets/ folder in your project : example.html

2) and then call load it like this :

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

In your case , your class Web is extending an Activity , and in Android you can't instanciate an Activity class , you should override the method onCreate() and then put your code into it .

EDIT : after splitting your HTML code, you can load it into your WebView like this :

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");
}

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

}

In your case , your class Web is extending an Activity , and in Android you can't instantiate an Activity class, you should override the method onCreate() .

So please remove 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;
   }
 }

As Houcine illustrate the real example tht'z the proper way to indulge webView.loadData()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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