2013-10-06 85 views
0

我試圖通過JSOUP簡單jsoup Java的HTML解析器將不會更新的TextView

http://jsoup.org/

更新使用網絡數據我的TextView出於某種原因,它似乎永遠不會從表中的數據來更新我的TextView我正試圖從中收集數據。

有什麼建議嗎?

我已經看了很多,我似乎無法發現我做錯了什麼 - 但我確定它很簡單。

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
      android:id="@+id/TextView01" 
     android:text="@string/hello_world" /> 

</RelativeLayout> 

來源:

public class MainActivity extends Activity { 

    TextView tv; 
    String url = "http://sheriff.org/apps/arrest/results.cfm?lname=ABBOTT&fname=LAUREA"; 
    String tr; 
    Document doc; 

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

     tv = (TextView) findViewById(R.id.TextView01); 
     new MyTask().execute(url); 
    } 

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

     ProgressDialog prog; 

     String title = ""; 

     @Override 
     protected void onPreExecute() { 
      prog = new ProgressDialog(MainActivity.this); 
      prog.setMessage("Loading...."); 
      prog.show(); 
     } 

     @Override 
     protected String doInBackground(String... params) { 
      try { 
       doc = Jsoup.connect(params[0]).get(); 
       Element tableElement = doc.select(".datagrid").first(); 

       Elements tableRows = tableElement.select("tr"); 
       for (Element row : tableRows) { 
        Elements cells = row.select("td"); 
        if (cells.size() > 0) { 
         System.out.println(
           cells.get(0).text() + "; " + cells.get(1).text() + "; " + cells 
             .get(2).text() + "; " + cells.get(3).text()); 
        } 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      return title; 
     } 


     @Override 
     protected void onPostExecute(String result) { 
      super.onPostExecute(result); 
      prog.dismiss(); 
      tv.setText(result); 
     } 
    } 
} 

編輯迴應的第一個答案 - 仍然沒有FIX:

public class MainActivity extends Activity { 

    TextView tv; 
    String url = "http://sheriff.org/apps/arrest/results.cfm?lname=ABBOTT&fname=LAUREA"; 
    String tr; 
    Document doc; 

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

     tv = (TextView) findViewById(R.id.TextView01); 
     new MyTask().execute(url); 
    } 

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

     ProgressDialog prog; 

     String title = ""; 

     @Override 
     protected void onPreExecute() { 
      prog = new ProgressDialog(MainActivity.this); 
      prog.setMessage("Loading...."); 
      prog.show(); 
     } 

     @Override 
     protected String doInBackground(String... params) { 
      try { 
       doc = Jsoup.connect(params[0]).get(); 
       Element tableElement = doc.select(".datagrid").first(); 

       Elements tableRows = tableElement.select("tr"); 
       for (Element row : tableRows) { 
        Elements cells = row.select("td"); 
        if (cells.size() > 0) { 
         title = cells.get(0).text() + "; " + cells.get(1).text() + "; " + 
           cells.get(2).text() + "; " + cells.get(3).text(); 
        } 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      return title; 
     } 


     @Override 
     protected void onPostExecute(String result) { 
      super.onPostExecute(result); 
      prog.dismiss(); 
      tv.setText(result); 
     } 
    } 
} 

我也試過:

public class MainActivity extends Activity { 

TextView tv; 
String url = "http://sheriff.org/apps/arrest/results.cfm?lname=ABBOTT&fname=LAUREA"; 
String tr; 
Document doc; 

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

    tv = (TextView) findViewById(R.id.TextView01); 
    new MyTask().execute(url); 
} 

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

    ProgressDialog prog; 

    String title = ""; 

    @Override 
    protected void onPreExecute() { 
     prog = new ProgressDialog(MainActivity.this); 
     prog.setMessage("Loading...."); 
     prog.show(); 
    } 

    @Override 
    protected String doInBackground(String... params) { 
     try { 
      doc = Jsoup.connect(params[0]).get(); 
      Element tableElement = doc.select(".datagrid").first(); 

      Elements tableRows = tableElement.select("tr"); 
      for (Element row : tableRows) { 
       Elements cells = row.select("td"); 
       if (cells.size() > 0) { 
        title = cells.get(0).text() + "; " + cells.get(1).text() + "; " + 
          cells.get(2).text() + "; " + cells.get(3).text(); 
       } 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return title; 
    } 


    @Override 
    protected void onPostExecute(String title) { 
     super.onPostExecute(title); 
     prog.dismiss(); 
     tv.setText(title); 
    } 
} 
} 

回答

0

1)您不會將任何內容分配給title變量。 2)請不要使用System.out.*。它用於控制檯程序。在Android中,使用Log

doInBackground()更改您的代碼:

title = cells.get(0).text() + "; " + cells.get(1).text() + "; " + 
cells.get(2).text() + "; " + cells.get(3).text()); 
+0

嘗試你的答案時,我得到一個強制關閉的錯誤...有什麼建議? – HelloMojo

+0

@ user2843378哦,哇,從來沒有想過你會刪除所有實際取回的Jsoup代碼。只需將** System.out.println **部分替換爲我所建議的部分,並將首先寫入的所有內容都保存下來。 – kaderud

+0

好的。謝謝 - 我做過了 - 但它仍然不會更新帶有表格數據的文本視圖:( – HelloMojo