public HttpURLConnection getConnection(URL url) {
String currentAPNId;//当前APN的id
String currentAPNUser;//当前APN的用户名
String currentAPNPassword;//当前APN的密码
HttpURLConnection conn = null;
if (isWIFIAvailable()) {// 判断wifi是否可用
try {
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(100000);//连接超时时间
conn.setReadTimeout(100000);
Log.v("imsi:", "imsi"+imsi);
} catch (IOException e) {
e.printStackTrace();
conn = null;
return conn;
}
return conn;
}
//获得当前的APN
Cursor mCursor_APN = tykmAndroid_Entity.getContentResolver().query(Constant.PREFERRED_APN_URI, null, null, null, null);
if (mCursor_APN == null||! mCursor_APN.moveToNext())
{
return null;
}
else
{
currentAPNId = mCursor_APN.getString(mCursor_APN.getColumnIndex("_id"));
currentAPNUser = mCursor_APN.getString(mCursor_APN.getColumnIndex("user"));
currentAPNPassword = mCursor_APN.getString(mCursor_APN.getColumnIndex("password"));
mCursor_APN.close();
}
if(!(currentAPNUser.equals("ctnet@mycdma.cn")&¤tAPNPassword.equals("vnet.mobi")||
currentAPNUser.equals("ctwap@mycdma.cn")&¤tAPNPassword.equals("vnet.mobi")))
//既不是wap又不是net上网方式需要指定上网方式
{
Cursor mCursor_APN_Defalut = tykmAndroid_Entity.getContentResolver().
query(Constant.APN_TABLE_URI, null, " user=? and password=?", new String[] { "ctnet@mycdma.cn", "vnet.mobi" }, null);
if (mCursor_APN_Defalut == null||! mCursor_APN_Defalut.moveToNext())//不存在ctnet则重新建立一个连接
{
mCursor_APN_Defalut.close();//关闭连接
String DefalutAPNId = bulidAPN_tykm();
if(DefalutAPNId==null||DefalutAPNId.equals("")||DefalutAPNId.equals(" "))//创建新的APN失败
{
return null;
}
ContentResolver resolver = tykmAndroid_Entity.getContentResolver();
ContentValues values = new ContentValues();
values.put("apn_id", DefalutAPNId);
resolver.update(Constant.PREFERRED_APN_URI, values, null, null);
}
else
{//使用系统默认的APN连接
String DefalutAPNId = mCursor_APN_Defalut.getString(mCursor_APN_Defalut.getColumnIndex("_id"));
ContentResolver resolver = tykmAndroid_Entity.getContentResolver();
ContentValues values = new ContentValues();
values.put("apn_id", DefalutAPNId);
resolver.update(Constant.PREFERRED_APN_URI, values, null, null);
mCursor_APN_Defalut.close();
// mCursor_APN_Defalut.close();//关闭连接
// String DefalutAPNId = bulidAPN_tykm();
// if(DefalutAPNId==null||DefalutAPNId.equals("")||DefalutAPNId.equals(" "))//创建新的APN失败
// {
// return null;
// }
// ContentResolver resolver = tykmAndroid_Entity.getContentResolver();
// ContentValues values = new ContentValues();
// values.put("apn_id", DefalutAPNId);
// resolver.update(Constant.PREFERRED_APN_URI, values, null, null);
}
try {
Thread.sleep(8000L);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if ((android.net.Proxy.getDefaultHost()) != null)// wap需要代理
{
try {
java.net.Proxy p = new java.net.Proxy(java.net.Proxy.Type.HTTP,
new InetSocketAddress(android.net.Proxy
.getDefaultHost(), android.net.Proxy
.getDefaultPort()));
conn = (HttpURLConnection) url.openConnection(p);
conn.setConnectTimeout(100000);
conn.setReadTimeout(100000);
Log.v("imsi:", ""+imsi);
Log.v("http", "httpdaili");
} catch (Exception e) {
conn = null;
}
return conn;
}
if ((android.net.Proxy.getDefaultHost()) == null)// net不需要代理
{
try {
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(100000);
Log.v("imsi:", "imsi"+imsi);
Log.v("http", "nohttpdaili");
} catch (Exception e) {
conn = null;
}
return conn;
}
return conn;
}
private static String bulidAPN_tykm() {
// TODO Auto-generated method stub
ContentResolver resolver = tykmAndroid_Entity.getContentResolver();
ContentValues values = new ContentValues();
values.put("name", "tykmAPN");
values.put("apn", "#777");
//values.put("proxy", apnNode.getProxy());
//values.put("port", apnNode.getPort());
values.put("user", "ctnet@mycdma.cn");
values.put("password", "vnet.mobi");
values.put("mcc", getMCC());
values.put("mnc", getMNC());
values.put("numeric", getSimOperator());
Cursor c = null;
String defaultAPNId = "";
try
{
Uri newRow = resolver.insert(Constant.APN_TABLE_URI, values);
if (newRow != null)
{
c = resolver.query(newRow, null, null, null, null);
int idindex = c.getColumnIndex("_id");
c.moveToFirst();
defaultAPNId = c.getString(idindex);
Log.d("Robert", "New ID: " + defaultAPNId + ": Inserting new APN succeeded!");
}
}
catch (SQLException e)
{
defaultAPNId = "";
}
if (c != null)
c.close();
return defaultAPNId;
}
private static String getMCC()
{
TelephonyManager tm = (TelephonyManager) tykmAndroid_Entity.getSystemService(Context.TELEPHONY_SERVICE);
String numeric = tm.getSimOperator();
String mcc = numeric.substring(0, 3);
Log.i("MCC is", mcc);
return mcc;
}
private static String getMNC()
{
TelephonyManager tm = (TelephonyManager) tykmAndroid_Entity.getSystemService(Context.TELEPHONY_SERVICE);
String numeric = tm.getSimOperator();
String mnc = numeric.substring(3, numeric.length());
Log.i("MNC is", mnc);
return mnc;
}
private static String getSimOperator()
{
TelephonyManager tm = (TelephonyManager) tykmAndroid_Entity.getSystemService(Context.TELEPHONY_SERVICE);
String SimOperator = tm.getSimOperator();
return SimOperator;
}
private static boolean isWIFIAvailable() {// 判断wifi是否可用
for (int i = 0; i < 5; i++) {
ConnectivityManager cwjManager = (ConnectivityManager) tykmAndroid.tykmAndroid_Entity
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiInfo = cwjManager.getActiveNetworkInfo();
if (wifiInfo == null) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
continue;
} else {
if (wifiInfo.getType() == ConnectivityManager.TYPE_WIFI)
return true;
}
}
return false;
}
public static boolean isNetWorkAvailable() // 判断有没有网络可以使用
{
for (int i = 0; i < 5; i++) {
ConnectivityManager cwjManager = (ConnectivityManager) tykmAndroid.tykmAndroid_Entity
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cwjManager.getActiveNetworkInfo();
if (netInfo == null) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
continue;
} else {
if (netInfo.isAvailable())
{
return true;
}
else
continue;
}
}
errorInfo="网络不可用,请查看是否已建立数据连接,或您的手机信号太弱,请您稍后再试";
tykmAndroid.tykmAndroid_Entity.handler.sendEmptyMessage(6011);
return false;
}