2013-05-28 74 views
0

嗨,我需要幫助,我嘗試了一個教程(http://xjaphx.wordpress.com/2012/02/04/android-xml-adventure-parsing-html-using-jsoup/)。這是關於jsoup,但我不能得到它的工作,我只得到錯誤和東西洛特。 這裏是我的代碼:我無法讓android jsoup工作

package com.sigustgebran.appsl; 

import android.os.Bundle; 

import org.jsoup.Jsoup; 
import org.jsoup.nodes.Document; 
import org.jsoup.select.Elements; 

import android.app.Activity; 
import android.view.Menu; 
import android.widget.TextView; 

public class MainAct extends Activity { 

//blog url 
static final String BLOG_URL = "http://xjaphx.wordpress.com/"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    //set layout view 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.act_main); 

    //process 
    try { 
     ((TextView)findViewById(R.id.tvDisplay)).setText(getBlogStats()); 
    } catch (Exception ex) { 
     ((TextView)findViewById(R.id.tvDisplay)).setText("Error"); 
    } 

} 

protected String getBlogStats() throws Exception { 
    String result = ""; 
    //get html document structure 
    Document document = Jsoup.connect(BLOG_URL).get(); 
    //select path 
    Elements nodeBlogStats = document.select("div#blog-stats ul li"); 
    //check results 
    if(nodeBlogStats.size() > 0) { 
     //get value 
     result = nodeBlogStats.get(0).text(); 
    } 

    //return 
    return result; 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

}

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    > 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textSize="30sp" 
    android:layout_gravity="center" 
    android:id="@+id/tvDisplay" 
    /> 

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.sigustgebran.appsl" 
    android:versionCode="1" 
    android:versionName="1.0" > 
<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="17" /> 

<uses-permission android:name="android.permission.INTERNET"></uses-permission> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="com.sigustgebran.appsl.MainAct" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 



</manifest> 

,我會很高興,如果有人可以幫助我。謝謝

+0

你得到的錯誤是什麼?包括你的Logcat在你的文章中將有助於更好地理解你的問題 – reidzeibel

回答

2

我現在開始工作。正如他在教程中所說的那樣,我在「外部罐子」中添加了jsoup庫,但是當我刪除它時,只是將它複製到「libs」映射中,而不是工作。

0

你必須實現AsyncTask。我認爲這會幫助你