我想將文本從網頁放置到Android 3.0的文本視圖中。我有這樣的代碼:將網頁文本放入文本視圖(Android)
public class Biografie extends Activity {
private TextView outtext;
private String HTML;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_biografie);
outtext= (TextView) findViewById(R.id.textview1);
try {
getHTML();
} catch (Exception e) {
e.printStackTrace();
}
outtext.setText("" + HTML);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.biografie, menu);
return true;
}
private void getHTML() throws ClientProtocolException, IOException
{
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet("http://artistone.appone.nl/api/biografie.php?dataid=998"); //URL!
HttpResponse response = httpClient.execute(httpGet, localContext);
String result = "";
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = null;
while ((line = reader.readLine()) != null) {
result += line + "\n";
HTML = result;
}
}
}
我的TextView返回「null」而不是頁面中的文本。請幫我解決這個問題。謝謝。
你得到一個異常來自getHTML?以及您正在測試的目標設備是什麼? – 2013-02-26 17:02:59
我在Android 4.0.2平板電腦上測試 – 2013-02-26 17:04:17