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;
}