我正在做一个获取网站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();
}
答案 0 :(得分: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>
答案 1 :(得分: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
,请按照以下步骤操作:
1)在项目的assets/
文件夹中创建一个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;
}
}
答案 2 :(得分: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()