android平台HttpGet、HttpPost请求实例

2019-12-10 20:05:32于海丽
出自网络搜索引擎巨头的Android平台,其对网络的支持自然不用多说,在Android SDK中已经集成了Apache的HttpClient模块。使用HttpClient模块,我们就可以使用HTTP协议进行网络连接了    

使用HttpClient中的HttpGet()方法进行http通信的实例:

复制代码
/**  
 *description:Android HttpGet()  
 *authour:YanEr·Gates  
 *website:http://www.easck.com/>  */

 

package me.gogogoog; 

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MyHttpGetActivity extends Activity{
 /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState){  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.result);

        TextView resultText = (TextView) this.findViewById(R.id.resultText);         

        String username="username";  
        String password="password"; 

        String httpUrl = "http://www.easck.com/AndroidLogin/loginAction.do?method=login&username="+username+"&password="+password;    
        //创建httpRequest对象 
        HttpGet httpRequest = new HttpGet(httpUrl);
  try
  {
   //取得HttpClient对象
   HttpClient httpclient = new DefaultHttpClient();
   //请求HttpClient,取得HttpResponse
   HttpResponse httpResponse = httpclient.execute(httpRequest);