Android开发之Api接口开发(Get请求和Post请求)总结

/**
 * 发送Get请求到服务器
 * @param strUrlPath:接口地址(带参数)
 * @return
 */
public static String getServiceInfo(String strUrlPath){
    String strResult = "";
    try {
        URL url = new URL(strUrlPath);
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.setRequestMethod("GET");
        conn.setUseCaches(false);
        conn.setConnectTimeout(10000);
        conn.setReadTimeout(10000);
        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
        StringBuffer buffer = new StringBuffer();
        String line = "";
        while ((line = in.readLine()) != null){
            buffer.append(line);
        }
        strResult = buffer.toString();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return strResult;
}

/**
 * 发送Post请求到服务器
 * @param strUrlPath:接口地址
 * @param params:请求体内容
 * @param encode:编码格式
 * @return
 */
public static String submitPostData(String strUrlPath, Map<String, String> params, String encode) {

    byte[] data = getRequestData(params, encode).toString().getBytes();//获得请求体
    try {
        URL url = new URL(strUrlPath);

        HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
        httpURLConnection.setConnectTimeout(10000);     //设置连接超时时间
        httpURLConnection.setDoInput(true);                  //打开输入流,以便从服务器获取数据
        httpURLConnection.setDoOutput(true);                 //打开输出流,以便向服务器提交数据
        httpURLConnection.setRequestMethod("POST");     //设置以Post方式提交数据
        httpURLConnection.setUseCaches(false);               //使用Post方式不能使用缓存
        //设置请求体的类型是文本类型
        httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        //设置请求体的长度
        httpURLConnection.setRequestProperty("Content-Length", String.valueOf(data.length));
        //获得输出流,向服务器写入数据
        OutputStream outputStream = httpURLConnection.getOutputStream();
        outputStream.write(data);
        //获得服务器的响应码
        int response = httpURLConnection.getResponseCode();
        if(response == HttpURLConnection.HTTP_OK) {
            InputStream inptStream = httpURLConnection.getInputStream();
            //处理服务器的响应结果
            return dealResponseResult(inptStream);
        }
    } catch (IOException e) {
        return "";
    }
    return "";
}

/**
 * 封装请求体信息
 * @param params:请求体内容
 * @param encode:编码格式
 * @return
 */
public static StringBuffer getRequestData(Map<String, String> params, String encode) {
    //存储封装好的请求体信息
    StringBuffer stringBuffer = new StringBuffer();
    try {
        for(Map.Entry<String, String> entry : params.entrySet()) {
            stringBuffer.append(entry.getKey())
                    .append("=")
                    .append(URLEncoder.encode(entry.getValue(), encode))
                    .append("&");
        }
        //删除最后的一个"&"
        stringBuffer.deleteCharAt(stringBuffer.length() - 1);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return stringBuffer;
}

/**
 * 处理服务器的响应结果(将输入流转化成字符串)
 * @param inputStream:服务器的响应输入流
 * @return
 */
public static String dealResponseResult(InputStream inputStream) {
    String resultData = null;      //存储处理结果
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    byte[] data = new byte[1024];
    int len = 0;
    try {
        while((len = inputStream.read(data)) != -1) {
            byteArrayOutputStream.write(data, 0, len);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    resultData = new String(byteArrayOutputStream.toByteArray());
    return resultData;
}

猜你喜欢

转载自blog.csdn.net/lpCrazyBoy/article/details/81198523