注意:
Task的实例必须在UI Thread中创建
execute方法不惜在UI thread中创建
task只能被执行一次 多次调用时会出现异常
通用AsyncTask 以及在主线程中使用网络请求回返的数据
通用AsyncTask是什么意思呢 发送不同的请求返回不同类型的数据 难道要一个类型写个AsyncTask 岂不是麻烦死咯
还有一种情况 我们通过异步任务得到了一个对象然后在一下行立刻使用这个对象逻辑完全没问题但是运行之后会报空指针异常。这是怎么回事呢?
AsycnTask开始了一个新的线程,但是主线程并没有停止还在继续运行,马上就使用这个对象,而你新开的线程可能正在访问网络这个对象为空
你无法确定AsycnTask什么时候才能获取到数据,网快嗖的一下就好了,网慢就要等好久。
看一个简略的小例子
首先呢 我们使用异步任务的时候要处理不同类型的数据把这个Http设置泛型类第三个参数返回值类型设置为泛型不管你是什么类型的数据全部ok
我又写了一个接口作为Http的属性 在onPostExecute方法调用其中的onResponse方法在Test中实现接口
这个接口的作用完全可以理解为一个监听事件 checkbox的改变监听触发条件是 是否选中这个接口监听是否有数据 完成网络访问有数据的时候就调用
我们在主线程中完成接口的实现已经在主线程中实现了返回来的数据还不是任君宰割阿~~~~~
public class Http<T> extends AsyncTask<String,Void,T> { private OnResponseListener<T> listener; public void setListener(OnResponseListener<T> listener) { this.listener = listener; } @Override protected T doInBackground(String... params) { return null; } @Override protected void onPostExecute(T t) { super.onPostExecute(t); if (listener!=null){listener.onResponse(t); } } //接口 类似一个监听事件 public interface OnResponseListener<T>{ void onResponse(T t); }}//获取数据的测试类public class Test { //要获取的user对象 private User user1=null; public void get(){ //创建网络访问实例 Http<User> http=new Http<User>(); //重写接口 http.setListener(new Http.OnResponseListener<User>() {@Overridepublic void onResponse(User user) {user1=user;} }); http.execute("xxx.balabala.com"); }}在发送请求的时候很容易就带个参数,请求的方式呢 无非就是get,post 两者的区别呢大白话的说get不安全参数通过url直接传过去post安全参数加密一下子
protected T doInBackground(String... params) { //网络连接对象 HttpURLConnection connection=null; //输入流 获取网络数据 InputStream is=null; //字节数组输出流 ByteArrayOutputStream bos=null; try {//获取网络连接对象connection=(HttpURLConnection) new URL(params[0]).openConnection();//设置get请求 必须大写connection.setRequestMethod("GET");//获取网络请求码 200 400 500之类 不懂百度int code=connection.getResponseCode();if(code==200){//获取流is=connection.getInputStream();//临时字节数组byte [] b=new byte[1024];int len=-1;bos=new ByteArrayOutputStream();while ((len=is.read(b))!=-1){ //写入数据 bos.write(b,0,len);}String json=bos.toString("utf-8");T t=JSON.parseObject(json,type);return t;}else{Log.e("error","网络访问失败==========="+code);} } catch (IOException e) {e.printStackTrace(); }finally {try {if (bos!=null){ bos.close();}if (is!=null){ is.close();}} catch (IOException e) {e.printStackTrace();}if (connection!=null){connection.disconnect();} } return null; }POST
protected T doInBackground(String... params) { //分割url 分为地址和参数两部分 String[] strArr=params[0].split("\?"); HttpURLConnection connection=null; //输出流 OutputStream os=null; //输入流 InputStream is=null; ByteArrayOutputStream bos=null; try {connection=(HttpURLConnection) new URL(strArr[0]).openConnection();connection.setRequestMethod("POST");//设置允许输入 输出 默认值true 不写也可以connection.setDoOutput(true);connection.setDoInput(true);os=connection.getOutputStream();//把参数写入os.write(strArr[1].getBytes("utf-8"));os.close();int code=connection.getResponseCode();if(code==200){is=connection.getInputStream();byte [] b=new byte[1024];int len=-1;bos=new ByteArrayOutputStream();while ((len=is.read(b))!=-1){ bos.write(b,0,len);}String json=bos.toString("utf-8");T t=JSON.parseObject(json,type);return t;}else{Log.e("error","网络访问失败==========="+code);} } catch (IOException e) {e.printStackTrace(); }finally {try {if (bos!=null){ bos.close();}if (is!=null){ is.close();}} catch (IOException e) {e.printStackTrace();}if (connection!=null){connection.disconnect();} } return null; }以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持脚本之家!