2015-05-10 1 views
0

Я хочу получать данные с сайта на свое приложение для Android, поэтому я использовал jsoup.Ошибка в подключении к проекту jsoup на сайте (метод connect (String) не определен для типа Jsoup)

Document document = Jsoup.connect(url).get(); 

ошибка в этой строке моих project.i использовать выше линии три раза, как мое требование, но все три раза, когда это выше линией является использование и все три линии показывает сообщение об ошибке. Помощь, как удалить эту ошибку ..

если кто-нибудь знает какой-либо другой простой метод/способ получения (выборки) данных из динамических данных веб-сайта для андроид приложение любезно также отметить, что способ ..

public class Jsoup extends Activity{ 

    // URL Address 
    String url = "http://www.vogella.com/tutorials/Android/article.html"; 
    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); 
     Button descbutton = (Button) findViewById(R.id.descbutton); 
     Button logobutton = (Button) findViewById(R.id.logobutton); 

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

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

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

    } 

    // Title AsyncTask 
    private class Title extends AsyncTask<Void, Void, Void> { 
     String title; 

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

     @Override 
     protected Void doInBackground(Void... params) { 
      try { 
       // Connect to the web site 
       Document document = Jsoup.connect(url).get(); 
       // Get the html document title 
       title = document.title(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      // Set title into TextView 
      TextView txttitle = (TextView) findViewById(R.id.titletxt); 
      txttitle.setText(title); 
      mProgressDialog.dismiss(); 
     } 
    } 

    // Description AsyncTask 
    private class Description extends AsyncTask<Void, Void, Void> { 
     String desc; 

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

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

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

    // Logo AsyncTask 
    private class Logo extends AsyncTask<Void, Void, Void> { 
     Bitmap bitmap; 

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

     @Override 
     protected Void doInBackground(Void... params) { 

      try { 
       // Connect to the web site 
       Document document = Jsoup.connect(url).get(); 
       // Using Elements to get the class data 
       Elements img = document.select("a[class=brand brand-image] img[src]"); 
       // Locate the src attribute 
       String imgSrc = img.attr("src"); 
       // Download image from URL 
       InputStream input = new java.net.URL(imgSrc).openStream(); 
       // Decode Bitmap 
       bitmap = BitmapFactory.decodeStream(input); 

      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      // Set downloaded image into ImageView 
      ImageView logoimg = (ImageView) findViewById(R.id.logo); 
      logoimg.setImageBitmap(bitmap); 
      mProgressDialog.dismiss(); 
     } 
    } 

}

+0

Можете ли вы отправить сообщение об ошибке вы получаете? – Suspended

+0

Метод connect (String) не определен для типа Jsoup – user3719634

+0

Не размещайте свои ошибки как комментарии, так как они должны быть частью вашего вопроса. Используйте опцию [edit] и добавьте информацию об ошибке в свой пост. – Pshemo

ответ

1

Вы не должны называть свой класс так же, как уже существующие классы, потому что компилятор будет подключать каждый вызов упрощенного имени к текущему классу, а не к классу из импорта. Например

class String { 
    public static void main(String[] args) { 
     System.out.println(String.valueOf("1")); 
    } 
} 

не будет компилировать, потому что String.valueOf не будет пытаться вызвать valueOf из java.lang.String но из вашего класса, и так как нет такого метода есть вы видите ошибку о том, что такой метод не определен.

Так изменить имя вашего класса

public class Jsoup extends Activity{ 
    ...{ 
     Document document = Jsoup.connect(url).get(); 
    } 
} 

к чему-то более, как

public class JsoupActivity extends Activity{ 
//   ^^^^^^^^^^^^^ 
    ...{ 
     Document document = Jsoup.connect(url).get(); 
    } 
} 
Смежные вопросы