2016-04-03 18 views
0

Android Studio'da yerel bir HTML sayfasından verileri ayıklamıyorum ve "text_view" ifadesini görüntüleyen doğru bilgi yerine. Kazıdığım verileri nasıl görüntüleyeceğimi bilen var mı? Aşağıda ana kod parçasıdır. Aşağıda Android - Uygulamam için Yanlış Bilgileri Görüntüleme

public class MainActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
/* try { 

     File input = new File("C:\\Users\\user\\Desktop\\Mobile Newest\\JSoup\\app\\src\\main\\assets\\filename.html"); 
     Document doc = Jsoup.parse(input, "UTF-8"); 
     Elements tableElements = doc.select("td"); 
     TextView textView = (TextView)findViewById(R.id.text_view); 
     for (Element td : tableElements) { 
      textView.setText(td.text()); 
      System.out.println(td.text()); 
     } 

    } catch (IOException e) { 
     e.printStackTrace(); 
    }*/ 
    try { 
     StringBuilder buf=new StringBuilder(); 
     InputStreamReader inputStream = new InputStreamReader(getAssets().open("filename.html")); 
     BufferedReader bufferedReader = new BufferedReader(inputStream); 
     String str; 
     while ((str=bufferedReader.readLine()) != null) { 
      buf.append(str); 
     } 
     Document doc = Jsoup.parse(buf.toString()); 
     //other code parts goes next 
     Elements tableElements = doc.select("td"); 
     TextView textView = (TextView)findViewById(R.id.text_view); 
     for (Element td : tableElements) { 
      textView.setText(td.text()); 
      System.out.println(td.text()); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

content_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" 
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" 
app:layout_behavior="@string/appbar_scrolling_view_behavior" 
tools:showIn="@layout/activity_main" tools:context=".MainActivity"> 

<TextView android:text="text_view1" 
    android:id="@+id/text_view" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

cevap

0

Bunu yolu C ulaşamadığı düşünüyorum: \ Users \ Desktop \ Mobile \ kullanıcı Yeni \ JSoup \ app \ src \ ana Yerel cihazınızda \ devices \ filename.html android cihazdan. Kodu ayıklamaya çalıştın mı? Sanırım IOException'ınızı kovmuş olmalısınız. Varlık dosyanızı bu şekilde okumayı deneyebilirsiniz.

try { 
     StringBuilder buf=new StringBuilder(); 
     InputStreamReader inputStream = new InputStreamReader(getAssets().open("filename.html")); 
     BufferedReader bufferedReader = new BufferedReader(inputStream); 
     String str; 
     while ((str=bufferedReader.readLine()) != null) { 
      buf.append(str); 
     } 
     Document doc = Jsoup.parse(buf.toString()); 
     //other code parts goes next 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
+0

diğer kod parçaları? Elements tableElements = doc.select ("td"); TextView textView = (TextView) findViewById (R.id.text_view); (Element td: tableElements) için { textView.setText (td.text()); System.out.println (td.text()); } Bu mu demek istediniz? – DW41

+0

Ana kodumu – DW41

+0

üzerinde güncelledim Bu sorun dosya yolundaydı, bu nedenle dosyayı okumayacağınız ve metin görünümünüz ayarlanmayacak. Başka bir şey sadece son masa elemanı ayarlanacak. Ve System.out.println, Android'de çalışmaz, Logcat'te ayrıştırılmış öğelerinizi görmek için Log.d kullanın. – thekekc

İlgili konular