使用AsyncTask从网上获取一个网页,在一个TextView中将其源代码显示出来
2016-09-26 15:15
465 查看
使用AsyncTask从网上获取一个网页,在一个TextView中将其源代码显示出来
package test.list; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.util.ArrayList; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import android.app.Activity; import android.app.ProgressDialog; import android.content.Context; import android.content.DialogInterface; import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class NetworkActivity extends Activity{ private TextView message; private Button open; private EditText url; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.network); message= (TextView) findViewById(R.id.message); url= (EditText) findViewById(R.id.url); open= (Button) findViewById(R.id.open); open.setOnClickListener(new View.OnClickListener() { public void onClick(View arg0) { connect(); } }); } private void connect() { PageTask task = new PageTask(this); task.execute(url.getText().toString()); } class PageTask extends AsyncTask<String, Integer, String> { // 可变长的输入参数,与AsyncTask.exucute()对应 ProgressDialog pdialog; public PageTask(Context context){ pdialog = new ProgressDialog(context, 0); pdialog.setButton("cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int i) { dialog.cancel(); } }); pdialog.setOnCancelListener(new DialogInterface.OnCancelListener() { public void onCancel(DialogInterface dialog) { finish(); } }); pdialog.setCancelable(true); pdialog.setMax(100); pdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); pdialog.show(); } @Override protected String doInBackground(String... params) { try{ HttpClient client = new DefaultHttpClient(); // params[0]代表连接的url HttpGet get = new HttpGet(params[0]); HttpResponse response = client.execute(get); HttpEntity entity = response.getEntity(); long length = entity.getContentLength(); InputStream is = entity.getContent(); String s = null; if(is != null) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buf = new byte[128]; int ch = -1; int count = 0; while((ch = is.read(buf)) != -1) { baos.write(buf, 0, ch); count += ch; if(length > 0) { // 如果知道响应的长度,调用publishProgress()更新进度 publishProgress((int) ((count / (float) length) * 100)); } // 让线程休眠100ms Thread.sleep(100); } s = new String(baos.toByteArray()); } // 返回结果 return s; } catch(Exception e) { e.printStackTrace(); } return null; } @Override protected void onCancelled() { super.onCancelled(); } @Override protected void onPostExecute(String result) { // 返回HTML页面的内容 message.setText(result); pdialog.dismiss(); } @Override protected void onPreExecute() { // 任务启动,可以在这里显示一个对话框,这里简单处理 message.setText(R.string.task_started); } @Override protected void onProgressUpdate(Integer... values) { // 更新进度 System.out.println(""+values[0]); message.setText(""+values[0]); pdialog.setProgress(values[0]); } } }
相关文章推荐
- 从网上获取一个网页,在一个TextView中将其源代码显示出来
- 从网上获取一个网页,在一个TextView中将其源代码显示出来
- android开发之获取一个页面的源代码用一个textview显示出来
- Unity中uniwebview+EasyAR SDK显示一个透明网页使用总结
- 使用ImageLoader获取图片,使用异步在网上请求json,然后解析,在listview中显示出来
- android LocationManager的简单使用(获取经纬度信息到textview显示)
- 爬虫 Http请求,urllib2获取数据,第三方库requests获取数据,BeautifulSoup处理数据,使用Chrome浏览器开发者工具显示检查网页源代码,json模块的dumps,loads,dump,load方法介绍
- 获取sd卡中的图片URL并显示,同时使用ImageView把选择的图片展现出来
- android 获取网页代码显示TextView中
- 解决Android TextView使用Html多个空格只显示一个空格
- 使用TextView显示网页中的图片
- Android中使用一个textview实现不同颜色以及内容分割的显示
- Android TextView与html的结合使用 一个控件显示多种文字
- Android的TextView使用Html来处理图片显示、字体样式、超链接等
- 使用ICSharpCode.TextEditor制作一个语法高亮显示的XML编辑器
- Android的TextView/EditText使用CharacterStyle&SpannableString来处理图片显示、字体样式、超链接等
- Android开发_WebView组件使用详解_LoadUrl直接显示网页内容
- Android基础教程(一)之------更改与显示文字标签TextView标签的使用
- Android入门教程(五)之------更改与显示文字标签TextView标签的使用(转)
- Android的TextView/EditText使用CharacterStyle&SpannableString来处理图片显示、字体样式、超链接等