博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
NetWorkUtil
阅读量:5973 次
发布时间:2019-06-19

本文共 6537 字,大约阅读时间需要 21 分钟。

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 static102     /**103      * HttpClient访问网络接口 (暂时)104      * @param url105      * @param jsonparams106      * @return107      *  @param108      *  @throws109      *  @return110      */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 block129             e.printStackTrace();130         } catch (ClientProtocolException e) {131             // TODO Auto-generated catch block132             e.printStackTrace();133         } catch (IOException e) {134             // TODO Auto-generated catch block135             e.printStackTrace();136         }137  138         return result ;139     }140      141      142      143     /**144      * 通过地址获取Bitmap (暂时)145      * @param imageUrl146      * @return147      *  @param148      *  @throws149      *  @return150      */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 block167             e.printStackTrace();168         } catch (IOException e) {169             // TODO Auto-generated catch block170             e.printStackTrace();171         }172         return result;173          174     }175     public interface LoadJson{176         void getJson(String data);177     }178 }

 

转载地址:http://vubox.baihongyu.com/

你可能感兴趣的文章
进入TP-Link路由器之后利用快捷键F12查看星号路由密码的方法
查看>>
linux内核的oops
查看>>
基于Token的WEB后台认证机制
查看>>
[MODx] Build a CMP (Custom manager page) using MIGX in MODX 2.3 -- 2
查看>>
uiimageview 异步加载图片
查看>>
屏幕录像专家注册机破解方法
查看>>
SPIE Example References
查看>>
2015阿里巴巴秋招在线笔试题
查看>>
前缀式计算(前缀表达式)
查看>>
poj2728 Desert King --- 01分数规划 二分水果。。
查看>>
iOS边练边学--介绍布局的三种方法
查看>>
[React] React Router: Nested Routes
查看>>
android: 播放视频
查看>>
超级 App 手机百度云端架构设计与个性化推荐
查看>>
携程对AIOps场景和算法的探索与实践
查看>>
Cable:360实现的新虚拟网络架构
查看>>
Visual Studio推出改进的C/C++支持
查看>>
为什么选择PostgreSQL而不是MySQL
查看>>
创建快乐的工作环境
查看>>
解读:Java 11中的模块感知服务加载器
查看>>