Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android使用Apache HttpClient發送GET、POST請求

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

編輯:關於Android編程

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


創建HttpClient對象
如果要發送GET請求,創建HttpGet對象;如果是POST請求,則創建HttpPost對象。
如果需要添加參數,對於HttpGet直接在構造URL的時候填入參數。對於POST請求,使用setEntity(HttpEntity entity)方法來設置
調用HttpClient對象的execute(HttpUriRequest request)發送請求,此方法返回一個HttpResponse
調用HttpResponse的getALLHeaders()、getHeaders(String name)等方法可獲取服務器響應頭;調用HttpResponse的getEntity()方法可獲取HttpEntity對象,該對象包裝了服務器響應內容。
注意:


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


GET請求Demo:

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(),"utf-8");
                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:

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(),"utf-8");
                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;
    }
 
}



  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved