[android,7]7.android在web下的应用-将客户端信息提交到服务端

浏览:
字体:
发布时间:2013-12-20 16:18:38
来源:

将客户端提交到服务端:

一、用户登录将用户名和密码传递个服务器端:get方式提交

1、设置布局:layout/main,xml

 

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical">

 

android:id="@+id/et_username"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="请输入用户名" >

 

 

android:id="@+id/et_password"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="请输入密码" />

 

android:id="@+id/button1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:onClick="login"

android:text="get登陆" />

 

 

 

2、在Activity类中获取登录的用户名和密码:

public class LoginActivityextends Activity {

private EditText et_username;

private EditText et_password;

private EditText et_file_path;

 

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

et_password = (EditText)this.findViewById(R.id.et_password);

et_username = (EditText)this.findViewById(R.id.et_username);

et_file_path = (EditText)this.findViewById(R.id.et_file_path);

 

}

 

/**

* 以get的方式 提交数据到服务器

*

* @param view

*/

public void login(View view) {

String password =et_password.getText().toString().trim();

String username =et_username.getText().toString().trim();

if(TextUtils.isEmpty(password) || TextUtils.isEmpty(username)) {

Toast.makeText(this, "用户名和密码不能为空", 0).show();

return;

} else {

// 通过get方式提交数据到服务器

//获取在res/values/string.xml文件中定义的服务端的请求路径

String path = getResources().getString(R.string.serverurl);

//调用向服务端发送请求的方法:

String result = NetService.sendDataByHttpClientGet(path,username, password);

}

 

}

3、Get方式向服务器发送数据的请求的方法:

/*

* 1. 拼装url 把参数进行 url的编码 URLEncoder.encode();

2. 通过http的get请求发送数据到服务器

* (中文乱码的问题)? 把数据按照tomcat的 iso-8859-1的方式进行转码. byte[] 在android下默认采用的编码方式

* utf-8的编码方式

*/

public static StringsendDataByGet(String path, String username,

String password) throwsException {

// ?name=zhangsan&password=123

// 解决姓名 或者 密码里面的空格或者非法字符串的问题

//拼接get方式的请求路径

String urlstr = path + "?name=" +URLEncoder.encode(username)

+ "&password=" + URLEncoder.encode(password);

//连接服务端

URL url = new URL(urlstr);

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

conn.setRequestMethod("GET");

conn.setConnectTimeout(5000);

 

if (conn.getResponseCode() == 200) {

//获取服务端的数据的流

InputStream is =conn.getInputStream();

//同工具类将流转成字节

byte[] result =StreamTools.getBytes(is);

return newString(result);//创建string对象

}

return null;

}

4、设置连接网络的权限

5、在服务端向向客户端响应数据:要以流的形式:并设置utf-8的编码形式

response.getOutputStream().write("登陆成功".getBytes("utf-8"));

 

二、乱码的问题:

1、 在默认的情况下 android的字符编码是UTF-8的编码形式,所以服务端响应的数据要以utf- 8的编码格式返回到客户端。

2、在以get方式请求客户端时,对url中的请求数据要进行url编码,保证对空格和中文的传输。

String urlstr = path + "?name=" +URLEncoder.encode(username)

 

三、通过post请求将数据发送给服务器:

1、post请求

public static String sendDataByPost(String path, String username,

String password) throwsException {

URL url = new URL(path);

//连接服务器

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

//设置请求方式

conn.setRequestMethod("POST");

conn.setConnectTimeout(5000);

// 一定要指定dooutput 的参数 运行客户端写数据给服务器

conn.setDoOutput(true);//表示要以流的形式将数据写给服务端

//拼接数据

String content = "name=" + URLEncoder.encode(username)+ "&password="

+ URLEncoder.encode(password);

byte[] buffer = content.getBytes();

//设置请求头:content-Type

conn.setRequestProperty("Content-Type",

"application/x-www-form-urlencoded");

//设置请求头Content-Length

conn.setRequestProperty("Content-Length",content.length() + "");

// 得到http的输出流

OutputStream os = conn.getOutputStream();

// 把内容写给了服务器

os.write(buffer);

// 注意的是: 在面向http协议编程 处理post请求的时候

 

//获取响应码

if (conn.getResponseCode() == 200) {

InputStream is =conn.getInputStream();

byte[] result =StreamTools.getBytes(is);

return newString(result);

}

return null;

}

2、post请求注意事项:

post 方式提交的数据 没有大小的限制

指定post的请求的地址

指定http的请求方式 为post

指定请求头中的Content-Type Content-Length

conn.setDoOutput(true);

利用conn.getoutputstream.write();

获取服务器返回回来的相应

 

 

四、post请求方式与get请求方式的区别:

1、get方式是通过组拼url的方式提交数据到服务器,

2、Post的请求方式是通过组拼提交的数据键值对,以流的方式写给了服务器,

Post请求方式提交数据到服务端请求头中必须要有Content-Type和Content-Length。

 

 

>更多相关文章
24小时热门资讯
24小时回复排行
资讯 | QQ | 安全 | 编程 | 数据库 | 系统 | 网络 | 考试 | 站长 | 关于东联 | 安全雇佣 | 搞笑视频大全 | 微信学院 | 视频课程 |
关于我们 | 联系我们 | 广告服务 | 免责申明 | 作品发布 | 网站地图 | 官方微博 | 技术培训
Copyright © 2007 - 2024 Vm888.Com. All Rights Reserved
粤公网安备 44060402001498号 粤ICP备19097316号 请遵循相关法律法规
');})();