0
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import javax.xml.xpath.*;

import com.example.androidtablayout.R;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class PhotosActivity extends Activity  {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.photos_layout);

        String htmlCode = "";

        try {
        URL url = new URL("http://www.example.com/");
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

        String inputLine;

        while ((inputLine = in.readLine()) != null)
            htmlCode += inputLine;

        in.close();
        } catch (Exception e) {
           System.out.print("Error: " + e.getMessage());
        }

             XPath xpath = XPathFactory.newInstance().newXPath();

            try {
                String str = (String) xpath.evaluate("//*[@class='postunit'][1]",htmlCode, XPathConstants.STRING);
                 TextView t = new TextView(this);
                 t=(TextView)findViewById(R.id.textView3); 
                 t.setText(str);
            } catch (XPathExpressionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }



    }
}

おい。Web サイトから html ソースを取得したいのですが、それを実行しました。XPathでスライスしたいのですが、レイアウト画面に何も表示されません。

try catch から TextView Code を置き換えようとしましたが、機能せず、このエラーが返されました (str は使用されません)。

この問題を解決するにはどうすればよいですか?

4

0 に答える 0