public class NetTool {
private static final String TAG = "RequestNet";
private static final char HEX_DIGITS[] = { '0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
public static int reqPost(Context context, String url,
ArrayList<BasicNameValuePair> params, DefaultJsonData jsonData,
boolean saveCache, String fileName) {
if (url == null
|| !(url.startsWith("http://") || url.startsWith("https://"))) {
Log.e(TAG, "协议错误!url应以\"http://\"开头,或以\"https://\"开头");
return -1;
}
int requestValue = 2;
System.out.println("post请求url:"+ url);
if (params == null)
params = new ArrayList<BasicNameValuePair>();
for (BasicNameValuePair pair : params) {
System.out.println("post请求参数:"+pair.getName() + ": " + pair.getValue());
}
HttpPost httpPost = new HttpPost(url);
BasicHttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 60000);
HttpConnectionParams.setSoTimeout(httpParams, 0);
HttpClient httpClient = new DefaultHttpClient(httpParams);
HttpResponse response = null;
try {
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params,
HTTP.UTF_8);
httpPost.setEntity(entity);
System.out.println("post请求start:"+ "connect");
response = httpClient.execute(httpPost);
System.out.println("post请求end:"+ "end connect");
System.out.println("post请求statu:"+ response.getStatusLine().getStatusCode() + "");
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
requestValue = 1;
} else {
requestValue = 2;
}
} catch (SocketTimeoutException e) {
e.printStackTrace();
requestValue = 3;
} catch (Exception e) {
e.printStackTrace();
requestValue = 2;
}
if (requestValue == 1) {// 请求成功
try {
HttpEntity entity = response.getEntity();
String buffData = EntityUtils.toString(entity, HTTP.UTF_8)
.trim();
if (saveCache) {
saveJsonToSDCard(buffData, fileName);
}
System.out.println("buffData:"+ buffData);
if (jsonData != null) {
if (buffData.startsWith("{")) {
JSONObject object = new JSONObject(buffData.toString());
jsonData.parse(object);
} else if (buffData.startsWith("[")) {
JSONArray object = new JSONArray(buffData.toString());
jsonData.parse(object);
}
}
} catch (Exception e) {
}
} else if (requestValue == 2) {// 请求失败
} else if (requestValue == 3) {// 超时
}
return requestValue;
}
/**
* 将json保存到sd卡
*/
public static void saveJsonToSDCard(String data, String fileName) {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
String sdPath = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/jdker/";
File file = new File(sdPath);
if (!file.exists())
file.mkdirs();
File jsonFlie = new File(file, fileName);
try {
jsonFlie.createNewFile();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
FileOutputStream outStream = new FileOutputStream(jsonFlie);
outStream.write(data.getBytes());
outStream.flush();
outStream.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 没有网络对话框
*
* @param con
*/
public static void netError(Context con) {
AlertDialog.Builder adb = new AlertDialog.Builder(con);
adb.setTitle("温馨提示");
adb.setMessage("没有网络");
adb.setPositiveButton("确定", null);
adb.show();
}
/**
* 判断网络是否可用
*
* @param context
* @return
*/
public static boolean isAccessNetwork(Context context) {
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity.getActiveNetworkInfo() != null
&& connectivity.getActiveNetworkInfo().isAvailable()) {
return true;
}
return false;
}
/**
* MD5加密字符
*/
public static String md5(String source) {
/*
* if (true) { // true表示在测试阶�?return source; } else {
*/
// System.out.println("md5source:" + source);
try {
MessageDigest digest = MessageDigest.getInstance("md5");
digest.update(source.getBytes());
byte[] mess = digest.digest();
return toHexString(mess);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return source;
// }
}
public static String toHexString(byte[] b) { // byte to String
StringBuilder sb = new StringBuilder(b.length * 2);
for (int i = 0; i < b.length; i++) {
sb.append(HEX_DIGITS[(b[i] & 0xf0) >>> 4]);
sb.append(HEX_DIGITS[b[i] & 0x0f]);
}
return sb.toString();
}
public static String getMacFromWifi(Context context){
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if (!wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(true);
}
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
String mResult = wifiInfo.getMacAddress();
Log.i("tag_net_wifi","Mac address(wifi): "+mResult);
return mResult;
}
}
httppsot android 检查网络状态 发送服务器请求
最新推荐文章于 2024-08-18 22:01:07 发布