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();
}
}
}
嘿。我想从网站获取 html 源代码,我做到了。当我想使用 XPath 进行切片时,我的布局屏幕上没有任何内容。
我尝试用 try catch 替换 TextView 代码,但它不起作用并返回此错误(未使用 str)
我该如何解决这个问题?