這是我的代碼和它的工作對我來說
private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
String response = "";
for (String url : urls) {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
ArrayList<String> itemList = new ArrayList<String>();
try {
HttpResponse execute = client.execute(httpGet);
InputStream content = execute.getEntity().getContent();
BufferedReader buffer = new BufferedReader(
new InputStreamReader(content, "iso-8859-1"));
StringBuilder sb = new StringBuilder();
String s = null;
while ((s = buffer.readLine()) != null) {
itemList.add(s);
}
response = itemList.get(0);
content.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return response;
}
@Override
protected void onPostExecute(String result) {
textView.setText(Html.fromHtml(result));
}
}
public void readWebpage(View view) {
DownloadWebPageTask task = new DownloadWebPageTask();
task.execute(new String[] { "http://www.yourURL.com" });
}
readWebpage功能的onclick功能我在按鈕XML創建這樣
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/readWebpage" android:onClick="readWebpage" android:text="Load Webpage"></Button>
<TextView android:id="@+id/TextView01" android:layout_width="match_parent" android:layout_height="match_parent" android:text="Example Text"></TextView>
在我的代碼
我試圖讓第一線所以我用itemList.get(0)如果你想得到另一條線只是改變索引像itemList.get(1)或itemList.get(n)
感謝此作品! – amg 2012-08-14 06:11:38