判断网络是否可用
来源:互联网 发布:染色体分析软件 编辑:程序博客网 时间:2024/05/17 08:55
1. **1.判断网络是否可用:**
---------------
private boolean isNet(Context context) {
ConnectivityManager con = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkinfo = con.getActiveNetworkInfo();
if (networkinfo == null || !networkinfo.isAvailable()) {
// 当前网络不可用
return false;
}
return true;
}
1. **post requets**
----------------
public String connect(String url, Map<String, Object> values, Context context) {
if (!isNet(context)) {
return "";
}
HttpClient httpClient = getHttpClient();
String result = "";
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = null;
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
if (values != null && !values.isEmpty()) {
// if (!CompareUtils.isEmpty(values)) {
Set<Entry<String, Object>> set = values.entrySet();
for (Entry<String, Object> entry : set) {
parameters.add(new BasicNameValuePair(entry.getKey(), String.valueOf(entry.getValue())));
}
}
try {
Log.i("tag", "jsonParam" + parameters.toString());
httpPost.setEntity(new UrlEncodedFormEntity(parameters, HTTP.UTF_8));
httpResponse = httpClient.execute(httpPost);
int code = httpResponse.getStatusLine().getStatusCode();
if (code == HttpStatus.SC_OK) {
result = EntityUtils.toString(httpResponse.getEntity());
Log.i("tag", "jsonResult" + result.toString());
} else {
Log.i("json_error", url + "____网络:" + httpResponse.getStatusLine().getStatusCode());
}
} catch (ConnectTimeoutException e) {
e.printStackTrace();
return "";
} catch (SocketTimeoutException e) {
e.printStackTrace();
return "";
} catch (Exception e) {
e.printStackTrace();
return "";
} finally {
httpClient.getConnectionManager().shutdown();
}
return result;
}
---------------
private boolean isNet(Context context) {
ConnectivityManager con = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkinfo = con.getActiveNetworkInfo();
if (networkinfo == null || !networkinfo.isAvailable()) {
// 当前网络不可用
return false;
}
return true;
}
1. **post requets**
----------------
public String connect(String url, Map<String, Object> values, Context context) {
if (!isNet(context)) {
return "";
}
HttpClient httpClient = getHttpClient();
String result = "";
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = null;
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
if (values != null && !values.isEmpty()) {
// if (!CompareUtils.isEmpty(values)) {
Set<Entry<String, Object>> set = values.entrySet();
for (Entry<String, Object> entry : set) {
parameters.add(new BasicNameValuePair(entry.getKey(), String.valueOf(entry.getValue())));
}
}
try {
Log.i("tag", "jsonParam" + parameters.toString());
httpPost.setEntity(new UrlEncodedFormEntity(parameters, HTTP.UTF_8));
httpResponse = httpClient.execute(httpPost);
int code = httpResponse.getStatusLine().getStatusCode();
if (code == HttpStatus.SC_OK) {
result = EntityUtils.toString(httpResponse.getEntity());
Log.i("tag", "jsonResult" + result.toString());
} else {
Log.i("json_error", url + "____网络:" + httpResponse.getStatusLine().getStatusCode());
}
} catch (ConnectTimeoutException e) {
e.printStackTrace();
return "";
} catch (SocketTimeoutException e) {
e.printStackTrace();
return "";
} catch (Exception e) {
e.printStackTrace();
return "";
} finally {
httpClient.getConnectionManager().shutdown();
}
return result;
}
0 0
- 判断网络是否可用
- 判断网络是否可用
- 判断网络是否可用
- 判断网络是否可用
- 判断网络是否可用
- 判断网络是否可用
- 判断网络是否可用
- 判断网络是否可用
- 判断网络是否可用
- 判断网络是否可用
- 判断网络是否可用
- 判断网络是否可用
- 判断是否有可用网络
- 判断数据网络是否可用
- android 判断网络是否可用
- 判断当前网络是否可用
- Android判断网络是否可用
- Android 判断网络是否可用
- Linux之Makefile(Makefile规则)
- eclipse中的java文件图标变成成空心j
- java反射的学习
- Mybatis入门实例(二)
- POJ C Looooops
- 判断网络是否可用
- 【DP+预处理|最长连续子序列】HDU-1505 City Game
- CUDA矩阵乘法——VS2010中使用CUDA示例
- dos下查看端口是否被占用,及80端口被System进程占用的解决方法
- Java DateTime 日期时间处理
- 基本的socket函数
- Apache POI组件操作Excel,制作报表(二)
- cuda向量加法时间
- hdu 5119 ^和>=m情况数+dp