Android同步网络请求工具类HttpURLConnection
程序员文章站
2022-06-04 15:29:49
...
package com.cz.utils; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.util.HashMap; import android.util.Log; public class HttpUtil { private static final String GET = "GET"; private static final String POST = "POST"; private static final int connectTimeout = 6000; private static final int readTimeout = 30000; /** * GET方式发送数据 */ public static String sendGet(String http, String data, String charset) { return request(http, data, charset, GET); } public static String sendGet(String http, HashMap<String, String> map, String charset) { return sendGet(http, map, charset, false); } public static String sendGet(String http, HashMap<String, String> map, String charset, boolean encode) { return sendGet(http, praseMap(map, charset, encode), charset); } /** * POST方式发送数据 */ public static String sendPost(String http, String data, String charset) { return request(http, data, charset, POST); } public static String sendPost(String http, HashMap<String, String> map, String charset) { return sendPost(http, map, charset, false); } public static String sendPost(String http, HashMap<String, String> map, String charset, boolean encode) { return sendPost(http, praseMap(map, charset, encode), charset); } /** * 解析map */ private static String praseMap(HashMap<String, String> map, String charset, boolean encode) { StringBuffer sb = new StringBuffer(); if (map != null && !map.isEmpty()) { try { boolean f = true; String v; for (String k : map.keySet()) { if (k != null && !"".equals(k)) { v = map.get(k).trim(); if (!f) sb.append("&"); if (encode) v = URLEncoder.encode(v, charset); sb.append(k).append("=").append(v); f = false; } } } catch (Exception e) { e.printStackTrace(); } } return sb.toString().trim(); } private static String request(String http, String data, String charset, String type) { StringBuffer sb = new StringBuffer(); HttpURLConnection conn = null; OutputStreamWriter out = null; BufferedWriter bw = null; InputStreamReader isr = null; BufferedReader br = null; try { if (GET.equals(type) && data != null && !"".equals(data)){ http = http + "?" + data; } URL u = new URL(http); conn = (HttpURLConnection) u.openConnection(); conn.setRequestMethod(type); conn.setConnectTimeout(connectTimeout); conn.setReadTimeout(readTimeout); if (POST.equals(type)) conn.setDoOutput(true); conn.setDoInput(true); conn.connect(); // 传送数据 if (POST.equals(type)) { if (data != null && !"".equals(data)) { out = new OutputStreamWriter(conn.getOutputStream(), charset); bw = new BufferedWriter(out); bw.write(data); bw.flush(); } } // 接收数据 if (conn.getResponseCode() == 200) { isr = new InputStreamReader(conn.getInputStream(), charset); br = new BufferedReader(isr); String line; while ((line = br.readLine()) != null){ sb.append(line).append(System.getProperty("line.separator")); } } } catch (Exception e) { e.printStackTrace(); } finally { try { bw.close(); } catch (Exception e) { } try { out.close(); } catch (Exception e) { } try { br.close(); } catch (Exception e) { } try { isr.close(); } catch (Exception e) { } try { conn.disconnect(); } catch (Exception e) { } } Log.e("sb:", sb.toString().trim()); return sb.toString().trim(); } }
推荐阅读
-
Android开发中4个常用的工具类【Toast、SharedPreferences、网络及屏幕操作】
-
android实用工具类分享(获取内存/检查网络/屏幕高度/手机分辨率)
-
Android工具类-->当前网络状态监听
-
Android封装的http请求实用工具类
-
Android自定义网络连接工具类HttpUtil
-
android实用工具类分享(获取内存/检查网络/屏幕高度/手机分辨率)
-
Android同步网络请求工具类HttpURLConnection
-
Android同步网络请求工具类HttpURLConnection
-
网络请求 当我们遇到调用很多次为了减少代码量和内存的占用 我们可以写一个工具类 每次需要我们可以直接调用 单例模式
-
【黑马Android】(05)短信/查询和添加/内容观察者使用/子线程网络图片查看器和Handler消息处理器/html查看器/使用HttpURLConnection采用Post方式请求数据/开源项目_html/css_WEB-ITnose