美文网首页
Android网络库相关知识

Android网络库相关知识

作者: AlanFu | 来源:发表于2018-05-14 13:53 被阅读0次
写在最前面的话

网络知识的重要性对于任何Android开发者怎么说都不过分。
现在的app主要提供展示和与服务器交互这些功能。所以很有必须认真学习一个开源的网络库
参考文章
volley
https://github.com/google/volley
Android网络库比较
https://blog.csdn.net/carson_ho/article/details/52171976
Android官方介绍volley
https://developer.android.com/training/volley/
android网络编程
https://blog.csdn.net/itachi85/article/details/50982995
android网络协议及网络知识
https://www.jianshu.com/p/e03b9126b262

1.volley的简介和适应场景

简介:
volley是Android官方开发文档上介绍,其可靠性不容置疑。可以让android网络开发更简单,更重要地是
volley具有以下几个优点:
1.自动调度网络请求。
2.支持多并发网络请求
3.支持请求优化级
4.随时可以取消请求。
5.自定义简单
6.支持调试和追踪工具。
适应场景:
频繁地轻量地数据网络请求。

2.volley使用方法示例
示例1:服务器拉取字符串

final TextView mTextView = (TextView) findViewById(R.id.text);
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://www.google.com";

// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        // Display the first 500 characters of the response string.
        mTextView.setText("Response is: "+ response.substring(0,500));
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        mTextView.setText("That didn't work!");
    }
});

// Add the request to the RequestQueue.
queue.add(stringRequest);
示例2:取消一次请求
//定义一个标识到你的请求
public static final String TAG = "MyTag";
StringRequest stringRequest; // Assume this exists.
RequestQueue mRequestQueue;  // Assume this exists.

// Set the tag on the request.
stringRequest.setTag(TAG);

// Add the request to the RequestQueue.
mRequestQueue.add(stringRequest);

//在Activity的onStop取消此次请求。
@Override
protected void onStop () {
    super.onStop();
    if (mRequestQueue != null) {
        mRequestQueue.cancelAll(TAG);
    }
}
示例3:设置RequestQueue参数
RequestQueue mRequestQueue;

// Instantiate the cache
Cache cache = new DiskBasedCache(getCacheDir(), 1024 * 1024); // 1MB cap

// Set up the network to use HttpURLConnection as the HTTP client.
Network network = new BasicNetwork(new HurlStack());

// Instantiate the RequestQueue with the cache and network.
mRequestQueue = new RequestQueue(cache, network);

// Start the queue
mRequestQueue.start();

String url ="http://www.example.com";

// Formulate the request and handle the response.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
        new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        // Do something with the response
    }
},
    new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            // Handle error
    }
});

// Add the request to the RequestQueue.
mRequestQueue.add(stringRequest);

// ...
示例4:请求返回JSON
String url = "http://my-json-feed";

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
        (Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

    @Override
    public void onResponse(JSONObject response) {
        mTextView.setText("Response: " + response.toString());
    }
}, new Response.ErrorListener() {

    @Override
    public void onErrorResponse(VolleyError error) {
        // TODO: Handle error

    }
});

// Access the RequestQueue through your singleton class.
MySingleton.getInstance(this).addToRequestQueue(jsonObjectRequest);
示例5:使用ImageLoader加载图片
      RequestQueue mQueue = Volley.newRequestQueue(getApplicationContext());
      ImageLoader imageLoader = new ImageLoader(mQueue, new BitmapCache());
      ImageLoader.ImageListener listener = ImageLoader.getImageListener(iv_image,R.drawable.ico_default, R.drawable.ico_default);
      imageLoader.get("http://img.my.csdn.net/uploads/201603/26/1458988468_5804.jpg", listener);

3.volley源码阅读

相关文章

网友评论

      本文标题:Android网络库相关知识

      本文链接:https://www.haomeiwen.com/subject/vajddftx.html