我已經搜遍了互聯網,並開發了一些代碼,我認爲這些代碼能夠以可讀的形式獲取網站的html
。但是,我仍然無法完成這項工作。這裏是我的代碼:Android:檢索網站的HTML - 不工作
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DownloadTask task = new DownloadTask();
task.execute("http://www.cnn.com");
}
private class DownloadTask extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... urls) {
HttpResponse response = null;
HttpGet httpGet = null;
HttpClient mHttpClient = null;
String s = "";
try {
if(mHttpClient == null){
mHttpClient = new DefaultHttpClient();
}
httpGet = new HttpGet(urls[0]);
response = mHttpClient.execute(httpGet);
s = EntityUtils.toString(response.getEntity(), "UTF-8");
} catch (IOException e) {
e.printStackTrace();
}
return s;
}
@Override
protected void onPostExecute(String result){
final TextView textview1 = (TextView) findViewById(R.id.headline);
textview1.setText(result);
}
}
}
當我嘗試這一點,在啓動應用程序崩潰,與LogCat
如下圖所示:
07-26 16:13:42.561: W/dalvikvm(23584): threadid=12: thread exiting with uncaught exception (group=0x416dfda0)
07-26 16:13:42.561: E/AndroidRuntime(23584): FATAL EXCEPTION: AsyncTask #1
07-26 16:13:42.561: E/AndroidRuntime(23584): Process: com.app.firstapp, PID: 23584
07-26 16:13:42.561: E/AndroidRuntime(23584): java.lang.RuntimeException: An error occured while executing doInBackground()
我不知道爲什麼會這樣,沒有人知道我是怎麼可以修改我的代碼,使其工作?提前致謝。
發佈堆棧跟蹤的其餘部分。並告訴我們在崩潰中命名的doInBackground行是什麼 –
@GabeSechan是與LogCat相同的堆棧跟蹤? – user3866661