【android,7】7.android在web下的应用-将客户端信息提交到服务端
2013-12-19 00:00
323 查看
将客户端提交到服务端:
一、用户登录将用户名和密码传递个服务器端:get方式提交
1、设置布局:layout/main,xml
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<EditText
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名" >
</EditText>
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="login"
android:text="get登陆" />
</LinearLayout>
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。
相关文章推荐
- 【android,6】6.android在web下的应用-在客户端获取服务端的信息
- Android应用开发之Android平台向web应用提交信息
- Android平台向web应用提交信息
- Android平台向web应用get、post方式提交信息案例
- Android平台向web应用get、post方式提交信息案例
- Android开发之web应用提交信息
- Android平台向web应用get、post方式提交信息案例
- Android平台向web应用get、post方式提交信息案例
- Android应用开发之Android平台向web应用提交信息
- Android平台向web应用get、post方式提交信息案例
- JavaWeb-1-IOS或Android客户端上传图片到Java服务端存到数据库,再从数据库取出下载下来的过程
- WEB-INF是Java的WEB应用的安全目录。所谓安全就是客户端无法访问,只有服务端可以访问的目录。
- 基于Linux搭建一个类似Qik手机录像直播平台(服务器端:feng streaming server + web server,客户端:Android手机应用)
- web客户端,服务端,android客户端关于JSON的使用分析
- web程序获取服务端以及客户端ip,name,MAC地址等信息
- SpringCloud系列九:SpringCloudConfig 基础配置(SpringCloudConfig 的基本概念、配置 SpringCloudConfig 服务端、抓取配置文件信息、客户端使用 SpringCloudConfig 进行配置、单仓库目录匹配、应用仓库自动选择、仓库匹配模式)
- Android做客户端,Web做服务端(Xml数据格式)
- 手把手教你搭建自己的Java Web(Android)项目(SpringMVC + Mybatis服务端,Html5 Web端, Android客户端实现)
- 简单java Web Service服务端和Android客户端
- Android应用性能测试(客户端-服务端)平台实现