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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android http 的使用

發布時間:2024/9/30 Android 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android http 的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、okHttp

? ? ? ?https://github.com/square/okhttp

?

2、okhttp-utils

? ? ??https://github.com/hongyangAndroid/okhttp-utils

?

3、NoHttp

? ? ? https://github.com/yanzhenjie/NoHttp

?

4、okhttp-OkGo

? ? ?https://github.com/jeasonlzy/okhttp-OkGo

?

5、最原生的http

package www.yiba.com.wifisdk.http;import android.os.Build;import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection;import javax.net.ssl.HttpsURLConnection;public class HttpUtil {private static final int TIMEOUT_IN_MILLIONS = 25000;/*** Get請求,獲得返回數據* @param urlStr* @return* @throws Exception*/public static String doGet(String urlStr , Callback callback) {URL url = null;URLConnection conn = null;InputStream is = null;ByteArrayOutputStream baos = null;try {url = new URL(urlStr);int responseCode = -1;if (urlStr.toLowerCase().startsWith("https")) {conn = url.openConnection();((HttpsURLConnection) conn).setRequestMethod("GET");conn.setReadTimeout(TIMEOUT_IN_MILLIONS);conn.setConnectTimeout(TIMEOUT_IN_MILLIONS);conn.setRequestProperty("accept", "*/*");conn.setRequestProperty("connection", "Keep-Alive");if (Build.VERSION.SDK != null&& Build.VERSION.SDK_INT > 13) {//http://www.tuicool.com/articles/7FrMVfconn.setRequestProperty("Connection", "close");}responseCode = ((HttpsURLConnection) conn).getResponseCode();} else {conn = url.openConnection();((HttpURLConnection) conn).setRequestMethod("GET");conn.setReadTimeout(TIMEOUT_IN_MILLIONS);conn.setConnectTimeout(TIMEOUT_IN_MILLIONS);conn.setRequestProperty("accept", "*/*");conn.setRequestProperty("connection", "Keep-Alive");if (Build.VERSION.SDK != null&& Build.VERSION.SDK_INT > 13) {//http://www.tuicool.com/articles/7FrMVfconn.setRequestProperty("Connection", "close");}responseCode = ((HttpURLConnection) conn).getResponseCode();}if (responseCode == 200) {is = conn.getInputStream();baos = new ByteArrayOutputStream();int len = -1;byte[] buf = new byte[128];while ((len = is.read(buf)) != -1) {baos.write(buf, 0, len);}baos.flush();if ( callback != null ){callback.ok( baos.toString() );}return baos.toString();} else {if ( callback != null ){callback.fail();}throw new RuntimeException(" responseCode is not 200 ... ");}} catch (Exception e) {if ( callback != null ){callback.timeOut();}e.printStackTrace();} finally {try {if (is != null)is.close();} catch (IOException e) {}try {if (baos != null)baos.close();} catch (IOException e) {}if (conn != null) {if (urlStr.toLowerCase().startsWith("https")) {((HttpsURLConnection) conn).disconnect();} else {((HttpURLConnection) conn).disconnect();}}}return null;}public static String doPost(String url, String param , Callback callback ) {PrintWriter out = null;BufferedReader in = null;String result = "";URLConnection conn = null;try {URL realUrl = new URL(url);//打開和URL之間的連接conn = realUrl.openConnection();//設置通用的請求屬性conn.setRequestProperty("accept", "*/*");conn.setRequestProperty("connection", "Keep-Alive");conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");if (url.toLowerCase().startsWith("https")) {((HttpsURLConnection) conn).setRequestMethod("POST");} else {((HttpURLConnection) conn).setRequestMethod("POST");}conn.setRequestProperty("charset", "utf-8");conn.setUseCaches(false);//發送POST請求必須設置如下兩行conn.setDoOutput(true);conn.setDoInput(true);conn.setReadTimeout(TIMEOUT_IN_MILLIONS);conn.setConnectTimeout(TIMEOUT_IN_MILLIONS);if (param != null && !param.trim().equals("")) {// 獲取URLConnection對象對應的輸出流out = new PrintWriter(conn.getOutputStream());// 發送請求參數out.print(param);// flush輸出流的緩沖out.flush();}int responseCode = ((HttpURLConnection) conn).getResponseCode();if ( responseCode == 200 ){// 定義BufferedReader輸入流來讀取URL的響應in = new BufferedReader(new InputStreamReader(conn.getInputStream()));String line;while ((line = in.readLine()) != null) {result += line;}if ( callback != null ){callback.ok( result );}}else {if ( callback != null ){callback.fail();}}} catch (Exception e) {if ( callback != null ){callback.timeOut();}e.printStackTrace();}finally {try {if (out != null) {out.close();}if (in != null) {in.close();}} catch (IOException ex) {ex.printStackTrace();}if (conn != null) {if (url.toLowerCase().startsWith("https")) {((HttpsURLConnection) conn).disconnect();} else {((HttpURLConnection) conn).disconnect();}}}return result;}public interface Callback{void ok( String result ) ;void fail();void timeOut() ;}}

?

6、對原生http的簡易封裝

? ? ? 6.1?HttpCall http 請求結果的實體類

? ? ??

package www.yiba.com.analytics.http;/*** Created by ${zyj} on 2016/8/18.*/ public class HttpCall {private ResponseType httpQuestType ;private String result ;public ResponseType getHttpQuestType() {return httpQuestType;}public void setHttpQuestType(ResponseType httpQuestType) {this.httpQuestType = httpQuestType;}public String getResult() {return result;}public void setResult(String result) {this.result = result;} }

  

? ? ? 6.2?ResponseType?http 請求結果的類型

package www.yiba.com.analytics.http;/*** Created by ${zyj} on 2016/8/18.*/ public enum ResponseType {OK("請求成功") , FAIL("請求失敗") , TIMEOUT("請求超時") ;private String name ;private ResponseType(String name ){this.name = name ;}public String getName() {return name;} }

  

? ? ? 6.3?HttpUtil 包含 get 和 post請求

package www.yiba.com.analytics.http;import android.os.Build;import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection;import javax.net.ssl.HttpsURLConnection;public class HttpUtil {private static final int TIMEOUT_IN_MILLIONS = 25000;/*** Get請求,獲得返回數據* @param urlStr* @return* @throws Exception*/public static HttpCall doGet(String urlStr , Callback callback) {URL url = null;URLConnection conn = null;InputStream is = null;ByteArrayOutputStream baos = null;HttpCall httpCall = new HttpCall() ;try {url = new URL(urlStr);int responseCode = -1;if (urlStr.toLowerCase().startsWith("https")) {conn = url.openConnection();((HttpsURLConnection) conn).setRequestMethod("GET");conn.setReadTimeout(TIMEOUT_IN_MILLIONS);conn.setConnectTimeout(TIMEOUT_IN_MILLIONS);conn.setRequestProperty("accept", "*/*");conn.setRequestProperty("connection", "Keep-Alive");if (Build.VERSION.SDK != null&& Build.VERSION.SDK_INT > 13) {//http://www.tuicool.com/articles/7FrMVfconn.setRequestProperty("Connection", "close");}responseCode = ((HttpsURLConnection) conn).getResponseCode();} else {conn = url.openConnection();((HttpURLConnection) conn).setRequestMethod("GET");conn.setReadTimeout(TIMEOUT_IN_MILLIONS);conn.setConnectTimeout(TIMEOUT_IN_MILLIONS);conn.setRequestProperty("accept", "*/*");conn.setRequestProperty("connection", "Keep-Alive");if (Build.VERSION.SDK != null&& Build.VERSION.SDK_INT > 13) {//http://www.tuicool.com/articles/7FrMVfconn.setRequestProperty("Connection", "close");}responseCode = ((HttpURLConnection) conn).getResponseCode();}if (responseCode == 200) {is = conn.getInputStream();baos = new ByteArrayOutputStream();int len = -1;byte[] buf = new byte[128];while ((len = is.read(buf)) != -1) {baos.write(buf, 0, len);}baos.flush();if ( callback != null ){callback.ok( baos.toString() );}if ( httpCall != null ){httpCall.setHttpQuestType( ResponseType.OK );httpCall.setResult( baos.toString() );}} else {if ( callback != null ){callback.fail();}if ( httpCall != null ){httpCall.setHttpQuestType( ResponseType.FAIL );httpCall.setResult( "" );}}} catch (Exception e) {if ( callback != null ){callback.timeOut();}if ( httpCall != null ){httpCall.setHttpQuestType( ResponseType.TIMEOUT );httpCall.setResult( "" );}e.printStackTrace();} finally {try {if (is != null)is.close();} catch (IOException e) {}try {if (baos != null)baos.close();} catch (IOException e) {}if (conn != null) {if (urlStr.toLowerCase().startsWith("https")) {((HttpsURLConnection) conn).disconnect();} else {((HttpURLConnection) conn).disconnect();}}return httpCall ;}}public static HttpCall doPost(String url, String param , Callback callback ) {PrintWriter out = null;BufferedReader in = null;String result = "";URLConnection conn = null;HttpCall httpCall = new HttpCall() ;try {URL realUrl = new URL(url);//打開和URL之間的連接conn = realUrl.openConnection();//設置通用的請求屬性conn.setRequestProperty("accept", "*/*");conn.setRequestProperty("connection", "Keep-Alive");conn.setRequestProperty("Content-Type","application/json");if (url.toLowerCase().startsWith("https")) {((HttpsURLConnection) conn).setRequestMethod("POST");} else {((HttpURLConnection) conn).setRequestMethod("POST");}conn.setRequestProperty("charset", "utf-8");conn.setUseCaches(false);//發送POST請求必須設置如下兩行conn.setDoOutput(true);conn.setDoInput(true);conn.setReadTimeout(TIMEOUT_IN_MILLIONS);conn.setConnectTimeout(TIMEOUT_IN_MILLIONS);if (param != null && !param.trim().equals("")) {// 獲取URLConnection對象對應的輸出流out = new PrintWriter(conn.getOutputStream());// 發送請求參數out.print(param);// flush輸出流的緩沖out.flush();}int responseCode = ((HttpURLConnection) conn).getResponseCode();if ( responseCode == 200 ){// 定義BufferedReader輸入流來讀取URL的響應in = new BufferedReader(new InputStreamReader(conn.getInputStream()));String line;while ((line = in.readLine()) != null) {result += line;}if ( callback != null ){callback.ok( result );}if ( httpCall != null ){httpCall.setHttpQuestType( ResponseType.OK );httpCall.setResult( result );}}else {if ( callback != null ){callback.fail();}if ( httpCall != null ){httpCall.setHttpQuestType( ResponseType.FAIL );httpCall.setResult( "" );}}} catch (Exception e) {if ( callback != null ){callback.timeOut();}if ( httpCall != null ){httpCall.setHttpQuestType( ResponseType.TIMEOUT );httpCall.setResult( "" );}e.printStackTrace();}finally {try {if (out != null) {out.close();}if (in != null) {in.close();}} catch (IOException ex) {ex.printStackTrace();}if (conn != null) {if (url.toLowerCase().startsWith("https")) {((HttpsURLConnection) conn).disconnect();} else {((HttpURLConnection) conn).disconnect();}}return httpCall ;}}public interface Callback{void ok(String result) ;void fail();void timeOut() ;}}

  

? ? ? 6.4 如何使用

private void httpGet(){//get 用法1new Thread(new Runnable() {@Overridepublic void run() {HttpUtil.doGet("http://www.baidu.com", new HttpUtil.Callback() {@Overridepublic void ok(String result) {//請求成功}@Overridepublic void fail() {//請求失敗}@Overridepublic void timeOut() {//請求超時}}) ;}}) ;//get 用法2new Thread(new Runnable() {@Overridepublic void run() {HttpCall httpCall = HttpUtil.doGet( "http://www.baidu.com" , null ) ;switch ( httpCall.getHttpQuestType() ){case OK://請求成功break;case FAIL://請求失敗break;case TIMEOUT://請求超時break;}}}) ;}private void httpPost(){//post 用法1new Thread(new Runnable() {@Overridepublic void run() {HttpUtil.doPost("http://www.baidu.com", "post請求參數" , new HttpUtil.Callback() {@Overridepublic void ok(String result) {//請求成功}@Overridepublic void fail() {//請求失敗}@Overridepublic void timeOut() {//請求超時}}) ;}}) ;//post 用法2new Thread(new Runnable() {@Overridepublic void run() {HttpCall httpCall = HttpUtil.doPost( "http://www.baidu.com" , "post請求參數" , null ) ;switch ( httpCall.getHttpQuestType() ){case OK://請求成功break;case FAIL://請求失敗break;case TIMEOUT://請求超時break;}}}) ;}

  

7、http下載圖片并且壓縮bitmap  

//從網絡下載bitmapprivate Bitmap downLoadBitmapFromNet( String urlString ){InputStream inputStream = null ;HttpURLConnection conn = null ;Bitmap bitmap ;try {URL url = new URL( urlString ) ;conn = (HttpURLConnection) url.openConnection();inputStream = new BufferedInputStream( conn.getInputStream() ) ;inputStream.mark( inputStream.available() );//壓縮圖片BitmapFactory.Options options = new BitmapFactory.Options() ;options.inJustDecodeBounds = true ;BitmapFactory.decodeStream( inputStream , null , options ) ;options.inSampleSize = 4 ;options.inPreferredConfig = Bitmap.Config.RGB_565 ;options.inJustDecodeBounds = false ;inputStream.reset();bitmap = BitmapFactory.decodeStream( inputStream , null , options ) ;return bitmap ;} catch (MalformedURLException e) {e.printStackTrace();}catch (IOException e) {e.printStackTrace();}finally {if ( inputStream != null ){try {inputStream.close();} catch (IOException e) {e.printStackTrace();}}if ( conn != null ){conn.disconnect();}}return null ;}

  需要注意的事項

1、并不是所有的inputStream都支持mark()方法的, 因為這里的 InputStream其實是?BufferedInputStream 。?BufferedInputStream 繼承 InputStream 并且重寫了里面的?mark() 、reset() 、markSupported()

?

總結

以上是生活随笔為你收集整理的Android http 的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 亚洲欧美国产精品久久久久久久 | 成人一二区 | 欧美成人91 | 国产成人精品免费在线观看 | 欧美一区高清 | 日韩av毛片在线观看 | 久热精品视频 | 欧美激情视频在线观看 | 91福利在线视频 | 亚洲天堂网络 | 日韩精品久久久久久久的张开腿让 | 欧美成人久久久免费播放 | 黄色片的网站 | 98国产视频 | 九九热精品在线视频 | 亚洲精品视频一区二区 | 探花视频在线版播放免费观看 | 日本888xxxx | 亚洲国产一级 | 成人影院免费 | 欧美人妖乱大交 | 免费视频a | 国产二区视频在线观看 | 狠狠操精品| 国产中文字幕一区二区 | 欧美肥老妇视频 | 麻豆精品91 | 久久久久久久久免费看无码 | 竹菊影视一区二区三区 | 祥仔视觉av | 日本少妇在线 | 欧美福利视频 | 午夜免费观看 | 91日韩在线 | a级在线观看 | 91香蕉视频污污 | 91精品久久久久 | 91麻豆精品91久久久久同性 | 国产毛片毛片毛片毛片 | 成人毛片100部免费看 | 国产一区不卡在线 | 国产www精品| 亚洲综合a| 少妇裸体视频 | 老熟女高潮一区二区三区 | 国产一区在线免费 | 亚洲理论在线 | 男女视频在线免费观看 | 人人九九精品 | 国产又爽又色 | 精品国产综合区久久久久久 | 超碰老司机 | 99午夜视频 | 午夜精品久久久久久久爽 | 欧美婷婷精品激情 | 一区二区美女 | 亚洲欧洲日韩在线 | 久久无码高潮喷水 | 久久精品蜜桃 | 午夜福利理论片在线观看 | 日b视频免费 | 欧美精品久久久久久久久久 | 国产任你操 | 中文字幕一区二区三区人妻电影 | 色婷婷综合网 | 性欧美大战久久久久久久久 | 国产精品免费一区二区三区都可以 | 欧美激情综合五月色丁香 | 亚洲欧洲一区二区在线观看 | 亚洲综合第一页 | 天天射天天草 | 亚洲天堂av线 | 国产农村妇女毛片精品 | 综合爱爱网 | 欧洲成人免费视频 | 亚洲天天干 | 国产福利免费视频 | 91们嫩草伦理 | 91精品国产91久久久久久吃药 | 国产精品亚洲AV色欲三区不卡 | 国产精品国产精品国产专区蜜臀ah | va欧美 | 美女黄色一级视频 | 日日操av| 亚洲免费av网站 | av久久久久久 | 欧美激情免费看 | 中文字幕日韩电影 | 欧美性视频一区二区 | 国产亚洲自拍一区 | 国产哺乳奶水91在线播放 | 国产91久久精品一区二区 | 欧美一区二区三区精品 | 国产午夜精品一区二区三区嫩草 | 中文国产视频 | 手机看片日韩 | 香蕉色视频 | 99久久婷婷国产综合精品草原 | 日韩在线视频一区二区三区 |