Android之HttpClient的GET和POST区别

我们通过AsyncTask这个异步类来执行和界面的交互,然后将返回的数据给UI主线程。

你可以把HttpClient想象成一个浏览器,通过它的API我们可以很方便的发出GET,POST请求。


userName = (EditText) findViewById(R.id.editText1);
passWord = (EditText) findViewById(R.id.editText2);

Get传值方式:

MyAsyncTaskGet task = new MyAsyncTaskGet(result);
String user = userName.getText().toString();
String pass = passWord.getText().toString();
task.execute("http://192.168.0.17:8080/HttpDemo/login.jsp?username="+user+"&password="+pass);

Android虚拟机ip使用10.0.2.2  实体机需要使用真实ip

Get处理方式:

import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
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.os.AsyncTask;
import android.widget.TextView;

public class MyAsyncTaskGet extends AsyncTask<String, Void, String> {
	
	private TextView textView;
	public MyAsyncTaskGet(TextView textView){
		this.textView = textView;
	}

	protected String doInBackground(String... params) {
		
		String httpUrl = params[0];
		//打开一个浏览器
		HttpClient client = new DefaultHttpClient();
		//输入网址
		HttpGet get = new HttpGet(httpUrl);
		//回车
		try {
			HttpResponse response = client.execute(get);
			//服务器响应状态
			int code = response.getStatusLine().getStatusCode();
			
			if(code == 200){
				//返回数据
				HttpEntity entity = response.getEntity();
				String result = EntityUtils.toString(entity);
                               //EntityUtils是一个工具类,通过它可以将数据转换成想要的格式
                                return result;
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	protected void onPostExecute(String result) {
		super.onPostExecute(result);
		textView.setText(result);
	}

}

POST传值方式:

MyAsyncTaskPost taskPost = new MyAsyncTaskPost(result);
taskPost.execute("http://192.168.0.17:8080/HttpDemo/login.jsp",
	userName.getText().toString(),passWord.getText().toString());

POST处理方式:

由于POST方式不能在url上直接传递参数,故通过HttpPost的setEntity方法设置键值对来作为参数。

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import android.os.AsyncTask;
import android.widget.TextView;

public class MyAsyncTaskPost extends AsyncTask<String, Void, String> {
	
	private TextView textView;
	public MyAsyncTaskPost(TextView textView){
		this.textView = textView;
	}
	

	protected String doInBackground(String... params) {
		String httpUrl = params[0];
		String username = params[1];
		String password = params[2];
		
		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(httpUrl);
		
		//UrlEncodedFormEntity里面的参数是一个List<NameValuePair>集合
		List<NameValuePair> list = new ArrayList<NameValuePair>();
		list.add(new BasicNameValuePair("username", username));
		list.add(new BasicNameValuePair("password", password));
		
		try {
			//setEntity里的参数是一个HttpEntity实体,这是一个接口,不能new出对象,所以用了一个继承它的类
			post.setEntity(new UrlEncodedFormEntity(list));
			HttpResponse response = client.execute(post);
			
			int code = response.getStatusLine().getStatusCode();
			if(code == 200){
				HttpEntity entity = response.getEntity();
				//通过EntityUtils工具类将返回的数据转换
				String result = EntityUtils.toString(entity);
				return result;
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	protected void onPostExecute(String result) {
		super.onPostExecute(result);
		
		textView.setText(result);
	}
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值