2013-05-28 76 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。我认为这会帮助你