Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android之網絡通信Volley框架用法

Android之網絡通信Volley框架用法

編輯:關於Android編程

概述:

Volley提供的功能

簡單的講,提供了如下主要的功能:

1、封裝了的異步的RESTful 請求API;

2、一個優雅和穩健的請求隊列;

3、一個可擴展的架構,它使開發人員能夠實現自定義的請求和響應處理機制;

4、能夠使用外部HTTP Client庫;

5、緩存策略;

6、自定義的網絡圖像加載視圖(NetworkImageView,ImageLoader等);

Volley也有兩種加載方式DoGet和DoPost。

代碼:

將volley框架單例封裝成一個方法:

/**
 * Created by Administrator on 2015/9/14.
 * 單例模式的RequestQueue,可以防止內存溢出
 */
public class MySingleTon  {
    private static MySingleTon mInstance;
    private RequestQueue mRequestQueue;
    private static Context mCtx;

    private MySingleTon(Context context) {
        mCtx = context;
        mRequestQueue = getRequestQueue();
    }

    public static synchronized MySingleTon getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new MySingleTon(context);
        }
        return mInstance;
    }

    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            // getApplicationContext() is key, it keeps you from leaking the
            // Activity or BroadcastReceiver if someone passes one in.
            mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
        }
        return mRequestQueue;
    }

    public  void addToRequestQueue(Request req) {
        getRequestQueue().add(req);
    }
}

DoGet方式:

private void volleyDoGet() {
        //RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
        //response的監聽有兩種,一個是正確響應的監聽,一個是錯誤響應的監聽,當沒有網絡時,網絡連接會出錯
        StringRequest request = new StringRequest(Request.Method.GET, http://www.360.com, new Response.Listener() {
            @Override
            public void onResponse(String response) {
                mTextViewContent.setText(response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                mTextViewContent.setText(網絡連接錯誤!);
            }
        });
        MySingleTon.getInstance(getApplicationContext()).addToRequestQueue(request);
    }

DoPost方式:

private void volleyDoPost() {
        //RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
        StringRequest request = new StringRequest(Request.Method.POST, http://192.168.0.30:8080/MyWebTest/MyTestServerlet, new Response.Listener() {
            @Override
            public void onResponse(String response) {
                mTextViewContent.setText(response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                mTextViewContent.setText(網絡連接錯誤!);
            }
        }){
            @Override
            protected Map getParams() throws AuthFailureError {
                HashMap map = new HashMap<>();
                map.put(username,zhaoliu);

                return map;
            }
        };
        MySingleTon.getInstance(getApplicationContext()).addToRequestQueue(request);
    }

結果演示:
這裡寫圖片描述

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved