封装AysncTask步骤

jilei1.首先把Http请求封装成一个类

 

public class HttpUrlUtils {
    //请求获取网络请求
      public  static   String  getHttpCon(Context mContext,String mPath,String mRam){
          String messages = "";
          try {
              //http://result.eolinker.com/k2BaduF2a6caa275f395919a66ab1dfe4b584cc60685573?uri=tt
              //  String  mPath="http://47.95.253.123/boot/api/findKtcon?pageNum="+page+"&pageSize="+pageSize+"";
              //String

                  URL murl = new URL(mPath);
                  Log.i("shop", "doInBackground: " + mPath);
                  HttpURLConnection connection = (HttpURLConnection) murl.openConnection();
                  //设置请求发送
                  connection.setRequestMethod(mRam);
                  //请求超时
                  connection.setConnectTimeout(5 * 1000);
                  connection.setReadTimeout(5 * 1000);
                  //数据流处理
                  if (connection.getResponseCode() == 200) {
                      //获取输入流
                      InputStream inputStream = connection.getInputStream();
                      //读取输入流
                      byte[] b = new byte[1024 * 512]; //定义一个byte数组读取输入流
                      ByteArrayOutputStream baos = new ByteArrayOutputStream(); //定义缓存流来保存输入流的数据
                      int len = 0;
                      while ((len = inputStream.read(b)) > -1) {  //每次读的len>-1 说明是是有数据的
                          baos.write(b, 0, len);  //三个参数  输入流byte数组   读取起始位置  读取终止位置
                      }
                      messages = baos.toString();
                      inputStream.close();
                      connection.disconnect();
                  }
              } catch(Exception e){
                  e.printStackTrace();
              }

          return messages;
       }

}

 

2.在AsyncTask封装用接口回调 中调用封装的Http

public class MyTask extends AsyncTask {
    String mPath;
    Context mContext;
    String mRam;
    private TaskListeners taskListener ;
    View mView;
    //2.获取接口
    public MyTask setTaskListener(TaskListeners taskListener ){
        this.taskListener = taskListener ;
        return this ;
    }
    public MyTask(Context mContext, String mPath, String mRam) {
        this.mPath=mPath;
        this.mContext=mContext;
        this.mRam=mRam;
    }
    @Override
    protected String doInBackground(T... ts) {
        return HttpUrlUtils.getHttpCon(mContext,mPath,mRam);
    }
    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        if (s != null) {
            //3.实现接口里面的方法
            taskListener.result(s);
        }
    }
    //1.定义接口
    public interface TaskListeners{
        void result( String t );
    }


}

3.在Fragment中调用封装的好的方法继承基类

public class Frag0 extends BaseFragment {
    String path="http://result.eolinker.com/k2BaduF2a6caa275f395919a66ab1dfe4b584cc60685573?uri=tt";
    private XListView xlistview;

    @Override
    protected void initView(View view) {
        xlistview = view.findViewById(R.id.xlistview1);
    }

    @Override
    protected int initlayout() {
        return R.layout.frag0;
    }

    @Override
    protected void initdata() {
        new MyTask(getActivity(),path,"GET").setTaskListenear(new MyTask.TaskListenears() {
            @Override
            public void result(String t) {
                Gson gson=new Gson();
                UrlBean json = gson.fromJson(t, UrlBean.class);
                if (json!=null){
                    MyAdapter adapter = new MyAdapter(getActivity(),json);
                    xlistview.setAdapter(adapter);
                }
            }
        }).execute();


    }
}

 

你可能感兴趣的:(封装AysncTask步骤)