最近培训安卓阶段快要结束了,也拿了个项目教我们做,其实我想提醒大家,既然是练手项目,就有他的意义。
就拿我前一篇天气教程,一个简单的APP也有难点,比如数据持久化,图片缓存。多多练手也对于我们这些初级开发者有着重要的意义。
因为只是一个练手项目,页面做的很丑,但是其中含有很多技术点。
1.Fastjson 解析
2.联网工具内自带线程范问
3.广告栏
4.图片缓存
5.数据持久化-保存到本地
我这一节讲解的是联网工具类。我知道联网工具类,有很多,Httpconnection,httpclient,Xutils,volley,ansy—http
如果前两种因为是初级封装,只是将联网加入,像后面三种都是自己开了线程or 线程池去联网。所以大家在用用后三种是不用考虑是不是在主线程。前两种得自己开启线程了。
我现在要写的就是第四层
第一层封装就是只是将Http的联网封装进了一个类,但是返回的是一个流,而我们需要的是一个String类型的字符床 |
|
|
第二层就是将输入流转字符串加进入了 也就是用bufferread进行转 输出String,这个时候基本已经满足了我们的需求了,但是还要为了他单独开线程这样不好。 |
第三层 加入线程池 进行联网 handler.post发送结果,用借口回调给调用者 |
第四层加入 访问图片 |
慢慢来,特别是接口回调
1. // 使用线程池来下载图片,同一时刻,最多有3个线程在运行
private static ExecutorService execuotrs = Executors.newFixedThreadPool(3);
会得到一个 execuotrsService对象 注意的是 通过对象会执行 线程操作
2.建立接口,并作为参数 ,回调数据
public interface OnNetWorkResponse {
public void ok(String response);
public void error(String error);
}
3.接着就是联网
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setConnectTimeout(5000);
connection.setDoInput(true);
connection.connect();
4.输入流转String
if (connection.getResponseCode() == 200) {
inputStream = connection.getInputStream();
outStream = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int len = 0;
while ((len = inputStream.read(b)) != -1) {
outStream.write(b, 0, len);
}
outStream.flush();
final String result = new String(
outStream.toByteArray());
5.用handler发送消息回去
6.调用的话只需要将接口实例化就行了
HttpUtils.RequestNetWork(AppURLFinal.URL_NAVIGATION, new OnNetWorkResponse() { @Override public void ok(String response) {
final Bitmap bitmap = BitmapFactory
.decodeStream(inputStream);
HttpUtils
package com.example.android20_lzhxw.utils; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.nio.ByteOrder; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Handler; /** * 封装的 网络 + 线程 */ public class HttpUtils { // 使用线程池来下载图片,同一时刻,最多有3个线程在运行 private static ExecutorService execuotrs = Executors.newFixedThreadPool(3); interface OnBitmapNetWorkResponse { public void ok(Bitmap bitmap); public void error(String error); } public static void RequestBitmapNetWork(final String path, final OnBitmapNetWorkResponse response) { final Handler handler = new Handler(); execuotrs.execute(new Runnable() { @Override public void run() { boolean isNetWorkOK = false; try { URL url = new URL(path); HttpURLConnection openConnection = (HttpURLConnection) url .openConnection(); openConnection.setConnectTimeout(5000); openConnection.connect(); if (openConnection.getResponseCode() == 200) { InputStream inputStream = openConnection .getInputStream(); final Bitmap bitmap = BitmapFactory .decodeStream(inputStream); handler.post(new Runnable() { @Override public void run() { response.ok(bitmap); } }); inputStream.close(); isNetWorkOK = true; } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (!isNetWorkOK) { handler.post(new Runnable() { @Override public void run() { response.error("服务器不在服务区内!"); } }); } } } }); } public interface OnNetWorkResponse { public void ok(String response); public void error(String error); } public static void RequestNetWork(final String path, final OnNetWorkResponse response) { //实例化handler final Handler hanlder = new Handler(); new Thread() { public void run() { //标志位 boolean isWorkOK = false; InputStream inputStream = null; ByteArrayOutputStream outStream = null; try { URL url = new URL(path); HttpURLConnection connection = (HttpURLConnection) url .openConnection(); connection.setConnectTimeout(5000); connection.setDoInput(true); connection.connect(); if (connection.getResponseCode() == 200) { inputStream = connection.getInputStream(); outStream = new ByteArrayOutputStream(); byte[] b = new byte[1024]; int len = 0; while ((len = inputStream.read(b)) != -1) { outStream.write(b, 0, len); } outStream.flush(); final String result = new String( outStream.toByteArray()); hanlder.post(new Runnable() { @Override public void run() { response.ok(result); } }); isWorkOK = true; } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // 网络操作出问题 if (!isWorkOK) { response.error("服务器走神拉!"); } if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (outStream != null) { try { outStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }; }.start(); } }