Android - HTTP util class SyncGET & SyncPOST
来源:互联网 发布:90后网络行为分析 编辑:程序博客网 时间:2024/06/06 10:26
Android - HTTP util class SyncGET & SyncPOST
public class UtilHttp { private static final String TAG = "UtilHttp"; public static String err; public static String doHttpGet(Context ctx, String serverUrl) { Log.v(TAG, "HTTPGET:" + serverUrl); String result = null; err = null; HttpURLConnection conn = null; InputStream in = null; try { int TIMEOUT_MILLISEC = 15000; conn = (HttpURLConnection) new URL(serverUrl).openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(TIMEOUT_MILLISEC); conn.setReadTimeout(TIMEOUT_MILLISEC); conn.setRequestProperty("Accept-Encoding", ""); conn.setRequestProperty("Accept", "application/json"); conn.setDoInput(true); conn.connect(); int responseCode = conn.getResponseCode(); Log.v(TAG, "Return code = " + responseCode); if (responseCode == 200 || responseCode == 400) { in = new BufferedInputStream(conn.getInputStream()); String encoding = conn.getContentEncoding() == null ? "UTF-8" : conn.getContentEncoding(); result = IOUtils.toString(in, encoding); } else { String responseMsg = conn.getResponseMessage(); if (responseMsg != null) { Log.v(TAG, "Response message = " + responseMsg); err = responseCode + ":" + responseMsg; } else { Log.v(TAG, "Response message is null"); err = responseCode + ":" + ctx.getString(R.string.responseisnull); } } } catch (IOException e) { e.printStackTrace(); err = e.getMessage(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } if (conn != null) { conn.disconnect(); conn = null; } } if (result != null) { Log.v(TAG, result); } else { Log.v(TAG, "Result is null"); } return result; } public static String doHttpPostWithResult(Context ctx, String serverUrl, String params) { Log.v(TAG, "HTTPPOST:" + serverUrl); String result = null; err = null; HttpURLConnection conn = null; InputStream in = null; OutputStreamWriter out = null; try { int TIMEOUT_MILLISEC = 15000; conn = (HttpURLConnection) new URL(serverUrl).openConnection(); conn.setRequestMethod("POST"); conn.setConnectTimeout(TIMEOUT_MILLISEC); conn.setReadTimeout(TIMEOUT_MILLISEC); conn.setRequestProperty("Accept-Encoding", ""); conn.setRequestProperty("Content-Type", "application/json"); conn.setRequestProperty("Accept", "application/json"); conn.setDoOutput(true); conn.setDoInput(true); conn.setChunkedStreamingMode(0); if (params != null) { out = new OutputStreamWriter(conn.getOutputStream()); out.write(params); out.flush(); } conn.connect(); int responseCode = conn.getResponseCode(); Log.v(TAG, "Return code = " + responseCode); if (responseCode == 200 || responseCode == 400) { in = new BufferedInputStream(conn.getInputStream()); String encoding = conn.getContentEncoding() == null ? "UTF-8" : conn.getContentEncoding(); result = IOUtils.toString(in, encoding); } else { String responseMsg = conn.getResponseMessage(); if (responseMsg != null) { Log.v(TAG, "Response message = " + responseMsg); err = responseCode + ":" + responseMsg; } else { Log.v(TAG, "Response message is null"); err = responseCode + ":" + ctx.getString(R.string.responseisnull); } } } catch (IOException e) { e.printStackTrace(); err = e.getMessage(); } finally { if (out != null) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } if (conn != null) { conn.disconnect(); conn = null; } } if (result != null) { Log.v(TAG, result); } else { Log.v(TAG, "Result is null"); } return result; }}
1 0
- Android - HTTP util class SyncGET & SyncPOST
- Android Util Class:TextUtils +DateFormat
- util class
- Android Studio:Could not find class android.util.ArrayMap
- android.util.Log http://blog.lytsing.org/archives/236.html
- Http CLASS
- Class,HTTP
- Http请求util类
- 关于java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.Att
- Android Studio各种坑爹之:dalvikvm: Could not find class 'android.util.ArrayMap'
- 关于java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.Att
- java.util.*; public class ScannerExample
- 【java】Java.util.Properties Class
- java.util.zip.ZipException: duplicate entry: android/support/annotation/ColorRes.class
- 记录java.util.zip.ZipException: duplicate entry: android/support/v4/os/BuildCompat.class
- Error:Could not initialize class com.android.ide.common.util.ReadWriteProcessLock 解决办法
- PullToRefreshWebView:can't find referenced method 'float floor(float)' in library class android.util
- Android 解决java.util.zip.ZipException: duplicate entry: com/ta/utdid2/a/a.class
- HTTP协议形象展现
- python学习——Day1
- 使用PHPStorm + Xdebug + Firefox 单步调试PHP并分析代码性能
- 三种方式实现侧滑菜单(自定义View+github经典demo+v4 drawerlayout)
- 获取 SHA1 值
- Android - HTTP util class SyncGET & SyncPOST
- MySQL主从同步校验工具_pt-table-checksum
- AQS
- 大数据实战下笔记——Hadoop NameNode HA的原理
- java使用POI实现excel文件的读取,兼容后缀名xls和xlsx
- UVA - 11389 The Bus Driver Problem
- 【挖坑记】 JZOJ 4724 斐波那契
- Android - local storage & UserTable sample
- bootstrap模态框和select2合用时input无法获取焦点