android寮€鍙戠瑪璁颁箣缃戠粶缂栫▼鈥斾娇鐢═CP鍗忚鍜孶RL杩涜缃戠粶缂栫▼
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android寮€鍙戠瑪璁颁箣缃戠粶缂栫▼鈥斾娇鐢═CP鍗忚鍜孶RL杩涜缃戠粶缂栫▼相关的知识,希望对你有一定的参考价值。
鏍囩锛?/p>
鎵嬫満鏈韩鏄綔涓烘墜鏈虹粓绔娇鐢ㄧ殑锛屽洜姝ゅ畠鐨勮绠楄兘鍔涳紝瀛樺偍鑳藉姏閮芥槸鏈夐檺鐨勩€傚畠鐨勪富瑕佷紭鍔挎槸鎼哄甫鏂逛究锛屽彲浠ラ殢鏃舵墦寮€锛岃€屼笖鎵嬫満閫氬父鎬绘槸澶勪簬鑱旂綉鐘舵€併€傚洜姝ょ綉缁滄敮鎸佸浜庢墜鏈哄簲鐢ㄩ潪甯搁噸瑕併€?/p>
android瀹屽叏鏀寔JDK鏈韩鐨凾CP锛孶DP缃戠粶閫氫俊API锛屼篃鍙互浣跨敤ServerSocket,Socket鏉ュ缓绔嬪熀浜嶵CP/IP鍗忚鐨勭綉缁滈€氫俊锛屼篃鍙互浣跨敤DatagramSocket,Datagrampacket鏉ュ缓绔嬪熀浜嶶DP鍗忚鐨勭綉缁滈€氫俊銆?
Android涔熸敮鎸丣DK鎻愪緵鐨刄RL锛孶RLConnection绛夌綉缁滈€氫俊API銆?
Android杩樺唴缃簡HttpClient锛屽拰HttpUrlConnection杩欐牱鍙互闈炲父鏂逛究鍦板彂閫丠TTP璇锋眰,骞惰幏鍙朒ttp鍝嶅簲銆?
鈥斺€斿熀浜嶵CP鍗忚鐨勭紪绋?/strong>
浣跨敤Socket,ServerSocket缂栫▼:
浣跨敤澶氱嚎绋嬶紝handler鏈哄埗瀹屾垚缃戠粶缂栫▼.
娉ㄦ剰锛氱敱浜庢墜鏈烘棤绾夸笂缃戠殑IP鍦板潃閫氬父閮芥槸鐢辩Щ鍔ㄨ繍钀ュ叕鍙稿姩鎬佸垎閰嶇殑锛屼竴鑸笉浼氭湁鑷繁鍥哄畾鐨処P鍦板潃锛屽洜姝ゅ緢灏戝湪鎵嬫満涓婅繍琛屾湇鍔″櫒绔紝鏈嶅姟鍣ㄧ閫氬父杩愯鍦ㄦ湁鍥哄畾IP鐨勬湇鍔″櫒涓娿€?
鈶犺窡缃戠粶鏈夊叧鐨勫氨瑕佸姞Internet鏉冮檺
鈶$綉缁滄暟鎹殑璇锋眰瑕佸湪瀛愮嚎绋嬩腑杩涜
鈶㈡洿鏂癠I闇€瑕佸€熷姪Handler鏈哄埗
鈶g紪鐮侀棶棰橈細Android榛樿缂栫爜涓篣TF-8 Java榛樿缂栫爜涓篏B2312
鈶ゆ湰鏈篒P鍦板潃闂锛氫娇鐢?27.0.0.1鍦ㄦā鎷熶腑涓嶈兘璁块棶鍒版湰鍦扮殑鏈嶅姟鍣紝鍥犱负妯℃嫙鍣ㄤ篃鍦ㄦ湰鏈?
鏈€鍚庢湁浣跨敤Socket,ServerSocket缃戠粶缂栫▼鐨勭ず渚?/p>
鈥斺€斾娇鐢║RL杩涜缃戠粶缂栫▼
**鈥斺€斾娇鐢║RL璁块棶缃戠粶璧勬簮**
URL瀵硅薄浠h〃缁熶竴璧勬簮瀹氫綅绗︺€傚畠鏄寚鍚戜簰鑱旂綉涓殑璧勬簮銆傝璧勬簮鍙互鏄畝鍗曠殑鏂囦欢鎴栫洰褰曘€備篃鍙互鏄鏇村鏉傜殑瀵硅薄鐨勫紩鐢ㄣ€?
URL鐨勬牸寮忥細protocol://host:port/resourceName
鍙互浣跨敤鏋勯€犲櫒鍒涘缓URL瀵硅薄锛屼竴鏃﹁幏寰桿RL瀵硅薄锛屽彲浠ヨ皟鐢ㄥ涓嬬殑鏂规硶璁块棶璇RL瀵瑰簲鐨勮祫婧愶細
String getFile():鑾峰彇URL鐨勮祫婧愬悕
String getHost():鑾峰彇URL鐨勪富鏈哄悕
String getPath():鑾峰彇URL鐨勮矾寰勯儴鍒?
int getPort():鑾峰彇URL鐨勭鍙e彿
String getProtocol():鑾峰彇URL鐨勫崗璁悕绉?
String getQuery()锛氳幏鍙朥RL鐨勬煡璇㈠瓧绗︿覆閮ㄥ垎
InputStream url.openStream():鎵撳紑URL鐨勮繛鎺ワ紝杩斿洖涓€涓鍙栬璧勬簮鐨処nputStream銆?
**鈥斺€斾娇鐢║RLConnection鎻愪氦璇锋眰**
URL鐨刼penConnection()鏂规硶灏嗚繑鍥炰竴涓猆RLConnection锛岃瀵硅薄琛ㄧず搴旂敤绋嬪簭鍜孶RL涔嬮棿鐨勯€氫俊杩炴帴锛岀▼搴忓彲浠ラ€氳繃URLConnection瀹炰緥鍚戣URL鍙戦€佽姹傦紝璇诲彇URL寮曠敤鐨勮祫婧愩€傞€氬父鍒涘缓涓€涓拰URL鐨勮繛鎺ワ紝骞跺彂閫佽姹傦紝璇诲彇姝RL寮曠敤鐨勮祫婧愩€?
闇€瑕佸涓嬫楠わ細
a)閫氳繃璋冪敤URL瀵硅薄openConnection()鏂规硶鏉ュ垱寤篣RLConnection瀵硅薄
b)璁剧疆URLConnection鐨勫弬鏁板拰鏅€氳姹傚睘鎬?/p>
conn.setRequestProperty("accept","*/*");
conn.setRequestProperty("connection","Keep-Alive");
conn.setRequestProperty("user-agent","Mozilla/4.0(compatible;MSIE 6.0;Windows NT 5.1;SV1)");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//鍙戦€丳OST璇锋眰蹇呴』璁剧疆濡備笅涓よ**
conn.setDoInput(true):璁剧疆璇RLConnection鐨刣oInput璇锋眰澶村瓧娈电殑鍊?/strong>
coon.setDoOutput(true):
c)璋冪敤connect():鎵撳紑鍒版URL寮曠敤鐨勮祫婧愮殑閫氫俊閾炬帴锛堝鏋滃皻鏈缓绔嬭繖鏍风殑杩炴帴锛夈€?
濡傛灉鍦ㄥ凡鎵撳紑杩炴帴(姝ゆ椂 connected 瀛楁鐨勫€间负 true)鐨勬儏鍐典笅璋冪敤 connect 鏂规硶锛屽垯蹇界暐璇ヨ皟鐢?
URLConnection 瀵硅薄缁忓巻涓や釜闃舵锛氶鍏堝垱寤哄璞★紝鐒跺悗寤虹珛杩炴帴銆?/strong>
鍦ㄥ垱寤哄璞′箣鍚庯紝寤虹珛杩炴帴涔嬪墠锛屽彲鎸囧畾鍚勭閫夐」(渚嬪doInput鍜孶seCaches).杩炴帴鍚庡啀杩涜璁剧疆灏变細鍙戠敓閿欒銆傝繛鎺ュ悗鎵嶈兘杩涜鐨勬搷浣?渚嬪getContentLength)锛屽鏈夊繀瑕侊紝灏嗛殣寮忔墽琛岃繛鎺?/strong>.
d)濡傛灉鍙槸鍙戦€丟ET鏂瑰紡璇锋眰锛屼娇鐢╟onnect鏂规硶寤虹珛鍜岃繙绋嬭祫婧愪箣闂寸殑瀹為檯杩炴帴鍗冲彲锛屽湪璇锋眰鐨勫湴鍧€涓紶鍏ユ暟鎹€?/p>
濡傛灉闇€瑕佸彂閫丳ost鏂规硶璇锋眰銆傞渶瑕佽幏鍙?
URLConnection瀹炰緥瀵瑰簲鐨勮緭鍑烘祦鏉ュ彂閫佽姹傚弬鏁帮紝
PrintWriter out=new PrintWriter(conn.getOutputStream());
//瑙e喅涔辩爜闂
String n=EncodingUtils.getString("寮犱笁".getBytes(),"UTF-8");
out.write("name="+n+"&pwd="+pwd);
out.flush();//鍒锋柊杈撳嚭娴佺殑缂撳啿
e)杩滅▼璧勬簮鍙樹负鍙敤锛岀▼搴忓彲浠ヨ闂繙绋嬭祫婧愮殑澶村瓧娈垫垨閫氳繃杈撳叆娴佽鍙栬繙绋嬭祫婧愮殑鏁版嵁銆?/p>
getInputStream()鑾峰彇杈撳叆娴併€?
浠庤緭鍏ユ祦璇诲彇response鐨勬暟鎹€?/p>
娉ㄦ剰:
1)濡傛灉鏃㈣浣跨敤杈撳叆娴佽鍙朥RLConnection鍝嶅簲鐨勫唴瀹癸紝涔熻浣跨敤杈撳嚭娴佸彂閫佽姹傚弬鏁帮紝涓€瀹氳鍏堜娇鐢ㄨ緭鍑烘祦锛屽啀浣跨敤杈撳叆娴併€?/p>
2)鍊熷姪浜嶶RLConnection绫荤殑甯姪锛屽簲鐢ㄧ▼搴忓彲浠ラ潪甯告柟渚垮湴涓庢寚瀹氱珯鐐逛氦鎹俊鎭紝鍖呮嫭鍙戦€丟ET璇锋眰锛孭OST璇锋眰锛屽苟鑾峰彇缃戠珯鐨勫搷搴旂瓑銆?/p>
浣跨敤Socket,ServerSocket缂栫▼绀轰緥锛?/p>
鏁堟灉锛?/p>
ServerHuadong涓烘湇鍔″櫒
鍩轰簬TCPIP鍗忚缃戠粶缂栫▼涓哄鎴风
涓嬭浇鍦板潃:http://download.csdn.net
以上是关于android寮€鍙戠瑪璁颁箣缃戠粶缂栫▼鈥斾娇鐢═CP鍗忚鍜孶RL杩涜缃戠粶缂栫▼的主要内容,如果未能解决你的问题,请参考以下文章
鍋囪缃戠粶宸ョ▼甯?2鈥斺€斾娇鐢╒RF绌胯秺MPLS鍩熼€氫俊