您的位置:首页 > 移动开发 > Android开发

android中用get和post方式向服务器提交请求

2013-12-12 15:47 447 查看
通过get和post方式向服务器发送请求

首先说一下get和post的区别

get请求方式是将提交的参数拼接在url地址后面,例如http://www.baidu.com/index.jsp?num=23&jjj=888;

但是这种形式对于那种比较隐私的参数是不适合的,而且参数的大小也是有限制的,一般是1K左右吧,对于上传文件

就不是很适合。

post请求方式是将参数放在消息体内将其发送到服务器,所以对大小没有限制,对于隐私的内容也比较合适。

在android中用get方式向服务器提交请求:

在android模拟器中访问本机中的tomcat服务器时,注意:不能写localhost,因为模拟器是一个单独的手机系统,所以要写真是的IP地址。

否则无法访问到服务器。

//要访问的服务器地址,下面的代码是要向服务器提交用户名和密码,提交时中文先要经过URLEncoder编码,因为模拟器默认的编码格式是utf-8

//而tomcat内部默认的编码格式是ISO8859-1,所以先将参数进行编码,再向服务器提交。

private String address = "http://192.168.2.101:80/server/loginServlet";

public boolean get(String username, String password) throws Exception {

username = URLEncoder.encode(username); // 中文数据需要经过URL编码

password = URLEncoder.encode(password);

String params = "username=" + username + "&password=" + password;

//将参数拼接在URl地址后面

URL url = new URL(address + "?" + params);

//通过url地址打开连接

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

//设置超时时间

conn.setConnectTimeout(3000);

//设置请求方式

conn.setRequestMethod("GET");

//如果返回的状态码是200,则一切Ok,连接成功。

return conn.getResponseCode() == 200;

}

//这种方式我平时喜欢用的方式

//获得要传递的数据

String username = et1.getText().toString();

String password = et2.getText().toString();



// 创建HttpGet对象

HttpGet request = new HttpGet(url +"name="+username+"&password="+password);

// 使用execute方法发送HTTP GET请求,并返回HttpResponse对象

// DefaultHttpClient为Http客户端管理类,负责发送请

HttpResponse response = httpClient.execute(request);

// 判断请求响应状态码,状态码为200表示服务端成功响应了客户端的请求

if (response.getStatusLine().getStatusCode() == 200) {

// 使用getEntity方法获得返回结果

String data = EntityUtils.toString(response.getEntity(),"gbk");

//获得Message对象

Message msg = handler.obtainMessage(1);

//创建Bundle对象

Bundle bundle = new Bundle();

//用mes传递数据

msg.setData(bundle);

//开启Message对象

msg.sendToTarget();

}



//用post得值

public boolean post(String username, String password) throws Exception {

username = URLEncoder.encode(username); // 中文数据需要经过URL编码

password = URLEncoder.encode(password);

String params = "username=" + username + "&password=" + password;

byte[] data = params.getBytes();

URL url = new URL(address);

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setConnectTimeout(3000);

//这是请求方式为POST

conn.setRequestMethod("POST");

//设置post请求必要的请求头

conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // 请求头, 必须设置

conn.setRequestProperty("Content-Length", data.length + ""); // 注意是字节长度, 不是字符长度

conn.setDoOutput(true); // 准备写出

conn.getOutputStream().write(data); // 写出数据

return conn.getResponseCode() == 200;

}

//下面是我喜欢的方式

//把来传递的数据封装到user对象中

User user = new User();

user.setUserName(et1.getText().toString());

user.setUserPass(et2.getText().toString());

//创建Post对象

HttpPost request = new HttpPost("http://10.0.2.2:8080/system/Servlet");

// 将需要传递的参数封装到List<NameValuePair>类型的对象中

List<NameValuePair> params = new ArrayList<NameValuePair>();

params.add(new BasicNameValuePair("username", user.getUserName()));

params.add(new BasicNameValuePair("password", user.getUserPass()));

// 将封装参数的对象存入request中,并设置编码方式

request.setEntity(new UrlEncodedFormEntity(params,

HTTP.UTF_8));

// DefaultHttpClient为Http客户端管理类,负责发送请求和接受响应

HttpResponse response = defaultHttpClient.execute(request);

// 判断请求响应状态码,状态码为200表示服务端成功响应了客户端的请求

if (response.getStatusLine().getStatusCode() == 200){

// 使用getEntity方法获得返回结果

String data = EntityUtils.toString(response.getEntity(),"gdk");

//创建bundle对象

Bundle bundle = new Bundle();

//用bundle对象来封装data数据

bundle.putString("data", data);

//创建Message对象

Message mes = handler.obtainMessage(1);

//存储bundle数据

mes.setData(bundle);



mes.sendToTarget();

转载自 http://blog.sina.com.cn/s/blog_a46817ff01017yxt.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: