NetWorkUtil

  1 import java.io.IOException;
  2 import java.io.UnsupportedEncodingException;
  3  
  4 import org.apache.http.HttpEntity;
  5 import org.apache.http.HttpResponse;
  6 import org.apache.http.HttpStatus;
  7 import org.apache.http.client.ClientProtocolException;
  8 import org.apache.http.client.HttpClient;
  9 import org.apache.http.client.methods.HttpGet;
 10 import org.apache.http.client.methods.HttpPost;
 11 import org.apache.http.entity.StringEntity;
 12 import org.apache.http.impl.client.DefaultHttpClient;
 13 import org.apache.http.params.BasicHttpParams;
 14 import org.apache.http.params.HttpConnectionParams;
 15 import org.apache.http.params.HttpParams;
 16 import org.apache.http.protocol.HTTP;
 17 import org.apache.http.util.EntityUtils;
 18 import org.json.JSONArray;
 19 import org.json.JSONObject;
 20  
 21 import android.content.Context;
 22 import android.graphics.Bitmap;
 23 import android.graphics.BitmapFactory;
 24 import android.net.ConnectivityManager;
 25 import android.net.NetworkInfo;
 26 import android.util.Log;
 27  
 28 import com.loopj.android.http.AsyncHttpClient;
 29 import com.loopj.android.http.JsonHttpResponseHandler;
 30  
 31 public class NetWorkUtil {
 32     private static String result;
 33     /**
 34      *
 35      * 方法说明:判断是否开启网络
 36      *
 37      * @param mContext
 38      * @return
 39      */
 40     public static boolean isNetwork(Context mContext){
 41         ConnectivityManager manager=(ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
 42         NetworkInfo info=manager.getActiveNetworkInfo();
 43         if(info==null||!info.isAvailable()){
 44             return false;
 45         }
 46         return true;
 47     }
 48     /**
 49      *
 50      * 方法说明:访问网络接口,接到json数据
 51      *
 52      * @param mContext
 53      * @param url
 54      * @param jsonparams
 55      * @return
 56      */
 57     public static void getJsonData(Context mContext,String url,String jsonparams,final LoadJson loadjson){
 58         AsyncHttpClient client=new AsyncHttpClient();
 59         StringEntity entity;
 60         try {
 61             entity = new StringEntity(jsonparams, "utf-8");
 62             client.post(mContext, url, entity, "application/json",new JsonHttpResponseHandler(){
 63                 @Override
 64                 public void onSuccess(int statusCode, JSONObject response) {
 65                     // TODO Auto-generated method stub
 66                     super.onSuccess(statusCode, response);
 67                     result=response.toString();
 68                     loadjson.getJson(response.toString());
 69                     Log.d("TAG", "请求数据成功");
 70                 }
 71                 @Override
 72                 public void onStart() {
 73                     // TODO Auto-generated method stub
 74                     super.onStart();
 75                     Log.d("TAG", "开始请求数据");
 76                 }
 77                 @Override
 78                 public void onFinish() {
 79                     // TODO Auto-generated method stub
 80                     super.onFinish();
 81                     Log.d("TAG", "请求数据结束");
 82                 }
 83                 @Override
 84                 public void onRetry() {
 85                     // TODO Auto-generated method stub
 86                     super.onRetry();
 87                     Log.d("TAG", "重试");
 88                 }
 89                 @Override
 90                 public void onFailure(Throwable e, JSONArray errorResponse) {
 91                     // TODO Auto-generated method stub
 92                     super.onFailure(e, errorResponse);
 93                     Log.d("TAG", "失败");
 94                 }
 95             });
 96         } catch (UnsupportedEncodingException e) {
 97             // TODO Auto-generated catch block
 98             e.printStackTrace();
 99         }
100     }
101 //  public static
102     /**
103      * HttpClient访问网络接口 (暂时)
104      * @param url
105      * @param jsonparams
106      * @return
107      *  @param
108      *  @throws
109      *  @return
110      */
111     public static String getHttpClientJsonData(String url,String jsonparams){
112          
113         HttpPost httpPost = new HttpPost(url);
114         StringEntity entity;
115         try {
116             entity = new StringEntity(jsonparams, HTTP.UTF_8);
117             entity.setContentType("application/json");
118             httpPost.setEntity(entity);
119             HttpClient client = new DefaultHttpClient();
120             HttpResponse response = client.execute(httpPost);
121             if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
122                 HttpEntity resEntity=response.getEntity();
123                 result=EntityUtils.toString(resEntity, "utf-8");
124             }else{
125                  
126             }
127         } catch (UnsupportedEncodingException e) {
128             // TODO Auto-generated catch block
129             e.printStackTrace();
130         } catch (ClientProtocolException e) {
131             // TODO Auto-generated catch block
132             e.printStackTrace();
133         } catch (IOException e) {
134             // TODO Auto-generated catch block
135             e.printStackTrace();
136         }
137  
138         return result ;
139     }
140      
141      
142      
143     /**
144      * 通过地址获取Bitmap (暂时)
145      * @param imageUrl
146      * @return
147      *  @param
148      *  @throws
149      *  @return
150      */
151     public static Bitmap loadImageFromUrl(String imageUrl){
152         Bitmap result = null;
153         HttpGet req = new HttpGet(imageUrl);
154         HttpParams connParams = new BasicHttpParams();
155         HttpConnectionParams.setConnectionTimeout(connParams, 5 * 1000);
156         HttpConnectionParams.setSoTimeout(connParams, 5 * 1000);
157         HttpClient client = new DefaultHttpClient(connParams);
158          
159         try {
160             HttpResponse resp = client.execute(req);
161             if(resp.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
162                 HttpEntity respEntity = resp.getEntity();
163                 result = BitmapFactory.decodeStream(respEntity.getContent());
164             }
165         } catch (ClientProtocolException e) {
166             // TODO Auto-generated catch block
167             e.printStackTrace();
168         } catch (IOException e) {
169             // TODO Auto-generated catch block
170             e.printStackTrace();
171         }
172         return result;
173          
174     }
175     public interface LoadJson{
176         void getJson(String data);
177     }
178 }
原文地址:https://www.cnblogs.com/androidsj/p/4762291.html