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