1.HttpClientåHttpURLConnectionçåºå«
HttpClientåHttpURLConnectionçåºå«
android 源码架构æ»ç»äºç½ä¸çä¸äºèµæºï¼ä¸»è¦æ以ä¸ä¸¤ä¸ªè§ç¹ï¼
android 源码架构åæä¸ï¼
android 源码架构å¨ç 究Volleyæ¡æ¶çæºç ä¸ï¼åç°å®å¨HTTP请æ±ç使ç¨ä¸æ¯è¾æææï¼å¨Android 2.3å以ä¸çæ¬ï¼ä½¿ç¨çæ¯HttpURLConnectionï¼èå¨Android 2.2å以ä¸çæ¬ï¼ä½¿ç¨çæ¯HttpClientãæä¹æ¯è¾å¥½å¥è¿ä¹ä½¿ç¨çåå ï¼äºæ¯ä¸é¨æ¾å°äºä¸ä½Googleçå·¥ç¨å¸åçä¸ç¯å客ï¼æä¸å¯¹HttpURLConnectionåHttpClientè¿è¡äºå¯¹æ¯ï¼ä¸é¢æå°±ç»å¤§å®¶ç®è¦å°ç¿»è¯ä¸ä¸ã
android 源码架构大å¤æ°çAndroidåºç¨ç¨åºé½ä¼ä½¿ç¨HTTPåè®®æ¥åéåæ¥æ¶ç½ç»æ°æ®ï¼èAndroidä¸ä¸»è¦æä¾äºä¸¤ç§æ¹å¼æ¥è¿è¡HTTPæä½ï¼HttpURLConnectionåHttpClientãè¿ä¸¤ç§æ¹å¼é½æ¯æHTTPSåè®®ã以æµçå½¢å¼è¿è¡ä¸ä¼ åä¸è½½ãé ç½®è¶ æ¶æ¶é´ãIPv6ã以åè¿æ¥æ± çåè½ã
android 源码架构HttpClient:
android 源码架构DefaultHttpClientåå®çå å¼AndroidHttpClienté½æ¯HttpClientå ·ä½çå®ç°ç±»ï¼å®ä»¬é½æ¥æä¼å¤çAPIï¼èä¸å®ç°æ¯è¾ç¨³å®ï¼bugæ°éä¹å¾å°ã
android 源码架构ä½åæ¶ä¹ç±äºHttpClientçAPIæ°éè¿å¤ï¼ä½¿å¾æ们å¾é¾å¨ä¸ç ´åå ¼å®¹æ§çæ åµä¸å¯¹å®è¿è¡å级åæ©å±ï¼æ以ç®åAndroidå¢éå¨æååä¼åHttpClientæ¹é¢çå·¥ä½æ度并ä¸ç§¯æã
android 源码架构HttpURLConnection:
android 源码架构HttpURLConnectionæ¯ä¸ç§å¤ç¨éãè½»éæçHTTP客æ·ç«¯ï¼ä½¿ç¨å®æ¥è¿è¡HTTPæä½å¯ä»¥éç¨äºå¤§å¤æ°çåºç¨ç¨åºãè½ç¶HttpURLConnectionçAPIæä¾çæ¯è¾ç®åï¼ä½æ¯åæ¶è¿ä¹ä½¿å¾æ们å¯ä»¥æ´å 容æå°å»ä½¿ç¨åæ©å±å®ã
android 源码架构ä¸è¿å¨Android 2.2çæ¬ä¹åï¼HttpURLConnectionä¸ç´åå¨çä¸äºä»¤äººåç¦çbugãæ¯å¦è¯´å¯¹ä¸ä¸ªå¯è¯»çInputStreamè°ç¨close()æ¹æ³æ¶ï¼å°±æå¯è½ä¼å¯¼è´è¿æ¥æ± 失æäºãé£ä¹æ们é常ç解å³åæ³å°±æ¯ç´æ¥ç¦ç¨æè¿æ¥æ± çåè½ï¼
android 源码架构[java] view plaincopy
android 源码架构[java] view plaincopy
android 源码架构private void disableConnectionReuseIfNecessary() {
android 源码架构// è¿æ¯ä¸ä¸ª2.2çæ¬ä¹åçbug
android 源码架构if (Integer.parseInt(Build.VERSION.SDK) < Build.VERSION_CODES.FROYO) {
android 源码架构System.setProperty(". ä¸ä½¿ç¨å¾æå¤ãæéè¦çåè®®äºï¼è¶æ¥è¶å¤ç Java åºç¨ç¨åºéè¦ç´æ¥éè¿ HTTP åè®®æ¥è®¿é®ç½ç»èµæºãå¨ JDK ç java.net å ä¸å·²ç»æä¾äºè®¿é® HTTP åè®®çåºæ¬åè½ï¼HttpURLConnectionã
android 源码架构HttpURLConnectionæ¯javaçæ åç±»ï¼HttpURLConnection继æ¿èªURLConnectionï¼å¯ç¨äºåæå®ç½ç«åéGET请æ±ãPOST请æ±ãå®å¨URLConnectionçåºç¡ä¸æä¾äºå¦ä¸ä¾¿æ·çæ¹æ³ï¼
android 源码架构int getResponseCode()ï¼è·åæå¡å¨çååºä»£ç ã
android 源码架构String getResponseMessage()ï¼è·åæå¡å¨çååºæ¶æ¯ã
android 源码架构String getResponseMethod()ï¼è·ååé请æ±çæ¹æ³ã
android 源码架构void setRequestMethod(String method)ï¼è®¾ç½®åé请æ±çæ¹æ³ã
android 源码架构å¨ä¸è¬æ åµä¸ï¼å¦æåªæ¯éè¦Webç«ç¹çæ个ç®å页é¢æ交请æ±å¹¶è·åæå¡å¨ååºï¼HttpURLConnectionå®å ¨å¯ä»¥èä»»ãä½å¨ç»å¤§é¨åæ åµä¸ï¼Webç«ç¹çç½é¡µå¯è½æ²¡è¿ä¹ç®åï¼è¿äºé¡µé¢å¹¶ä¸æ¯éè¿ä¸ä¸ªç®åçURLå°±å¯è®¿é®çï¼å¯è½éè¦ç¨æ·ç»å½èä¸å ·æç¸åºçæéæå¯è®¿é®è¯¥é¡µé¢ãå¨è¿ç§æ åµä¸ï¼å°±éè¦æ¶åSessionãCookieçå¤çäºï¼å¦ææç®ä½¿ç¨HttpURLConnectionæ¥å¤çè¿äºç»èï¼å½ç¶ä¹æ¯å¯è½å®ç°çï¼åªæ¯å¤çèµ·æ¥é¾åº¦å°±å¤§äºã
android 源码架构为äºæ´å¥½å°å¤çåWebç«ç¹è¯·æ±ï¼å æ¬å¤çSessionãCookieçç»èé®é¢ï¼Apacheå¼æºç»ç»æä¾äºä¸ä¸ªHttpClient项ç®ï¼çå®çå称就ç¥éï¼å®æ¯ä¸ä¸ªç®åçHTTP客æ·ç«¯ï¼å¹¶ä¸æ¯æµè§å¨ï¼ï¼å¯ä»¥ç¨äºåéHTTP请æ±ï¼æ¥æ¶HTTPååºãä½ä¸ä¼ç¼åæå¡å¨çååºï¼ä¸è½æ§è¡HTML页é¢ä¸åµå ¥çJavascript代ç ï¼ä¹ä¸ä¼å¯¹é¡µé¢å 容è¿è¡ä»»ä½è§£æãå¤çã
android 源码架构ç®åæ¥è¯´ï¼HttpClientå°±æ¯ä¸ä¸ªå¢å¼ºççHttpURLConnectionï¼HttpURLConnectionå¯ä»¥åçäºæ HttpClientå ¨é¨å¯ä»¥åï¼HttpURLConnection没ææä¾çæäºåè½ï¼HttpClientä¹æä¾äºï¼ä½å®åªæ¯å ³æ³¨äºå¦ä½åé请æ±ãæ¥æ¶
android 源码架构ååºï¼ä»¥å管çHTTPè¿æ¥ã
android 源码架构使ç¨HttpClientåé请æ±ãæ¥æ¶ååºå¾ç®åï¼åªè¦å¦ä¸å æ¥å³å¯ã
android 源码架构å建HttpClient对象ã
android 源码架构å¦æéè¦åéGET请æ±ï¼å建HttpGet对象ï¼å¦æéè¦åéPOST请æ±ï¼å建HttpPost对象ã
android 源码架构å¦æéè¦åé请æ±åæ°ï¼å¯è°ç¨HttpGetãHttpPostå ±åçsetParams(HetpParams params)æ¹æ³æ¥æ·»å 请æ±åæ°ï¼å¯¹äºHttpPost对象èè¨ï¼ä¹å¯è°ç¨setEntity(HttpEntity entity)æ¹æ³æ¥è®¾ç½®è¯·æ±åæ°ã
android 源码架构è°ç¨HttpClient对象çexecute(HttpUriRequest request)åé请æ±ï¼æ§è¡è¯¥æ¹æ³è¿åä¸ä¸ªHttpResponseã
android 源码架构è°ç¨HttpResponseçgetAllHeaders()ãgetHeaders(String name)çæ¹æ³å¯è·åæå¡å¨çååºå¤´ï¼è°ç¨HttpResponseçgetEntity()æ¹æ³å¯è·åHttpEntity对象ï¼è¯¥å¯¹è±¡å è£ äºæå¡å¨çååºå 容ãç¨åºå¯éè¿è¯¥å¯¹è±¡è·åæå¡å¨çååºå 容ã
android 源码架构å¦å¤ï¼Androidå·²ç»æåå°éæäºHttpClientï¼è¿æå³çå¼å人åå¯ä»¥ç´æ¥å¨Androidåºç¨ä¸ä½¿ç¨Httpclientæ¥è®¿é®æ交请æ±ãæ¥æ¶ååºã
android 源码架构æ¯å¦ä¸ä¸ªAndroidåºç¨éè¦åæå®é¡µé¢åé请æ±ï¼ä½è¯¥é¡µé¢å¹¶ä¸æ¯ä¸ä¸ªç®åç页é¢ï¼åªæå½ç¨æ·å·²ç»ç»å½ï¼èä¸ç»å½ç¨æ·çç¨æ·åæææ¶æå¯è®¿é®è¯¥é¡µé¢ãå¦æ使ç¨HttpURLConnectionæ¥è®¿é®è¿ä¸ªè¢«ä¿æ¤ç页é¢ï¼é£ä¹éè¦å¤ççç»è就太å¤æäºã
android 源码架构å ¶å®è®¿é®Webåºç¨ä¸è¢«ä¿æ¤ç页é¢ï¼ä½¿ç¨æµè§å¨åååç®åï¼ç¨æ·éè¿ç³»ç»æä¾çç»å½é¡µé¢ç»å½ç³»ç»ï¼æµè§å¨ä¼è´è´£ç»´æ¤ä¸æå¡å¨ä¹é´çSesionï¼å¦æç¨æ·ç»å½çç¨æ·åãå¯ç 符åè¦æ±ï¼å°±å¯ä»¥è®¿é®è¢«ä¿æ¤èµæºäºã
android 源码架构å¨Androidåºç¨ç¨åºä¸ï¼åå¯ä½¿ç¨HttpClientæ¥ç»å½ç³»ç»ï¼åªè¦åºç¨ç¨åºä½¿ç¨åä¸ä¸ªHttpClientåé请æ±ï¼HttpClientä¼èªå¨ç»´æ¤ä¸æå¡å¨ä¹é´çSessionç¶æï¼ä¹å°±æ¯è¯´ç¨åºç¬¬ä¸æ¬¡ä½¿ç¨HttpClientç»å½ç³»ç»åï¼æ¥ä¸æ¥ä½¿ç¨HttpClientå³å¯è®¿é®è¢«ä¿æ¤é¡µèäºã
android 源码架构2024-11-19 02:33
2024-11-19 02:30
2024-11-19 02:00
2024-11-19 01:42
2024-11-19 01:27
2024-11-19 00:44
2024-11-19 00:33
2024-11-19 00:02