使用Http协议访问网络--HttpClient
生活随笔
收集整理的這篇文章主要介紹了
使用Http协议访问网络--HttpClient
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
public interface
HttpClient是Apache提供的Http網(wǎng)絡(luò)訪問(wèn)接口。 1、創(chuàng)建HttpClient實(shí)例 HttpClient是一個(gè)接口,無(wú)法直接創(chuàng)建實(shí)例,通常創(chuàng)建一個(gè)DefaultHttpClient(HttpClient 的SubClass)實(shí)例。
HttpClient httpClient = new DefaultHttpClient();
2、如果想要發(fā)起一條GET請(qǐng)求 ,可以創(chuàng)建一個(gè)HttpGet對(duì)象,傳入目標(biāo)地址,之后調(diào)用HttpClient的execute()方法
HttpGet = new HttpClient("http://www.baidu.com"); httpClient.execute(httpGet);
3、執(zhí)行execute()后,會(huì)返回一個(gè)HttpResponse對(duì)象,服務(wù)器返回的所有信息都在這里面,通常都會(huì)先取出服務(wù)器返回的狀態(tài)嗎,若等于200則說(shuō)明請(qǐng)求和相應(yīng)都成功了
if(httpResponse.getStatusLine().getStatusCode() == 200){ }
接下來(lái)在這個(gè)if判斷內(nèi)部取出服務(wù)返回的具體內(nèi)容,可以調(diào)用httpResponse getEntity()獲得一個(gè)HttpEntity實(shí)例,再調(diào)用EntityUtils.toString這個(gè)靜態(tài)方法將HttpEntity轉(zhuǎn)換成字符串
HttpEntity httpEntity = httpResponse.getEntity(); String responseString = EntityUtils.toString(httpEntity,"utf-8");
由于在子線程中無(wú)法對(duì)UI操作,最后使用Message,將服務(wù)器返回的數(shù)據(jù)放到Message對(duì)象中,用Handler將messge發(fā)送出去,在主線程進(jìn)行UI操作
//將服務(wù)器返回的結(jié)果存放到Message對(duì)象中 Message message = Message.obtain(mHandler, SEND_REQUEST, responseString); mHandler.sendMessage(message);
Handler mHandler = new Handler(){@Overridepublic void handleMessage(Message msg){if(msg.what == SEND_REQUEST){String response = (String)msg.obj;responseTextView.setText(response);}}};
主要代碼: public class MainActivity extends ActionBarActivity implements OnClickListener{private Button sendRequestBtn;private TextView responseTextView;private int SEND_REQUEST = 0;Handler mHandler = new Handler(){@Overridepublic void handleMessage(Message msg){if(msg.what == SEND_REQUEST){String response = (String)msg.obj;responseTextView.setText(response);}}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);sendRequestBtn = (Button)findViewById(R.id.requestBtn);responseTextView = (TextView)findViewById(R.id.responseText);sendRequestBtn.setOnClickListener(this);}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif(v.getId() == R.id.requestBtn){sendRequestWithHttpClient();}}private void sendRequestWithHttpClient(){new Thread(new Runnable(){@Overridepublic void run(){try{HttpClient httpClient = new DefaultHttpClient();HttpGet httpGet= new HttpGet("http://www.baidu.com");HttpResponse httpResponse = httpClient.execute(httpGet);if(httpResponse.getStatusLine().getStatusCode() == 200){//獲得消息的響應(yīng)實(shí)體HttpEntity httpEntity = httpResponse.getEntity();String responseString = EntityUtils.toString(httpEntity,"utf-8");//將服務(wù)器返回的結(jié)果存放到Message對(duì)象中Message message = Message.obtain(mHandler, SEND_REQUEST, responseString);mHandler.sendMessage(message);}}catch(Exception e){e.printStackTrace();}}}).start();}}
?完整代碼在:https://github.com/HiSunny/ComeOnHttpClient.git
HttpClient
| org.apache.http.client.HttpClient |
HttpClient是Apache提供的Http網(wǎng)絡(luò)訪問(wèn)接口。 1、創(chuàng)建HttpClient實(shí)例 HttpClient是一個(gè)接口,無(wú)法直接創(chuàng)建實(shí)例,通常創(chuàng)建一個(gè)DefaultHttpClient(HttpClient 的SubClass)實(shí)例。
HttpClient httpClient = new DefaultHttpClient();
2、如果想要發(fā)起一條GET請(qǐng)求 ,可以創(chuàng)建一個(gè)HttpGet對(duì)象,傳入目標(biāo)地址,之后調(diào)用HttpClient的execute()方法
HttpGet = new HttpClient("http://www.baidu.com"); httpClient.execute(httpGet);
3、執(zhí)行execute()后,會(huì)返回一個(gè)HttpResponse對(duì)象,服務(wù)器返回的所有信息都在這里面,通常都會(huì)先取出服務(wù)器返回的狀態(tài)嗎,若等于200則說(shuō)明請(qǐng)求和相應(yīng)都成功了
if(httpResponse.getStatusLine().getStatusCode() == 200){ }
接下來(lái)在這個(gè)if判斷內(nèi)部取出服務(wù)返回的具體內(nèi)容,可以調(diào)用httpResponse getEntity()獲得一個(gè)HttpEntity實(shí)例,再調(diào)用EntityUtils.toString這個(gè)靜態(tài)方法將HttpEntity轉(zhuǎn)換成字符串
HttpEntity httpEntity = httpResponse.getEntity(); String responseString = EntityUtils.toString(httpEntity,"utf-8");
由于在子線程中無(wú)法對(duì)UI操作,最后使用Message,將服務(wù)器返回的數(shù)據(jù)放到Message對(duì)象中,用Handler將messge發(fā)送出去,在主線程進(jìn)行UI操作
//將服務(wù)器返回的結(jié)果存放到Message對(duì)象中 Message message = Message.obtain(mHandler, SEND_REQUEST, responseString); mHandler.sendMessage(message);
Handler mHandler = new Handler(){@Overridepublic void handleMessage(Message msg){if(msg.what == SEND_REQUEST){String response = (String)msg.obj;responseTextView.setText(response);}}};
主要代碼: public class MainActivity extends ActionBarActivity implements OnClickListener{private Button sendRequestBtn;private TextView responseTextView;private int SEND_REQUEST = 0;Handler mHandler = new Handler(){@Overridepublic void handleMessage(Message msg){if(msg.what == SEND_REQUEST){String response = (String)msg.obj;responseTextView.setText(response);}}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);sendRequestBtn = (Button)findViewById(R.id.requestBtn);responseTextView = (TextView)findViewById(R.id.responseText);sendRequestBtn.setOnClickListener(this);}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif(v.getId() == R.id.requestBtn){sendRequestWithHttpClient();}}private void sendRequestWithHttpClient(){new Thread(new Runnable(){@Overridepublic void run(){try{HttpClient httpClient = new DefaultHttpClient();HttpGet httpGet= new HttpGet("http://www.baidu.com");HttpResponse httpResponse = httpClient.execute(httpGet);if(httpResponse.getStatusLine().getStatusCode() == 200){//獲得消息的響應(yīng)實(shí)體HttpEntity httpEntity = httpResponse.getEntity();String responseString = EntityUtils.toString(httpEntity,"utf-8");//將服務(wù)器返回的結(jié)果存放到Message對(duì)象中Message message = Message.obtain(mHandler, SEND_REQUEST, responseString);mHandler.sendMessage(message);}}catch(Exception e){e.printStackTrace();}}}).start();}}
?完整代碼在:https://github.com/HiSunny/ComeOnHttpClient.git
總結(jié)
以上是生活随笔為你收集整理的使用Http协议访问网络--HttpClient的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 微信游戏中心电脑版(微信游戏首页)
- 下一篇: 最可怕的十大电脑病毒(十大最危险的电脑病