Android 4.0以上系统中使用volley进行网络通讯

1、安装

1)、Git clone the repository by typing the following at the command line:

git clone https://android.googlesource.com/platform/frameworks/volley

2)、Import the downloaded source into your app project as an Android library project or make a .jar file.
如果使用Android Studio,也可以在build.gradle dependencies中添加
compile 'com.mcxiaoke.volley:library:1.0.19'

如图
volley.png

2、使用

使用分为同步和异步方式,异步方式如下

public void get(String path, final Handler handler, Context context) {
    StringRequest request = new StringRequest(Request.Method.GET, path, new Response.Listener<String>() {
        @Override
        public void onResponse(String s) {
            Message message = new Message();
            message.what = 1;
            message.obj = s;
            handler.sendMessage(message);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            Message message = new Message();
            message.what = -1;
            handler.sendMessage(message);
        }
    });
    RequestQueue requestQueue = Volley.newRequestQueue(context);
    requestQueue.add(request);
}

public void post(String path, final Map<String, String> params, final Handler handler, Context context) {
    StringRequest request = new StringRequest(Request.Method.POST, path, new Response.Listener<String>() {
        @Override
        public void onResponse(String s) {
            Message message = new Message();
            message.what = 1;
            message.obj = s;
            handler.sendMessage(message);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            Message message = new Message();
            message.what = -1;
            handler.sendMessage(message);
        }
    }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            return params;
        }
    };
    RequestQueue requestQueue = Volley.newRequestQueue(context);
    requestQueue.add(request);
}

调用方式使用:

Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        switch (msg.what) {
            case 1:
                Log.d("resp",msg.obj);
                break;
            default:
                Toast.makeText(MainActivity.this, "出现异常", Toast.LENGTH_SHORT).show();
        }
    }
};
get("http://www.baidu.com/",handler,getApplicationContext());
Map<String, String> map = new HashMap<>();
map.put("action","Hello");
new net().post("http://www.baidu.com/",map,handler,getApplicationContext());

同步方式:

public String sendGet(String path, Context context) throws ExecutionException, InterruptedException {
    RequestFuture<String> future = RequestFuture.newFuture();
    StringRequest request = new StringRequest(path,future,future);
    RequestQueue queue = Volley.newRequestQueue(context);
    queue.add(request);
    return future.get();
}

public String sendPost(String path, final Map<String, String> params, Context context) throws ExecutionException, InterruptedException, TimeoutException {
    RequestFuture<String> future = RequestFuture.newFuture();
    StringRequest request = new StringRequest(Request.Method.POST,path,future,future) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            return params;
        }
    };
    RequestQueue queue = Volley.newRequestQueue(context);
    queue.add(request);
    return future.get();
}

使用方法:

new Thread(new Runnable() {
    @Override
    public void run() {
        Map<String,String> map = new HashMap<String, String>();
        map.put("action","Hello");
        Log.d("resp",new net().sendPost("http://www.baidu.com/",map,getApplicationContext()));
        Log.d("resp",new net().sendGet("http://www.baidu.com/",getApplicationContext()));
    }
}).start();

标签: none

评论已关闭