Java 到安卓?

发布于 2024-12-16 13:12:27 字数 408 浏览 2 评论 0原文

我如何将 Java 中的这段代码传递到 Android,它在 Eclipse Java 项目中运行良好。

public class GetCode {
public static void main(String[] args) throws IOException {
    Document doc = Jsoup.connect("http://www.sapo.pt/").get();
    Elements divs = doc.select("div");
    for (Element div : divs)
        System.out.println(div.text());
    }
}       

谁能帮我解决这个问题吗?我在 AndroidManifest.xml 中有互联网访问权限。

How can i pass this code in Java to Android, it works fine in Eclipse Java Project.

public class GetCode {
public static void main(String[] args) throws IOException {
    Document doc = Jsoup.connect("http://www.sapo.pt/").get();
    Elements divs = doc.select("div");
    for (Element div : divs)
        System.out.println(div.text());
    }
}       

Can anyone help me with this? I have Internet acess in AndroidManifest.xml.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愛放△進行李 2024-12-23 13:12:27

首先,Android应用程序必须扩展Activity。

所以你的代码应该是这样

public class GetCode extends Activity {

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Document doc = Jsoup.connect("http://www.sapo.pt/").get();
    Elements divs = doc.select("div");
    for (Element div : divs)
        //there are many ways to show data. This is one show it as a pop up message
        Toast toast = Toast.maketext(this, div.text(), Toast.Lenght_Short);
        toast.show;
        //this prints it in the logcat
        debug.i("my app", div.text);
        //Alert dialogues work too
    }
} 

,并且你不能向它传递参数。如果您想在活动之间传递数据,请使用捆绑包。您必须在 android 项目中编写它,因为它引用了 eclipse 的 android 插件在编译时创建的布局和生成的代码。

如果 div.text() 是 html 文本,您可以使用

 WebView wv = new WebView(this);
 wv.loadData(div.text() , "text/html", "utf-8");

它,然后像视图一样使用它,例如在您的提醒对话框广告中,使用

   ad.setView(wv);

First of all the android applications must extend Activity.

So your code should be

public class GetCode extends Activity {

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Document doc = Jsoup.connect("http://www.sapo.pt/").get();
    Elements divs = doc.select("div");
    for (Element div : divs)
        //there are many ways to show data. This is one show it as a pop up message
        Toast toast = Toast.maketext(this, div.text(), Toast.Lenght_Short);
        toast.show;
        //this prints it in the logcat
        debug.i("my app", div.text);
        //Alert dialogues work too
    }
} 

and you can't pass arguments to it. If you want to pass data between activities, use bundles. You must write it within an android project, since it references to layouts and generated code that is created at compile time by eclipse's android plugin.

If div.text() is a html text, you can use

 WebView wv = new WebView(this);
 wv.loadData(div.text() , "text/html", "utf-8");

and then use it like a view, for example in your Alert Dialog ad, use

   ad.setView(wv);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文