NetWorkUtil
import java.io.IOException;
import java.io.UnsupportedEncodingException; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject; import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log; import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler; public class NetWorkUtil {
private static String result;
/**
*
* 方法说明:判断是否开启网络
*
* @param mContext
* @return
*/
public static boolean isNetwork(Context mContext){
ConnectivityManager manager=(ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info=manager.getActiveNetworkInfo();
if(info==null||!info.isAvailable()){
return false;
}
return true;
}
/**
*
* 方法说明:访问网络接口,接到json数据
*
* @param mContext
* @param url
* @param jsonparams
* @return
*/
public static void getJsonData(Context mContext,String url,String jsonparams,final LoadJson loadjson){
AsyncHttpClient client=new AsyncHttpClient();
StringEntity entity;
try {
entity = new StringEntity(jsonparams, "utf-8");
client.post(mContext, url, entity, "application/json",new JsonHttpResponseHandler(){
@Override
public void onSuccess(int statusCode, JSONObject response) {
// TODO Auto-generated method stub
super.onSuccess(statusCode, response);
result=response.toString();
loadjson.getJson(response.toString());
Log.d("TAG", "请求数据成功");
}
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.d("TAG", "开始请求数据");
}
@Override
public void onFinish() {
// TODO Auto-generated method stub
super.onFinish();
Log.d("TAG", "请求数据结束");
}
@Override
public void onRetry() {
// TODO Auto-generated method stub
super.onRetry();
Log.d("TAG", "重试");
}
@Override
public void onFailure(Throwable e, JSONArray errorResponse) {
// TODO Auto-generated method stub
super.onFailure(e, errorResponse);
Log.d("TAG", "失败");
}
});
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// public static
/**
* HttpClient访问网络接口 (暂时)
* @param url
* @param jsonparams
* @return
* @param
* @throws
* @return
*/
public static String getHttpClientJsonData(String url,String jsonparams){ HttpPost httpPost = new HttpPost(url);
StringEntity entity;
try {
entity = new StringEntity(jsonparams, HTTP.UTF_8);
entity.setContentType("application/json");
httpPost.setEntity(entity);
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(httpPost);
if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
HttpEntity resEntity=response.getEntity();
result=EntityUtils.toString(resEntity, "utf-8");
}else{ }
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} return result ;
} /**
* 通过地址获取Bitmap (暂时)
* @param imageUrl
* @return
* @param
* @throws
* @return
*/
public static Bitmap loadImageFromUrl(String imageUrl){
Bitmap result = null;
HttpGet req = new HttpGet(imageUrl);
HttpParams connParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(connParams, 5 * 1000);
HttpConnectionParams.setSoTimeout(connParams, 5 * 1000);
HttpClient client = new DefaultHttpClient(connParams); try {
HttpResponse resp = client.execute(req);
if(resp.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
HttpEntity respEntity = resp.getEntity();
result = BitmapFactory.decodeStream(respEntity.getContent());
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result; }
public interface LoadJson{
void getJson(String data);
}
}
NetWorkUtil的更多相关文章
- Android NetWorkUtil
package com.android.hcframe.netdisc.util; import java.io.BufferedReader; import java.io.InputStreamR ...
- 用Java代码实现拦截区域网数据包
起因: 吃饭的时间在想如果区域网内都是通过路由器上网,那如何实现拦截整个区域网的数据包,从而实现某种窥探欲. 思路: 正常是通过电脑网卡预先设置或分配的IP+网关对路由器进行通讯,比如访问百 ...
- C#开发微信门户及应用(35)--微信支付之企业付款封装操作
在前面几篇随笔,都是介绍微信支付及红包相关的内容,其实支付部分的内容还有很多,例如企业付款.公众号支付或刷卡支付.摇一摇红包.代金券等方面的内容,这些都是微信接口支持的内容,本篇继续微信支付这一主题, ...
- C#开发微信门户及应用(33)--微信现金红包的封装及使用
我在上篇随笔<C#开发微信门户及应用(32)--微信支付接入和API封装使用>介绍为微信支付的API封装及使用,其中介绍了如何配置好支付环境,并对扫码支付的两种方式如何在C#开发中使用进行 ...
- ZooKeeper 笔记(6) 分布式锁
目前分布式锁,比较成熟.主流的方案有基于redis及基于zookeeper的二种方案. 大体来讲,基于redis的分布式锁核心指令为SETNX,即如果目标key存在,写入缓存失败返回0,反之如果目标k ...
- Android okHttp网络请求之缓存控制Cache-Control
前言: 前面的学习基本上已经可以完成开发需求了,但是在项目中有时会遇到对请求做个缓存,当没网络的时候优先加载本地缓存,基于这个需求我们来学习一直okHttp的Cache-Control. okHttp ...
- Winform混合式开发框架访问Web API接口的处理
在我的混合式开发框架里面,集成了WebAPI的访问,这种访问方式不仅可以实现简便的数据交换,而且可以在多种平台上进行接入,如Winform程序.Web网站.移动端APP等多种接入方式,Web API的 ...
- Android判断网路是否畅通加权限
权限 <uses-permission android:name="android.permission.INTERNET"/> <uses-permission ...
- AsyncTask 异步处理
1 package com.ch.day8_asynctaskdemo; 2 3 import java.util.ArrayList; 4 5 import com.ch.myutils.NetWo ...
随机推荐
- HDU 4287-Intelligent IME(哈希)
Intelligent IME Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Dubbo超时重试机制带来的数据重复问题
Dubbo的超时重试机制为服务容错.服务稳定提供了比较好的框架支持,但是在一些比较特殊的网络环境下(网络传输慢,并发多)可能 由于服务响应慢,Dubbo自身的超时重试机制(服务端的处理时间超过了设定的 ...
- org.hibernate.exception.ConstraintViolationException: could not delete:
转自:http://fireinwind.iteye.com/blog/848515 异常描述: org.hibernate.exception.ConstraintViolationExceptio ...
- java之 ------ 图形界面(三)
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.TitledBo ...
- lodash camelCase 驼峰写法
_.camelCase([string='']) 转换字符串为 驼峰写法 _.camelCase('Foo Bar'); // => 'fooBar' _.camelCase('--foo-ba ...
- C语言-srand种子详解
rand() 函数取得随机数的时候是通过一个叫做"种子"的变量经过计算得出一个数值, 然后得出的数值再作为新的"种子"参与下一次的运算, 这样就得到了所谓的随机 ...
- androidstudio新建项目中在布局文件中不显示title的方法
在androidstudio新建项目的时候,在布局文件里有时候会出现如下情况: 上面的标题栏非常碍眼,要想隐藏标题栏的话,可以在Manifest文件的theme标签里进行配置,自定义一个theme,加 ...
- webpack打包vue2.0项目时必现问题(转载)
原文地址:http://www.imooc.com/article/17868 [Vue warn]: You are using the runtime-only build of Vue wher ...
- YII2 学习
下载地址: http://www.yiiframework.com/download/ http://www.yiichina.com/download 直接选择basic模板下载即可 下载之后解压到 ...
- NIO之阻塞IO与非阻塞IO(包含Selector使用)
阻塞IO 传统的 IO 流都是阻塞式的. 也就是说,当一个线程调用 read() 或 write()时,该线程被阻塞,直到有一些数据被读取或写入,该线程在此期间不能执行其他任务. 因此,在完成网络通信 ...