2013-10-06 127 views
0

我試圖執行JSOUP查詢,但是出現錯誤,指出「doc無法解析」和「doc無法解析爲變量」我知道我需要在使用它之前調用doc我只是不確定如何去做 - 這是我第一次用JSOUP構建解析器 - 我確定它非常簡單 - 我只需要一個快速指針。JSOUP - doc無法解析並且doc無法解析爲變量

public class MainActivity extends Activity { 

    TextView tv; 
    String url = "http://microsoft.com"; 
String tr; 

    @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); 
     } 
    } 
} 
+0

你永遠不會創建對象文檔,這就是爲什麼。您可以將它創建爲一個字段並在該方法中進行初始化,或者將其創建爲局部變量。除非你打算在方法之外使用它,否則後者可能最適合你。 –

+0

我打算在這種方法之外使用它......我如何將它實例化爲一個字段? (如果你希望你可以發表你的回答作爲答案,我會接受它) – HelloMojo

回答

0

您正在嘗試使用尚未聲明的變量。正如你已經與

TextView tv; 

做,你將不得不聲明變量文檔。

Document doc; 

不要忘了導入下列包org.jsoup.nodes.Document