android网络连接

package com.bluedatax.w65.net;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.bluedatax.w65.BaseApplication;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by bdx108 on 15/12/29.
 */
public class InternetConnection {
    //将网络连接单例
    private static InternetConnection mConnection;
    private InternetConnection() {
    }
    public static synchronized InternetConnection getInstance() {
        if (mConnection == null) {
            mConnection = new InternetConnection();
        }
        return mConnection;
    }

    /**
     * 监听网络连接上的接口
     */
    public interface OnConnectionListerner {
        /**
         * 网络连接的状态的。
         * @param connection 判断网络是否连接,连接返回true,未连接返回false
         * @param type       如果连接网络,返回网络的连接类型。
         */
        void isConnection(boolean connection, String type);

        /**
         * 连接网络成功回调的方法。
         * @param response 返回网络的返回值
         */
        void  onSuccessConnection(String response);

        /**
         * 网络连接错误
         * @param response   返回“网络连接错误”字符串
         * @param statusCode 返回连接的错误码
         */
        void onFailConnection(String response, int statusCode);
    }

    /**
     * 提交HashMap数据的网络连接
     * @param url                 网络连接的URL网址
     * @param map                 提交的数据
     * @param connectionListerner 网络连接的监听对象, 获得网络的连接状态
     */
    public void addRequest(String url, final HashMap<String, String> map, final OnConnectionListerner connectionListerner) {
        //获取网络的状态
        if (!NetWorkUtils.isConnection()) {
            connectionListerner.isConnection(false, null);
        } else {
            connectionListerner.isConnection(true, NetWorkUtils.getConnectionType());
        }

        //创建请求
        StringRequest request = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        connectionListerner.onSuccessConnection(response);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        if(error.networkResponse==null){
                            connectionListerner.onFailConnection("网络连接失败", 404);
                        }else {
                            connectionListerner.onFailConnection("网络连接失败", error.networkResponse.statusCode);
                        }
                    }
                }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return map;
            }
        };
        //创建请求队列并将请求添加到请求队列中

        VolleySingleton.getInstance(BaseApplication.getContext()).addToRequestQueue(request);
    }
}
package com.bluedatax.w65.net;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.bluedatax.w65.BaseApplication;

/**
 * Created by bdx108 on 15/12/29.
 */
public class NetWorkUtils {
    //定义网络连接的管理器
    private  static  ConnectivityManager mManager  = (ConnectivityManager) BaseApplication.getContext().getSystemService(BaseApplication.getContext().CONNECTIVITY_SERVICE);
    private static NetworkInfo networkInfo  = mManager.getActiveNetworkInfo();;

    /**
     * 判断网络是否连接
     * @return 连接返回true,未连接返回false.
     */
    public static boolean isConnection(){
        if (networkInfo != null && networkInfo.isConnected()) {
            return true;
        }
        return false;
    }

    /**
     * 获得网络连接的类型
     * @return 返回网络连接的类型,例如Wifi.
     */
    public  static String getConnectionType() {
        String type = "";
        if (networkInfo != null && networkInfo.isConnected()) {
            type = networkInfo.getTypeName();
        } else {
            type = "无网络连接";
        }
        return type;
    }
}
package com.bluedatax.w65.net;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.LruCache;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;

public class VolleySingleton {
    private static VolleySingleton mInstance;
    private RequestQueue mRequestQueue;//消息队列
    private ImageLoader mImageLoader;//ImageLoader对象
    private static Context mCtx;

    private VolleySingleton(Context context) {
        mCtx = context;
        mRequestQueue = getRequestQueue();
        mImageLoader = getImageLoader();
    }
    public static synchronized VolleySingleton getInstance(Context context){
        if(mInstance == null){
            mInstance = new VolleySingleton(context);
        }
        return mInstance;
    }
    public RequestQueue getRequestQueue(){
        if(mRequestQueue == null){
            mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());


        }
        return mRequestQueue;
    }
    public ImageLoader getImageLoader(){
        if(mImageLoader==null){
            mImageLoader = new ImageLoader(getRequestQueue(), new ImageLoader.ImageCache(){
                private final LruCache<String,Bitmap> cache = new LruCache<String,Bitmap>(20);//设置图片缓存
                @Override
                public Bitmap getBitmap(String url) {
                    return null;
                }
                @Override
                public void putBitmap(String url, Bitmap bitmap) {
                }
            });
        }
        return mImageLoader;
    }
    //将请求添加到队列中
    public void addToRequestQueue(Request req){
        getRequestQueue().add(req);
        req.setRetryPolicy(new DefaultRetryPolicy(8000,3,1.0f));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值