AsyncTask异步请求工具类

package com.bwie.utils.xujiahui20180921;

import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;

import com.google.common.io.CharStreams;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**

  • @author:HttpUtils 2018.9.21

  • 网络请求工具类
    */
    public class HttpUtils {
    private HttpRequestListener httpRequestListener;

    public HttpUtils(HttpRequestListener httpRequestListener) {
    this.httpRequestListener = httpRequestListener;
    }
    private void doHttp(String url, String type, String conn) {
    MyAsyncTask myAsyncTask = new MyAsyncTask(url, type, conn);
    myAsyncTask.execute(url);
    }

    public void doGet(String url) {
    doHttp(url, “GET”, “”);
    }

    public void doPost(String url, String conn) {
    doHttp(url, “POST”, conn);
    }

    //MyasyncTask
    private class MyAsyncTask extends AsyncTask<String, Integer, String> {
    private String url;
    private String type;
    private String conn;

     public MyAsyncTask(String url, String type, String conn) {
         this.url = url;
         this.type = type;
         this.conn = conn;
     }
    
     @Override
     protected String doInBackground(String... strings) {
         String data = "";
         try {
             URL murl = new URL(url);
             try {
                 HttpURLConnection connection = (HttpURLConnection) murl.openConnection();
                 connection.setRequestMethod(type);
                 connection.setConnectTimeout(1 * 5000);
                 if ("POST".equals(type)) {
                     connection.setDoOutput(true);
                     connection.getOutputStream().write(conn.getBytes());
                 }
                 int code = connection.getResponseCode();
                 if (code == HttpURLConnection.HTTP_OK) {
                     InputStream inputStream = connection.getInputStream();
                     data = CharStreams.toString(new InputStreamReader(inputStream, "UTF-8"));
                     Message message = new Message();
                     message.obj = data;
                     message.what = 1000;
                     handler.sendMessage(message);
                 } else {
                     data = "0";
                 }
             } catch (IOException e) {
                 e.printStackTrace();
             }
         } catch (MalformedURLException e) {
             e.printStackTrace();
         }
    
         return data;
     }
    
     @Override
     protected void onPostExecute(String s) {
         super.onPostExecute(s);
     }
    

    }

    private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
    super.handleMessage(msg);
    int what = msg.what;
    if (what == 1000) {
    String data = (String) msg.obj;
    if (“0”.equals(data)) {
    httpRequestListener.Filed(“请求失败”);
    } else {
    httpRequestListener.SuccessRequest(data);
    }
    }
    }
    };
    /* //doHttp
    private void doHttp(String url, String type, String con) {
    MyAsyncTask myAsyncTask = new MyAsyncTask(url, type, con);
    myAsyncTask.execute(url);
    }

    //get
    public void doGet(String url) {
    doHttp(url, “GET”, “”);
    }

    //post
    public void doPost(String url, String con) {
    doHttp(url, “POST”, con);
    }

    //MyAsyncTask
    private class MyAsyncTask extends AsyncTask<String, Integer, String> {
    private String url;
    private String type;
    private String con;

     public MyAsyncTask(String url, String type, String con) {
         this.url = url;
         this.type = type;
         this.con = con;
     }
    
     @Override
     protected String doInBackground(String... strings) {
         String data = "";
         try {
             URL murl = new URL(url);
             try {
                 HttpURLConnection connection = (HttpURLConnection) murl.openConnection();
                 connection.setRequestMethod(type);
                 connection.setConnectTimeout(2000);
                 if ("POST".equals(type)) {
                     connection.setDoOutput(true);
                     connection.getOutputStream().write(con.getBytes());
                 }
                 int code = connection.getResponseCode();
                 if (code == HttpURLConnection.HTTP_OK) {
                     InputStream inputStream = connection.getInputStream();
                     data = CharStreams.toString(new InputStreamReader(inputStream, "UTF-8"));
                     Message message = new Message();
                     message.obj = data;
                     message.what = 1000;
                     handler.sendMessage(message);
                 } else {
                     data = "0";
                 }
             } catch (IOException e) {
                 e.printStackTrace();
             }
         } catch (MalformedURLException e) {
             e.printStackTrace();
         }
    
         return data;
    
     }
    
     @Override
     protected void onPostExecute(String s) {
         super.onPostExecute(s);
     }
    

    }

    private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
    super.handleMessage(msg);
    int what = msg.what;
    if (what == 1000) {
    String data = (String) msg.obj;
    if (data.equals(0)) {
    httpRequestListener.SuccessRequest(data);
    } else {
    httpRequestListener.Filed(“请求失败”);
    }

         }
     }
    

    };*/
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值