Android使用Apache HttpClient發送GET、POST請求

簡單的網頁下載,HttpURLConnection能夠完成,可是涉及到用戶登陸等權限相關問題,就須要涉及Session、Cookies。,就很難使用HttpURLConnection來處理了。Apache開源組織提供了一個HttpClient項目能夠處理這些問題。HttpClient關注於如何發送請求、接受請求,以及管理HTTP連接。
使用HttpClient對象來發送請求、接受響應步驟:php

  1. 建立HttpClient對象web

  2. 若是要發送GET請求,建立HttpGet對象;若是是POST請求,則建立HttpPost對象。服務器

  3. 若是須要添加參數,對於HttpGet直接在構造URL的時候填入參數。對於POST請求,使用setEntity(HttpEntity entity)方法來設置網絡

  4. 調用HttpClient對象的execute(HttpUriRequest request)發送請求,此方法返回一個HttpResponseide

  5. 調用HttpResponse的getALLHeaders()、getHeaders(String name)等方法可獲取服務器響應頭;調用HttpResponse的getEntity()方法可獲取HttpEntity對象,該對象包裝了服務器響應內容。post

注意:ui

很多地方說能夠使用HttpGet和HttpPost共同的setParams(HttpParams params)方法添加請求參數,可是我沒有設置成功,網上搜索發現好多人也沒成功。Even Apache’s official example uses URIBuilder’s setParameter method to build the params out in the URI,因此沒有使用這種方法.this

GET請求Demo:url

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public class MainActivity extends Activity {
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         TextView textViewShow = (TextView) findViewById(R.id.showText);
         //直接在URL後添加請求參數
         String url = "http://192.168.1.103/index.php?get1=hello&get2=bay" ;
         try {
             // 建立DefaultHttpClient對象
             HttpClient httpclient = new DefaultHttpClient();
             // 建立一個HttpGet對象
             HttpGet get = new HttpGet(url);
             // 獲取HttpResponse對象
             HttpResponse response = httpclient.execute(get);
             //判斷是否連接成功
             if (response.getStatusLine().getStatusCode() == 200 ) {
                 //實體轉換爲字符串
                 String content = EntityUtils.toString(response.getEntity());
                 textViewShow.setText(content);
             } else {
                 textViewShow.setText( "網絡錯誤" );
             }
 
         } catch (ClientProtocolException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         } catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }
     }
 
     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
         // Inflate the menu; this adds items to the action bar if it is present.
         getMenuInflater().inflate(R.menu.main, menu);
         return true ;
     }
}

POST請求Demo:spa

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
public class MainActivity extends Activity {
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         TextView textViewShow = (TextView) findViewById(R.id.showText);
 
         String url = "http://192.168.1.103/index.php" ;
         HttpClient httpClient = new DefaultHttpClient();
         try {
         HttpPost post = new HttpPost(url);
         List params = new ArrayList();
         params.add( new BasicNameValuePair( "get1" , "hello" ));
         params.add( new BasicNameValuePair( "get2" , "usrl" ));
 
             post.setEntity( new UrlEncodedFormEntity(params, HTTP.UTF_8));
             HttpResponse response = httpClient.execute(post);
             if (response.getStatusLine().getStatusCode() == 200 ){
                 String content = EntityUtils.toString(response.getEntity());
                 textViewShow.setText(content);
 
             } else {
                 textViewShow.setText( "網絡問題" );
             }
 
         } catch (UnsupportedEncodingException e) {
             // TODO Auto-generated catch block
             textViewShow.setText( "UnsupportedEncodingException" );
         } catch (ClientProtocolException e) {
             // TODO Auto-generated catch block
             textViewShow.setText( "ClientProtocolException" );
         } catch (IOException e) {
             // TODO Auto-generated catch block
             textViewShow.setText( "IOException" );
         }
 
     }
 
     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
         // Inflate the menu; this adds items to the action bar if it is present.
         getMenuInflater().inflate(R.menu.main, menu);
         return true ;
     }
 
}
相關文章
相關標籤/搜索