欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

网络请求封装

程序员文章站 2022-06-05 21:06:34
...
package bw.com.ak.network;

import android.content.Context;
import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

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;
       }






}