Volley的基本特点:
Google 2013年I/O大会上提出的,它是Android 平台上的通信模块,能使网络通信更快,更简单。
提供的功能:
- JSON,图像等的异步下载;
- 网络请求的排序(scheduling)
- 网络请求的优先级处理
- 缓存
- 多级别取消请求
- 和Activity和生命周期的联动(Activity结束时同时取消所有网络请求)
volley适合:数据量不大,通信频繁的网络操作,对大数据量(下载上传文件),会很糟糕。
那么下面来实现下登陆功能,这个功能主要是依靠Volley发出携带参数的Post请求,到服务器实登陆。
1.导入Volley框架
http://download.csdn.net/detail/sinyu890807/7152015
将下载的jar包导入到工程中
2.布局文件
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
android:orientation="vertical"
tools:context=".LoginActivity">
<EditText
android:hint="请输入你的账号"
android:id="@+id/id_er_account"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:hint="请输入你的密码"
android:id="@+id/id_et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:text="登陆"
android:id="@+id/id_bt_login"
android:layout_marginRight="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="注册"
android:id="@+id/id_bt_register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
3.定义一个实体类去保存用户信息
public class UserLocal {
public static String username = " " ;//用户名
public static String password =" ";//密码
public static String getUsername() {
return username;
}
public static void setUsername(String username) {
UserLocal.username = username;
}
public static String getPassword() {
return password;
}
public static void setPassword(String password) {
UserLocal.password = password;
}
}
这里写成全局变量,全局的引用方法就是类名直接引用,不用再new 一个对象。
4.在Activity中初始化控件
private void init() {
mAccount = (EditText) findViewById(R.id.id_er_account);
mPassword = (EditText) findViewById(R.id.id_et_password);
mLoginBtn = (Button) findViewById(R.id.id_bt_login);
mRegisterBtn = (Button) findViewById(R.id.id_bt_register);
mLoginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String account = mAccount.getText().toString().trim();
String password = mPassword.getText().toString().trim();
if (!TextUtils.isEmpty(account) && !TextUtils.isEmpty(password)) {
UserLocal.setUsername(mAccount.getText().toString().trim());
UserLocal.setPassword(mPassword.getText().toString().trim());
//这里是实现与服务器交互
new LoginSupport(LoginActivity.this).LoginToServer();
}
}
});
这里是获取控件上的文本转成String类型,如果两个控件都不为空则提交数据到服务器验证。
5.实现Volley去与服务器通信,我写了一个LoginSupport类
首先定义一个构造方法,把上下文传进来
private Context context;
public LoginSupport(Context context){
this.context=context;
}
然后定义一个LoginServe去实现通信
public void LoginToServer( ) {
String url = "http://********";
StringRequest loginRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String s) {
//服务器请求成功的回调方法
DealResponseFromServer(s);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
//服务器请求失败的回调的方法
Toast.makeText(context,"登录失败", Toast.LENGTH_LONG).show();
}
})
{
//这里是发送参数的地方,重写了 getParams() 方法(传什么参数给服务器也是实际你自己修改)
@Override
protected Map<String, String> getParams()throws AuthFailureError {
HashMap<String, String> map = new HashMap<String, String>();
//如果出现空指针异常或者是登录失败,先检查这里有木有传进来你要发送的用户名和密码。
//所以在执行get数据方法之前一定要先存数据(set方法)
map.put("username", UserLocal.getUsername());
map.put("password",UserLocal.getPassword());
return map;
}
};
MyApplication.getHttpQueue().add(loginRequest);
}
请求成功的回调方法,这里请求成功服务器返回一个字符”1”
private void DealResponseFromServer(String s) {
if(s.equals("1")){
Intent intent = new Intent(context,MainActivity.class);
context.startActivity(intent);
Toast.makeText(context, "登录成功", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(context, "登录失败,请检查账号", Toast.LENGTH_SHORT).show();
}
}
6.建立一个全局请求队列
public class MyApplication extends Application {
public static RequestQueue requestQueue;//
@Override
public void onCreate() {
super.onCreate();
requestQueue = Volley.newRequestQueue(getApplicationContext());
}
public static RequestQueue getHttpQueue(){
return requestQueue;
}
}
最后要在清单文件中声明
android:name=".MyApplication"
要添加在Application中
让后还有网络请求权限
<!--允许联网 -->
<uses-permission android:name="android.permission.INTERNET" />
注册功能实现逻辑一样,携带的参数不一样而已。