Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android使用HttpClient方法和易錯問題

Android使用HttpClient方法和易錯問題

編輯:關於Android編程

HttpClient為Android開發者提供了跟簡潔的操作Http網絡連接的方法,在連接過程中也有兩種方式,get和post,先看一下如何實現的

默認是get方式

//先將參數放入List,再對參數進行URL編碼  
List params = new LinkedList();  
params.add(new BasicNameValuePair("param1", "中國"));  
params.add(new BasicNameValuePair("param2", "value2"));  
//baseUrl             
String baseUrl = "http://www.baidu.com";  
  
//將URL與參數拼接  
HttpGet getMethod = new HttpGet(baseUrl + "?" + param);  
              
HttpClient httpClient = new DefaultHttpClient();  
  
try {  
    HttpResponse response = httpClient.execute(getMethod); //發起GET請求  
  
    Log.i(TAG, "resCode = " + response.getStatusLine().getStatusCode()); //獲取響應碼  
    Log.i(TAG, "result = " + EntityUtils.toString(response.getEntity(), "utf-8"));//獲取服務器響應內容  
} catch (ClientProtocolException e) {  
    // TODO Auto-generated catch block  
    e.printStackTrace();  
} catch (IOException e) {  
    // TODO Auto-generated catch block  
    e.printStackTrace();  
}  


post方式

//和GET方式一樣,先將參數放入List  
params = new LinkedList();  
params.add(new BasicNameValuePair("param1", "Post方法"));  
params.add(new BasicNameValuePair("param2", "第二個參數"));  
              
try {  
    HttpPost postMethod = new HttpPost(baseUrl);  
    postMethod.setEntity(new UrlEncodedFormEntity(params, "utf-8")); //將參數填入POST Entity中  
                  
    HttpResponse response = httpClient.execute(postMethod); //執行POST方法  
    Log.i(TAG, "resCode = " + response.getStatusLine().getStatusCode()); //獲取響應碼  
    Log.i(TAG, "result = " + EntityUtils.toString(response.getEntity(), "utf-8")); //獲取響應內容  
                  
} catch (UnsupportedEncodingException e) {  
    // TODO Auto-generated catch block  
    e.printStackTrace();  
} catch (ClientProtocolException e) {  
    // TODO Auto-generated catch block  
    e.printStackTrace();  
} catch (IOException e) {  
    // TODO Auto-generated catch block  
    e.printStackTrace();  
}  
如果需要在獲得網絡資源後,去更新UI的一些東西,需要使用異步的方式,否則會發生錯誤

Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {

            if (msg.what == 0x123) {
                tv.setText(result);
            }
        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv = (TextView) findViewById(R.id.tv);
        result = "";

        final HttpClient httpclient = new DefaultHttpClient();

        new Thread() {
            public void run() {
                HttpGet httpRequest = new HttpGet(
                        "http://www.baidu.com");

                try {
                    HttpResponse httpResponse = httpclient.execute(httpRequest);
                    if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                        // 取得返回的字符串
                        result = EntityUtils.toString(httpResponse.getEntity());
                      
                        //tv.setText(result);//如果在這裡來使用會報錯
                      Message msg = new Message();
                       msg.what = 0x123;
                       handler.sendMessage(msg);
                    }
                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }.start();
    
    }




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