日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

网络请求工具

發布時間:2025/1/21 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 网络请求工具 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近在對以前寫的代碼進行總結,為了方便以后的使用和查看,所以對自己負責模塊的通用代碼進行總結。

在我負責的應用管控中,網絡請求用的是HttpURLConnection,并沒有用OkHttp。

網絡請求一般就Get和Post請求。

1. 工具類定義
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.HttpURLConnection; import java.net.URL;/*** Created by salmonzhang on 2018/12/27.* HttpURLConnection網絡請求工具類*/public class HttpUtil {private static final String TAG = "HttpUtil";public interface HttpCallbackListener {//網絡請求成功void onFinish(String response);//網絡請求失敗void onError(Exception e);}/*** Get請求*/public static void sendGetRequest(final String urlString, final HttpCallbackListener listener) {// 因為網絡請求是耗時操作,所以需要另外開啟一個線程來執行該任務。new Thread(new Runnable() {@Overridepublic void run() {HttpURLConnection httpURLConnection = null;BufferedReader reader = null;try {// 根據URL地址創建URL對象URL url = new URL(urlString);// 獲取HttpURLConnection對象httpURLConnection = (HttpURLConnection) url.openConnection();// 設置請求方式,默認為GEThttpURLConnection.setRequestMethod("GET");// 設置連接超時httpURLConnection.setConnectTimeout(8000);// 設置讀取超時httpURLConnection.setReadTimeout(8000);// 響應碼為200表示成功,否則失敗。if (httpURLConnection.getResponseCode() == 200) {// 獲取網絡的輸入流InputStream in = httpURLConnection.getInputStream();// 讀取輸入流中的數據reader = new BufferedReader(new InputStreamReader(in));StringBuilder response = new StringBuilder();String line;while ((line = reader.readLine()) != null) {response.append(line);}//響應數據if (listener != null) {// 回調onFinish()方法listener.onFinish(response.toString());}} else {LogUtil.i(TAG, "run: 請求失敗");}} catch (IOException e) {// 回調onError()方法if (listener != null) {// 回調onError()方法listener.onError(e);}e.printStackTrace();} finally {//關流if (reader != null) {try {reader.close();} catch (IOException e) {e.printStackTrace();}}if (httpURLConnection != null) {// 釋放資源httpURLConnection.disconnect();}}}}).start();}/*** Post請求*/public static void sendPostRequest(final String urlString,final String data, final HttpCallbackListener listener) {// 因為網絡請求是耗時操作,所以需要另外開啟一個線程來執行該任務。new Thread(new Runnable() {@Overridepublic void run() {URL url;HttpURLConnection httpURLConnection = null;BufferedReader reader = null;try {url = new URL(urlString);httpURLConnection = (HttpURLConnection) url.openConnection();httpURLConnection.setRequestMethod("POST");httpURLConnection.setConnectTimeout(8000);httpURLConnection.setReadTimeout(8000);// 設置運行輸入httpURLConnection.setDoInput(true);// 設置運行輸出httpURLConnection.setDoOutput(true);//獲取URLConnection對象對應的輸出流PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());//發送請求參數printWriter.write(data);//data的參數格式xx=xx&yy=yy//flush輸出流的緩沖printWriter.flush();printWriter.close();//開始獲取數據if (httpURLConnection.getResponseCode() == 200) {InputStream in = httpURLConnection.getInputStream();// 讀取輸入流中的數據reader = new BufferedReader(new InputStreamReader(in));StringBuilder response = new StringBuilder();String line;while ((line = reader.readLine()) != null) {response.append(line);}//響應數據if (listener != null) {// 回調onFinish()方法listener.onFinish(response.toString());}} else {LogUtil.i(TAG, "請求失敗 ");}} catch (IOException e) {if (listener != null) {// 回調onError()方法listener.onError(e);}e.printStackTrace();} finally {//關流if (reader != null) {IoCloseUtil.closeAll(reader);}if (httpURLConnection != null) {// 最后記得關閉連接httpURLConnection.disconnect();}}}}).start();} }
2. 工具類使用
HttpUtil.sendGetRequest(url, new HttpUtil.HttpCallbackListener() {@Overridepublic void onFinish(String response) {LogUtil.d(TAG, "request control list json = " + response);//開始解析網絡請求返回的數據parseJsonData(response);}@Overridepublic void onError(Exception e) {LogUtil.d(TAG, "network request control list failed !!!");} });
非常感謝您的耐心閱讀,希望我的文章對您有幫助。歡迎點評、轉發或分享給您的朋友或技術群。

總結

以上是生活随笔為你收集整理的网络请求工具的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。