日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當(dāng)前位置:首頁(yè) > 芯聞號(hào) > 充電吧
[導(dǎo)讀]public abstract class HttpURLConnection extends URLConnectionjava.lang.Object ???? java.net.URLCon

public abstract class HttpURLConnection extends URLConnection
java.lang.Object ???? java.net.URLConnection ? ???? java.net.HttpURLConnection
1、獲取HttpURLConnection實(shí)例
HttpURLConnection(URL url)Constructs a new HttpURLConnection instance pointing to the resource specified by theurl.
HttpURLConnection是abstract的,不能new出實(shí)例。一般new出一個(gè)URL對(duì)象并傳入目標(biāo)網(wǎng)絡(luò)地址,然后調(diào)用openConnection()方法。

URL?url?=?new?URL("http://www.baidu.com");
//public?URLConnection?openConnection?()?
//Returns?a?new?connection?to?the?resource?referred?to?by?this?URL.
//向url發(fā)出HTTP請(qǐng)求的HttpURLConnection實(shí)例
//Obtain?a?new?HttpURLConnection?by?calling?URL.openConnection()?and?casting?the?result?to?HttpURLConnection
connection?=??(HttpURLConnection)url.openConnection();

2、對(duì)HttpURLConnection實(shí)例設(shè)置HTTP請(qǐng)求所使用的方法,連接超時(shí)等

connection.setRequestMethod("GET");
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);

3、調(diào)用connection.getInputStream()獲得到服務(wù)器返回的輸入流(連接發(fā)生),對(duì)輸入流讀取

//發(fā)生連接(在其之前設(shè)置好參數(shù))
InputStream?in?=?connection.getInputStream();//同url.openStream();可以不用connection
//java.io.BufferedReader.BufferedReader(Reader?in)
//Constructs?a?new?BufferedReader,?providing?in?with?a?buffer?of?8192?characters.
//public?class?BufferedReader?extends?Reader
BufferedReader?reader?=?new?BufferedReader(new?InputStreamReader(in));
StringBuilder?responseStringBuilder?=?new?StringBuilder();
String?lineStr?;
while((lineStr?=?reader.readLine())!=?null){
	responseStringBuilder.append(lineStr);
					}

Android4.0后,網(wǎng)絡(luò)操作要在子線(xiàn)程中進(jìn)行。 采用Handler、Message發(fā)送消息。 ?

private?Handler?handler?=?new?Handler(){
		public?void?handleMessage(Message?msg){
			switch(msg.what){
			case?SHOW_RESPONSE:
				String?response?=?(String)msg.obj;
				responseTextView.setText(response);
				break;
			default:
				break;
			
			}
		}
	};
Message?message?=?Message.obtain(mHandler,?SEND_REQUEST,?responseString);
mHandler.sendMessage(message);


訪(fǎng)問(wèn)百度首頁(yè):


Protected Constructors
本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專(zhuān)欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除( 郵箱:macysun@21ic.com )。
換一批
延伸閱讀
關(guān)閉