1、新建项目并将用到的jar包Volley.jar和gson-2.3.1.jar添加到项目的app/libs中
2、封装volley请求方便调用
VolleyUtil.java
package com.ihefe.test.volley;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
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 com.google.gson.Gson;
import java.util.Map;
/**
* 请求数据工具类
*/
public class VolleyUtil {
private static RequestQueue requestQueue;
private VolleyUtil() {
}
public static VolleyUtil getInstance() {
return VolleyUtilHold.getVolleyUtilInstance();
}
public void init(Context context) {
requestQueue = Volley.newRequestQueue(context);
}
/**
* post请求
*
* @param path 请求路径
* @param map 请求参数
* @param entity 需要解析的类
* @return
*/
public void StringRequest_POST(final Handler handler, String path, final Map map, final Object entity) {
StringRequest request = new StringRequest(StringRequest.Method.POST, path, new Response.Listener() {
@Override
public void onResponse(String response) {
Gson gson = new Gson();
Object o = gson.fromJson(response, entity.getClass());
Message msg = Message.obtain();
msg.obj = o;
handler.sendMessage(msg);
}
}, new MyErrorListener()) {
@Override
protected Map getParams() throws AuthFailureError {
return map;
}
};
request.setTag(path);
requestQueue.add(request);
}
/**
* get请求
*
* @param path 请求路径
* @param entity 需要解析的类
* @return
*/
public void StringRequest_GET(final Handler handler, String path, final Object entity) {
StringRequest request = new StringRequest(path, new Response.Listener() {
@Override
public void onResponse(String response) {
Gson gson = new Gson();
Object o = gson.fromJson(response, entity.getClass());
Message msg = Message.obtain();
msg.obj = o;
handler.sendMessage(msg);
}
}, new MyErrorListener());
request.setTag(path);
requestQueue.add(request);
}
private static final class VolleyUtilHold {
public static VolleyUtil getVolleyUtilInstance() {
return new VolleyUtil();
}
}
/**
* 执行请求错误
*/
public static class MyErrorListener implements Response.ErrorListener {
@Override
public void onErrorResponse(VolleyError error) {
// LogUtils.showLog("VolleyError" + error.getMessage());
}
}
/**
* 需要手动解析字符串的post请求
*
* @param path
* @param succes
* @param error
* @param map
*/
public void simple_post(String path,
Response.Listener succes, Response.ErrorListener error,
final Map map) {
StringRequest request_post = new StringRequest(Request.Method.POST,
path, succes, error) {
@Override
protected Map getParams() throws AuthFailureError {
return map;
}
};
request_post.setTag(path);
System.out.println(request_post);
requestQueue.add(request_post);
}
/**
* 需要手动解析字符串的get请求
*
* @param path
* @param succes
* @param error
*/
public void simple_get(String path, Response.Listener succes, Response.ErrorListener error) {
Log.e("VolleyUtil", "path-->"+path);
StringRequest request = new StringRequest(path,succes,error);
requestQueue.add(request);
}
/**
* 取消请求
*
* @param path 请求路径
*/
public void removeRequest(String path) {
requestQueue.cancelAll(path);
}
}
3、activity调用volley工具类发送post请求
package com.ihefe.test.volley;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init_config(getApplication());//初始化
Map map = new HashMap<>();
map.put("username", "test");
map.put("password", "f5f97c92ae39d49a4fa87d97eb3d89ff");
//发送请求
VolleyUtil.getInstance().simple_post("http://192.168.15.43/server.php",
new Response.Listener() {
@Override
public void onResponse(String result) {
Log.e("TAG", "onResponse: " + result);
Toast.makeText(MainActivity.this, "请求成功",
Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError arg0) {
Toast.makeText(MainActivity.this, "网络错误!",
Toast.LENGTH_SHORT).show();
}
}, map);
}
/**
* 初始化设置
*
* @param context
*/
public void init_config(Context context) {
VolleyUtil.getInstance().init(context);
}
}
5、运行app项目查看结果:
附:
server.php
'test','age'=>100);
$data[]=array('name'=>'小黑','age'=>80);
$data[]=array('name'=>'小白','age'=>80);
$result['list']=$data;
$result['info']=$_POST;
echo json_encode($result);