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;
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));
}
}