Volley的基本使用
Volley的基本使用
导入jar包
导入成功
源码
GET请求下载地址:http://download.csdn.net/detail/q4878802/9053969
POST请求下载地址:http://download.csdn.net/detail/q4878802/9054111
说明
应用比较灵活所以没有封装,可以根据自己的需求去进行封装
这里是启动Activity以后就开始轮询请求网络,退出以后取消请求。
添加网络权限
<!-- 添加网络权限 -->
<uses-permission android:name="android.permission.INTERNET" />
GET请求
package com.kongqw.kqwvolleydemo;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
public class MainActivity extends Activity {
private RequestQueue mQueue;
private StringRequest stringRequest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 创建一个RequestQueue对象
mQueue = Volley.newRequestQueue(this);
// GET请求
stringRequest = new StringRequest("http://www.baidu.com",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("TAG", response);
Toast.makeText(MainActivity.this, "response = " + response, Toast.LENGTH_SHORT).show();
mQueue.add(stringRequest);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("TAG", error.getMessage(), error);
Toast.makeText(MainActivity.this, "error = " + error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
mQueue.add(stringRequest);
}
@Override
protected void onStop() {
mQueue.stop();
super.onStop();
}
}
POST请求
package com.kongqw.kqwvolleydemo;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends Activity {
private RequestQueue mQueue;
private StringRequest stringRequest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 创建一个RequestQueue对象
mQueue = Volley.newRequestQueue(this);
// POST请求
stringRequest = new StringRequest(Request.Method.POST, "http://www.baidu.com",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("TAG", response);
Toast.makeText(MainActivity.this, "response = " + response, Toast.LENGTH_SHORT).show();
mQueue.add(stringRequest);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("TAG", error.getMessage(), error);
Toast.makeText(MainActivity.this, "error = " + error.getMessage(), Toast.LENGTH_SHORT).show();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> map = new HashMap<String, String>();
map.put("params1", "value1");
map.put("params2", "value2");
return map;
}
};
mQueue.add(stringRequest);
}
@Override
protected void onStop() {
mQueue.stop();
super.onStop();
}
}
请求数据