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..
Add the xml webview..