安卓程序错误

发布于 2024-10-23 18:20:48 字数 3722 浏览 0 评论 0原文

我已经编写了这个 Android 程序作为新闻提要...它运行良好,直到 - 目前 - 关于 focus.ie 的文章。所以前 6 个故事加载得很好,但随后就无法加载其余的故事,我绞尽脑汁试图找出原因。任何帮助将不胜感激。这是代码。谢谢

package com.news;



import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class NewsActivity extends Activity {
    WebView mWebView;
    String test2 = "<html><body><table border=0 width=10 height=10>";
    Document docs;
    Document writing;

    String text(String link)
{
    String full ="<html><body><table border=0 cellpadding=2 cellspacing=2><tr><td>";;
    try {
         writing = Jsoup.connect(link).get();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Element heading = writing.select("h2").first();
    Elements classname = writing.getElementsByClass("news");
    Elements items = classname.select("p");

    full = full + heading.text() + "<br>" + items.get(0).text() + "</td></tr>";
    Elements imgs2 = writing.select("img[src$=.jpg]");
    String picture = imgs2.get(1).absUrl("src");
    String newImg = "<img src=\"" + picture + "\"/ width =100 >";
    full = full + "<tr><td>" + newImg + "</td></tr>";
    full = full + "<tr><td>";

    for (int i = 1; i< items.size(); i++)
    {
        full = full + items.get(i).text();
    }

    full = full + "</td></tr></table></body></html>";
    return full;
}
public void main(String... args) 
{
    try 
    {
         docs = Jsoup.connect("http://www.dcu.ie/news/index.shtml").get();
    } 
    catch (IOException e) 
    {
        e.printStackTrace();
    }

    Elements imgs = docs.select("img[src$=.jpg]");
    Elements txt = docs.select("h2");

    Elements article = docs.getElementsByClass("date");
    Elements links = article.select("a[href]");

    for (int i = 1; i < imgs.size(); i++){
        String url = imgs.get(i).absUrl("src");
        String temp = links.get(i-1).absUrl("href");

        String temp2 = "<a href=\"" + temp + "\"/>";
        String newImg = temp2 + "<img src=\"" + url + "\"/ width =100 >";
        test2 = test2 + "<tr>";

        test2 = test2 + "<td>";
        test2 = test2 + " " + newImg + " ";
        test2 = test2 + "</td>";
        test2 = test2 + "<td>";
        test2 = test2 + " " + txt.get(i-1).text() + " " + temp2;
        test2 = test2 + "</td>";
        test2 = test2 + "</tr>";

        }
    test2 = test2 + "</table>";
    test2 = test2 + "</html></body>";

}

public void onCreate(Bundle savedInstanceState) {
    main();

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mWebView = (WebView) findViewById(R.id.webview);
    mWebView.setWebViewClient(new NewsClient());
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setDomStorageEnabled(true);

    mWebView.loadData(test2, "text/html", "utf-8");

}


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
        mWebView.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

private class NewsClient extends WebViewClient {
    public boolean shouldOverrideUrlLoading(WebView view, String url) {

        String newUrl = text(url);
        view.loadData(newUrl, "text/html", "utf-8");
        return true;
    }
}
}

I've written this Android program as a news feed...it works fine up until - currently - the article about focal.ie. So the first 6 stories load fine but then it doesn't load the rest and Im wrecking my head trying to figure out why. Any help would be appreciated. Here's the code. Thanks

package com.news;



import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class NewsActivity extends Activity {
    WebView mWebView;
    String test2 = "<html><body><table border=0 width=10 height=10>";
    Document docs;
    Document writing;

    String text(String link)
{
    String full ="<html><body><table border=0 cellpadding=2 cellspacing=2><tr><td>";;
    try {
         writing = Jsoup.connect(link).get();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Element heading = writing.select("h2").first();
    Elements classname = writing.getElementsByClass("news");
    Elements items = classname.select("p");

    full = full + heading.text() + "<br>" + items.get(0).text() + "</td></tr>";
    Elements imgs2 = writing.select("img[src$=.jpg]");
    String picture = imgs2.get(1).absUrl("src");
    String newImg = "<img src=\"" + picture + "\"/ width =100 >";
    full = full + "<tr><td>" + newImg + "</td></tr>";
    full = full + "<tr><td>";

    for (int i = 1; i< items.size(); i++)
    {
        full = full + items.get(i).text();
    }

    full = full + "</td></tr></table></body></html>";
    return full;
}
public void main(String... args) 
{
    try 
    {
         docs = Jsoup.connect("http://www.dcu.ie/news/index.shtml").get();
    } 
    catch (IOException e) 
    {
        e.printStackTrace();
    }

    Elements imgs = docs.select("img[src$=.jpg]");
    Elements txt = docs.select("h2");

    Elements article = docs.getElementsByClass("date");
    Elements links = article.select("a[href]");

    for (int i = 1; i < imgs.size(); i++){
        String url = imgs.get(i).absUrl("src");
        String temp = links.get(i-1).absUrl("href");

        String temp2 = "<a href=\"" + temp + "\"/>";
        String newImg = temp2 + "<img src=\"" + url + "\"/ width =100 >";
        test2 = test2 + "<tr>";

        test2 = test2 + "<td>";
        test2 = test2 + " " + newImg + " ";
        test2 = test2 + "</td>";
        test2 = test2 + "<td>";
        test2 = test2 + " " + txt.get(i-1).text() + " " + temp2;
        test2 = test2 + "</td>";
        test2 = test2 + "</tr>";

        }
    test2 = test2 + "</table>";
    test2 = test2 + "</html></body>";

}

public void onCreate(Bundle savedInstanceState) {
    main();

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mWebView = (WebView) findViewById(R.id.webview);
    mWebView.setWebViewClient(new NewsClient());
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setDomStorageEnabled(true);

    mWebView.loadData(test2, "text/html", "utf-8");

}


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
        mWebView.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

private class NewsClient extends WebViewClient {
    public boolean shouldOverrideUrlLoading(WebView view, String url) {

        String newUrl = text(url);
        view.loadData(newUrl, "text/html", "utf-8");
        return true;
    }
}
}

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

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

发布评论

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

评论(1

季末如歌 2024-10-30 18:20:48

我的猜测是缺少的结束锚标签导致了问题。我猜测 HTML 解析器不喜欢空锚标记:

String temp2 = "<a href=\"" + temp + "\"/>";
String newImg = temp2 + "<img src=\"" + url + "\"/ width =100 >";
test2 = test2 + "<tr>";

应该是:

String temp2 = "<a href=\"" + temp + "\" >";
String newImg = temp2 + "<img src=\"" + url + "\"/ width =100 >" + "</a>";
test2 = test2 + "<tr>";

和:

test2 = test2 + " " + txt.get(i-1).text() + " " + temp2;

应该是:

test2 = test2 + temp2+ " " + txt.get(i-1).text() + " </a>";

My guess is that the missing closing anchor tags are causing problems. I'm guessing the HTML parser doesn't like empty anchor tags:

String temp2 = "<a href=\"" + temp + "\"/>";
String newImg = temp2 + "<img src=\"" + url + "\"/ width =100 >";
test2 = test2 + "<tr>";

should be:

String temp2 = "<a href=\"" + temp + "\" >";
String newImg = temp2 + "<img src=\"" + url + "\"/ width =100 >" + "</a>";
test2 = test2 + "<tr>";

and:

test2 = test2 + " " + txt.get(i-1).text() + " " + temp2;

should be:

test2 = test2 + temp2+ " " + txt.get(i-1).text() + " </a>";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文