Volley是Google推出的Android异步网络请求框架和图片加载库,特别适合数据量不大但通信频繁的网络操作。它有以下特点:
在app的build.gradle文件中添加Volley依赖:
dependencies {
implementation 'com.android.volley:volley:1.2.1'
}
RequestQueue queue = Volley.newRequestQueue(this);
String url = "https://example.com/api/data";
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
Request.Method.GET,
url,
null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// 处理成功的响应
try {
String name = response.getString("name");
int age = response.getInt("age");
boolean isStudent = response.getBoolean("isStudent");
// 处理嵌套JSON对象
JSONObject address = response.getJSONObject("address");
String city = address.getString("city");
// 处理JSON数组
JSONArray hobbies = response.getJSONArray("hobbies");
for (int i = 0; i < hobbies.length(); i++) {
String hobby = hobbies.getString(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// 处理错误
error.printStackTrace();
}
}
);
// 将请求添加到队列
queue.add(jsonObjectRequest);
String url = "https://example.com/api/items";
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(
Request.Method.GET,
url,
null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
// 处理成功的响应
try {
for (int i = 0; i < response.length(); i++) {
JSONObject item = response.getJSONObject(i);
String id = item.getString("id");
String name = item.getString("name");
// 处理每个项目...
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// 处理错误
error.printStackTrace();
}
}
);
// 将请求添加到队列
queue.add(jsonArrayRequest);
String url = "https://example.com/api/post";
JSONObject jsonBody = new JSONObject();
try {
jsonBody.put("username", "user123");
jsonBody.put("password", "pass123");
} catch (JSONException e) {
e.printStackTrace();
}
JsonObjectRequest postRequest = new JsonObjectRequest(
Request.Method.POST,
url,
jsonBody,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// 处理响应
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// 处理错误
}
}
) {
@Override
public Map<String, String> getHeaders() {
HashMap<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
headers.put("Authorization", "Bearer token123");
return headers;
}
};
queue.add(postRequest);
原因:
解决:
原因:
解决:
原因:
解决:
public class VolleySingleton {
private static VolleySingleton instance;
private RequestQueue requestQueue;
private static Context ctx;
private VolleySingleton(Context context) {
ctx = context.getApplicationContext();
requestQueue = getRequestQueue();
}
public static synchronized VolleySingleton getInstance(Context context) {
if (instance == null) {
instance = new VolleySingleton(context);
}
return instance;
}
public RequestQueue getRequestQueue() {
if (requestQueue == null) {
requestQueue = Volley.newRequestQueue(ctx);
}
return requestQueue;
}
public <T> void addToRequestQueue(Request<T> req) {
getRequestQueue().add(req);
}
}
@Override
protected void onStop() {
super.onStop();
if (queue != null) {
queue.cancelAll(this);
}
}
Volley特别适合需要频繁网络请求但数据量不大的应用,如社交网络应用、新闻阅读器等。