Android网络编程使用HttpClient访问web站点
生活随笔
收集整理的這篇文章主要介紹了
Android网络编程使用HttpClient访问web站点
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
HttpClientDemo.java界面就是兩個(gè)按鈕和一個(gè)文本框
/** 用HttpClientlai 來(lái)訪問(wèn)提交請(qǐng)求,接收響應(yīng)* A,發(fā)送GET請(qǐng)求* 1,創(chuàng)建HttpClient對(duì)象;HttpClient httpclient=new DefaultHttpClient();* 2,發(fā)送GET請(qǐng)求,創(chuàng)建HttpGet對(duì)象:HttpGet httpget=new HttpGet("http://www.baidu.com");* 3,用HttpClient對(duì)象實(shí)行HttpGet對(duì)象會(huì)得到服務(wù)器響應(yīng)對(duì)象HttpResponse的對(duì)象,響應(yīng)就封裝在HttpResponse中:* HttpResponse httpresponse=httpclient.execute(httpget);* 4,從httpresponse響應(yīng)中獲得Http實(shí)例HttpEntity entity=httpresponse.getEntity(); * */ public class HttpClientDemo extends Activity {TextView response;//聲明HttpClient對(duì)象HttpClient httpclient;Handler handler=new Handler(){public void handleMessage(Message msg){if(msg.what==0x123){// 使用response顯示服務(wù)器的響應(yīng)response.append(msg.obj.toString()+"\n");}}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_http_client);//1,創(chuàng)建DefaultHttpClient對(duì)象,接口回調(diào)HttpClient是個(gè)接口httpclient=new DefaultHttpClient();response=(TextView) findViewById(R.id.response);}/** 向服務(wù)發(fā)送GET請(qǐng)求流程* * */public void accessSecret(View v){response.setText("");//點(diǎn)擊按鈕,開啟線程,在線程中發(fā)送Get請(qǐng)求new Thread(){public void run(){//2,創(chuàng)建一個(gè)HttpGet對(duì)象HttpGet httpget=new HttpGet("http://localhost:8080/foo/secret.jsp");//jsp部署在To嗎cat服務(wù)器上try {//3,用HttpClient對(duì)象實(shí)行HttpGet對(duì)象會(huì)得到服務(wù)器響應(yīng)對(duì)象HttpResponse的對(duì)象,響應(yīng)就封裝在HttpResponse中HttpResponse httpresponse=httpclient.execute(httpget);//4,從httpresponse響應(yīng)中獲得Http實(shí)例HttpEntity entity=httpresponse.getEntity();if(entity!=null){//5,entity實(shí)例中獲得內(nèi)容,建立輸入流,讀取服務(wù)器內(nèi)容BufferedReader br=new BufferedReader(new InputStreamReader(entity.getContent()));String line=null;while((line=br.readLine())!=null){//循環(huán)從輸入流中讀取內(nèi)容Message msg=new Message();msg.what=0x123;msg.obj=line;handler.sendMessage(msg);//發(fā)給UI線程更新UI組件}}} catch (ClientProtocolException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}.start();}/** 發(fā)送Post請(qǐng)求流程* * * */public void showLogin(View v){final View loginDialog=getLayoutInflater().inflate(R.layout.login, null);new AlertDialog.Builder(HttpClientDemo.this).setTitle("登錄系統(tǒng)").setView(loginDialog).setPositiveButton("確定", new DialogInterface.OnClickListener(){@Overridepublic void onClick(DialogInterface dialog, int which) {// 獲取對(duì)話框的用戶名和密碼final String name=((EditText)loginDialog.findViewById(R.id.name)).getText().toString();final String pass=((EditText)loginDialog.findViewById(R.id.pass)).getText().toString();//點(diǎn)擊確定,開啟線程,在線程中發(fā)送Post請(qǐng)求new Thread(){public void run(){ try {//2,創(chuàng)建HttpPost對(duì)象HttpPost httppost=new HttpPost("http://localhost:8080/foo/login.jsp");//jsp部署在To嗎cat服務(wù)器上//3,對(duì)傳遞的參數(shù)進(jìn)行封裝,NameValuePair是簡(jiǎn)單名稱值對(duì)節(jié)點(diǎn)類型List<NameValuePair> params=new ArrayList<NameValuePair>();params.add(new BasicNameValuePair("name",name));//添加參數(shù)params.add(new BasicNameValuePair("pass",pass));//3,設(shè)置編碼httppost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));//4,HttpClient對(duì)象執(zhí)行HttpPost請(qǐng)求,獲得相應(yīng)HttpResponse httpresponse=httpclient.execute(httppost);//5,如果狀態(tài)碼是200就表示服務(wù)器成功相應(yīng)if(httpresponse.getStatusLine().getStatusCode()==200){//200:響應(yīng)成功,301/302:重定向,404:not found未找到資源 ,501服務(wù)器遇到錯(cuò)誤,使其無(wú)法對(duì)請(qǐng)求提供服務(wù)String msg = EntityUtils.toString(httpresponse.getEntity()); Looper.prepare();//提示登錄成功Toast.makeText(HttpClientDemo.this, msg, Toast.LENGTH_LONG).show();Looper.loop();}} catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} }}.start();}}).setNegativeButton("取消", null).show();}
總結(jié)
以上是生活随笔為你收集整理的Android网络编程使用HttpClient访问web站点的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Eclipse真机测试注意事项
- 下一篇: android sina oauth2.