[android,7]7.android在web下的应用-将客户端信息提交到服务端
将客户端提交到服务端:
一、用户登录将用户名和密码传递个服务器端: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。
- 01-11全球最受赞誉公司揭晓:苹果连续九年第一
- 12-09罗伯特·莫里斯:让黑客真正变黑
- 12-09谁闯入了中国网络?揭秘美国绝密黑客小组TA
- 12-09警示:iOS6 惊现“闪退”BUG
- 03-08消息称微软开发内部AI推理模型,或将成为Op
- 03-08美国法院驳回马斯克请求,未阻止OpenAI转型
- 03-08饿了么成立即时配送算法专家委员会 持续全局
- 03-08长安汽车:预计今年底长安飞行汽车将完成试
- 03-08谷歌推出虚拟试穿、AR美妆新功能