2015-02-24 81 views
0

使用此代碼,應用程序應該提取網站div的文本並將其顯示在屏幕上,但這不會發生,並且[並且在Logcat中沒有顯示任何錯誤,我是什麼做錯了?使用Jsoup從div中提取文本

package com.androidbegin.jsouptutorial; 

import java.io.IOException; 
import java.io.InputStream; 

import org.jsoup.Jsoup; 
import org.jsoup.nodes.Document; 
import org.jsoup.select.Elements; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.app.Activity; 
import android.app.ProgressDialog; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 

import android.widget.TextView; 

public class MainActivity extends Activity { 
    TextView txtdesc; 

    // URL Address 
    String url = "http://uat.sophiejuliete.com.br/tendencias/"; 
    ProgressDialog mProgressDialog; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // Locate the Buttons in activity_main.xml 
     Button titlebutton = (Button) findViewById(R.id.titlebutton); 
     txtdesc = (TextView) findViewById(R.id.desctxt); 


     // Capture button click 
     titlebutton.setOnClickListener(new OnClickListener() { 
      public void onClick(View arg0) { 
       // Execute Title AsyncTask 
       new Title().execute(); 
      } 
     }); 

    } 


    private class Title extends AsyncTask<Void, Void, String> { 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      mProgressDialog = new ProgressDialog(MainActivity.this); 
      mProgressDialog.setTitle("Android Basic JSoup Tutorial"); 
      mProgressDialog.setMessage("Loading..."); 
      mProgressDialog.setIndeterminate(false); 
      mProgressDialog.show(); 
     } 

     @Override 
     protected String doInBackground(Void... params) { 
      String desc = null; 
      try { 
       // Connect to the web site 
       Document document = Jsoup.connect(url).get(); 
       // Using Elements to get the Meta data 
       Elements description = document.select("div[class=postWrapper]"); 
       // Locate the content attribute 
       desc = description.text(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      return desc; 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      // Set description into TextView 
      txtdesc.setText(result); 
      mProgressDialog.dismiss(); 
     } 

    } 



} 

這是網站,你需要分析結構:

<div class="postWrapper" id="post162"> 
     <div class="postTitle"> 


      <h2> 
       <a href="http://uat.sophiejuliete.com.br/tendencias/agarradinhos-as-orelhas/"> 
        Agarradinhos às orelhas    </a> 
      </h2> 

      <div class="fb-custom-share" data-url="http://uat.sophiejuliete.com.br/tendencias/agarradinhos-as-orelhas/"> 
       Compartilhar 
      </div> 

      <div class="date"> 
       26 de janeiro de 2015   </div> 

     </div> 

     <div class="postContent"><p>Agarradinhos às orelhas, os solitários e brincos curtos são ideais tanto para o dia como para a noite.</p> 
<p>E melhor ainda ficam bem em qualquer formato de rosto.</p> 
<p>Basta apenas escolher o modelo conforme a ocasião que você vai utilizar.</p> 
<p>&nbsp;</p> 
<p><a href="http://sophiejuliete.com.br/shop/brincos.html"><img style="display: block; margin-left: auto; margin-right: auto;" src="http://uat.sophiejuliete.com.br/media/wysiwyg/Agarradinhos_s_orelhas.jpg" alt=""></a></p></div> 
    </div> 

回答

0

嘗試

desc = description.text(); 

,而不是

desc = description.attr("postContent"); 

例子:

public static void main(String[] args) throws Exception { 
    String url = "http://uat.sophiejuliete.com.br/tendencias/"; 
    Document document = Jsoup.connect(url).timeout(10000).get(); 
    // Using Elements to get the Meta data 
    Elements description = document.select("div[class=postContent]"); 
    // Locate the content attribute 
    String desc = description.text(); 
    System.out.println(desc); 
    // prints out "Agarradinhos às orelhas, os solitários e brincos..." 
} 

UPDATE

由於JSoup部分是固定的,你可能有異步任務的一些問題。嘗試使用String作爲結果類型,像這樣

private class Title extends AsyncTask<Void, Void, String> { 

    ... 

    @Override 
    protected String doInBackground(Void... params) { 
     String desc = null; 
     try { 
      // Connect to the web site 
      Document document = Jsoup.connect(url).get(); 
      // Using Elements to get the Meta data 
      Elements description = document.select("div[class=postContent]"); 
      // Locate the content attribute 
      desc = description.text(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return desc; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     // Set description into TextView 
     TextView txtdesc = (TextView) findViewById(R.id.desctxt); 
     txtdesc.setText(result); 
     mProgressDialog.dismiss(); 
    } 

} 

更新2

聲明txtdesc全球範圍內,在MainActivity

TextView txtdesc; 

onCreate()

txtdesc = (TextView) findViewById(R.id.desctxt); 
初始化0

並刪除onPostExecute()聲明,所以只有txtdesc.setText(result);

@Override 
protected void onPostExecute(String result) { 
    // Set description into TextView 
    txtdesc.setText(result); 
    mProgressDialog.dismiss(); 
} 
+0

我改變了它 DESC = description.attr( 「postContent」);這就是爲什麼, desc = description.text(); 但它沒有奏效! – 2015-02-24 13:51:58

+0

適用於我,也許你有你的代碼的其他問題,但JSoup部分是好的。我已經添加了一個小例子,你可以檢查它是否適用於你? – 2015-02-24 13:55:28

+0

我是計劃的新手,如何以及在哪裏適合這段代碼片段,您可以將其放在我的發佈代碼上嗎? – 2015-02-24 13:59:41