2011-03-17 72 views
0

我已經把這個Android程序寫成了新聞源...它工作得很好,直到 - 當前 - 關於focal.ie的文章。因此,前6個故事加載正常,但它沒有加載其餘的和Im破壞我的頭試圖找出原因。任何幫助,將不勝感激。這是代碼。謝謝Android程序錯誤

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

logcat中有錯誤嗎? – 2011-03-17 20:16:16

回答

0

我的猜測是缺少閉合錨定標記導致問題。我猜的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>";