判断网络是否可用
2014-12-04 22:28
176 查看
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;
}
相关文章推荐
- 精确的判断网络是否可用(转贴)
- Android中判断网络功能是否可用
- 判断网络是否可用
- android 判断网络是否可用,并调用系统设置项
- Android判断当前网络是否可用--示例代码
- android 判断是否有可用网络
- 判断网络是否可用
- android 程序中判断当前是否连接网络,网络是否可用
- Android判断当前网络是否可用--示例代码
- Android中判断有无可用网络(是否是3G或者WIFI网络)
- 网络监听以及判断网络是否可用
- [C#]精确的判断网络是否可用
- C# 判断计算机网络是否可用
- Android中判断有无可用网络(是否是3G或者WIFI网络)
- 判断SDCARD扫描状态和是否存在,网络是否可用
- 判断数据网络是否可用
- 精确的判断网络是否可用
- Android中判断网络连接是否可用
- Android判断GPS、wifi、移动网络是否可用的探知函数
- android中判断网络连接是否可用