Android10--Android之异步任务加载网页和图片
来源:互联网 发布:电子图书数据库有哪些 编辑:程序博客网 时间:2024/06/04 19:34
今天介绍的是一个简单的从网上获取网页和图片,我们将其做成工具类,再使用时只需要传入相应的参数即可.
首先我们定义一个要获取的一个类型Entry:因为设计到Gson解析,我们不要忘记给没个属性加上注解.
public class Entry { @SerializedName("title") private String title; @SerializedName("message") private String message; @SerializedName("img") private String image; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public String getImage() { return image; } public void setImage(String image) { this.image = image; }}
图片加载类工具类:通用
这里对图片的加载做了三种处理:加载之前 加载完成 加载失败
public class ImageLoader extends AsyncTask<String, Void, Bitmap> { private ImageView image; public ImageLoader(ImageView image) { this.image = image; } @Override protected void onPreExecute() { super.onPreExecute(); image.setImageResource(R.mipmap.coming); } @Override protected Bitmap doInBackground(String... params) { String url = params[0]; try { HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); connection.setRequestMethod("GET"); connection.setDoInput(true); int code = connection.getResponseCode(); if (code == 200) { InputStream is = connection.getInputStream(); return BitmapFactory.decodeStream(is); } } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Bitmap bitmap) { super.onPostExecute(bitmap); if (bitmap != null) { image.setImageBitmap(bitmap); } else { image.setImageResource(R.mipmap.ic_launcher); } }}
解析一个网页工具类:通用
只需传入要解析的网址,和要解析成的Class,之后再去开启这个异步线程
public class NetworkTask<T> extends AsyncTask<NetworkTask.Callback<T>, Void, Object> { private Callback<T> callback; private String url; private Class<T> t; public NetworkTask(String url, Class<T> t) { this.url = url; this.t = t; } @Override protected Object doInBackground(Callback<T>... params) { callback = params[0]; try { HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); connection.setRequestMethod("GET"); connection.setDoInput(true); int code = connection.getResponseCode(); if (code == 200) { InputStream is = connection.getInputStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); int length; byte[] buffer = new byte[102400]; while ((length = is.read(buffer)) != -1){ bos.write(buffer, 0, length); } Gson gson = new Gson(); return gson.fromJson(bos.toString("UTF-8"), t); } else { return new RuntimeException("ResponseCode: " + code); } } catch (IOException e) { return e; } } @Override protected void onPostExecute(Object o) { super.onPostExecute(o); if (t.isInstance(o)) { callback.onSuccess((T) o); } else if (o instanceof Exception) { callback.onFail((Exception) o); } } public interface Callback<S> { void onSuccess(S text); void onFail(Exception e); }}
为了让加载之后的图片显示和网页内容一起滑动:我们使用的ScrollView,但是要注意ScrollView中只能放置一个控件.一般是一个布局
<ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/main2_image"/> <WebView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/main2_web"/> </LinearLayout> </ScrollView>
测试类:
用webView承载一个网页的内容时,如果是图文混排,里面的图片可能会超出屏幕的宽度,我们可以加上: private static final String CSS = "<style>img{max-width:100%}</style>";
public class Main2Activity extends AppCompatActivity implements NetworkTask.Callback<Entry>{ private WebView web; private ImageView image; private static final String CSS = "<style>img{max-width:100%}</style>"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); web = (WebView) findViewById(R.id.main2_web); image = (ImageView) findViewById(R.id.main2_image); new NetworkTask<>("http://www.tngou.net/api/top/show?id=13122", Entry.class).execute(this); } @Override public void onSuccess(Entry entry) { setTitle(entry.getTitle()); new ImageLoader(image).execute("http://tnfs.tngou.net/img" + entry.getImage()); web.loadDataWithBaseURL("http://www.tngou.net", CSS + entry.getMessage(), "text/html; charset=utf-8", "UTF-8", null); } @Override public void onFail(Exception e) { e.printStackTrace(); Toast.makeText(Main2Activity.this, "网络错误", Toast.LENGTH_SHORT).show(); }}
下一篇讲引入动态接口:将之中的工具类打包为另一个Liabray,在喜爱次使用时只要import即可..
http://blog.csdn.net/u012954720/article/details/52398370
1 0
- Android10--Android之异步任务加载网页和图片
- Android10--Android之动态接口实现加载网页和图片
- Android10--Android之异步任务AsyncTask
- android之图片异步加载
- Android之异步任务加载网络数据
- Android开发之异步任务加载网络图片并存储在sdcard中(源代码分享)
- 异步任务加载网络图片
- android异步任务 访问网络 加载图片 解决方案大集合
- Android之handle异步加载图片
- android 之图片异步加载,带缓存。
- Android开发之ListView异步加载图片
- Android之异步加载网络图片
- Android异步图片加载组件之 Universal_image_loader
- Android有效加载图片 之 使用AsyncTask异步加载图片
- Android ListView和GridView异步加载图片
- android异步加载图片
- android 异步加载图片
- android异步加载图片
- iOS 打电话 发短信(sms) 发邮件(mail) 调用浏览器 safari
- 类(class)简单总结
- [LeetCode] 125. Valid Palindrome
- 关于springmvc 刷新action的方法重复提交的问题 重复执行
- 《从零开始学Swift》学习笔记(Day 22)——闭包那些事儿!
- Android10--Android之异步任务加载网页和图片
- squarePatternPrint
- ios developer tiny share-20160829
- 淘宝TFS安装
- toLowerCase和toLocaleLowerCase的区别
- 在 Linux 虚拟机中手动安装或升级 VMware Tools
- js实现标准无缝滚动
- 场景示例 Nginx 访问日志
- 使用js实现带有停顿效果的图片滚动(按钮控制)