我是靠谱客的博主 完美黑米,最近开发中收集的这篇文章主要介绍Android原生网络请求工具类,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

package com.lyn.ist_library;

import org.json.JSONObject;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Map;


/**
 * 原生网络请求工具类
 *
 * @author longyn
 * @version 1.0.0`在这里插入代码片`
 * @date 2022/11/17
 */
public class HttpUtil {

    private static class HttpUtilTypeClass {
        private static final HttpUtil instance = new HttpUtil();
    }

    public static HttpUtil getInstance() {
        return HttpUtilTypeClass.instance;
    }

    public String post(String url, JSONObject data) {
        try {
            HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
            conn.setRequestMethod("POST");
            conn.setReadTimeout(5000);
            conn.setConnectTimeout(5000);
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            conn.setRequestProperty("Connection", "keep-Alive");
            conn.setRequestProperty("Content-Type", "application/json");
            OutputStream outputStream = conn.getOutputStream();
            outputStream.write(data.toString().getBytes(StandardCharsets.UTF_8));
            outputStream.flush();
            if (conn.getResponseCode() == 200) {
                InputStream inputStream = conn.getInputStream();
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                int length = 0;
                byte[] bytes = new byte[1024];
                while ((length = inputStream.read(bytes)) != -1) {
                    byteArrayOutputStream.write(bytes, 0, length);
                }
                byteArrayOutputStream.close();
                inputStream.close();
                return byteArrayOutputStream.toString();
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * get 请求
     *
     * @param url url地址
     * @param par 参数
     * @return 请求结果
     */
    public String get(String url, Map<String, String> par) {
        try {
            HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
            conn.setRequestMethod("GET");
            conn.setReadTimeout(5000);
            conn.setConnectTimeout(5000);
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            conn.setRequestProperty("Connection", "keep-Alive");
            OutputStream outputStream = conn.getOutputStream();
            outputStream.write(getUrlPar(par).getBytes(StandardCharsets.UTF_8));
            outputStream.flush();
            if (conn.getResponseCode() == 200) {
                InputStream inputStream = conn.getInputStream();
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                int length = 0;
                byte[] bytes = new byte[1024];
                while ((length = inputStream.read(bytes)) != -1) {
                    byteArrayOutputStream.write(bytes, 0, length);
                }
                byteArrayOutputStream.close();
                inputStream.close();
                return byteArrayOutputStream.toString();
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 解析url带参为字符串
     *
     * @param par 参数map
     * @return url参数字符串
     */
    public String getUrlPar(Map<String, String> par) {
        if (par == null || par.isEmpty()) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        int i = 0;
        for (String key : par.keySet()) {
            if (i == 0) {
                sb.append("?").append(key).append("=").append(par.get(key));
            } else {
                sb.append("&").append(key).append("=").append(par.get(key));
            }
            i += 1;
        }
        return sb.toString();
    }
}

最后

以上就是完美黑米为你收集整理的Android原生网络请求工具类的全部内容,希望文章能够帮你解决Android原生网络请求工具类所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(51)

评论列表共有 0 条评论

立即
投稿
返回
顶部