Android HTTP请求移动数据



在我的一个应用程序中,我需要在2G/3G/4G上提出HTTP请求。如果设备都连接到WiFi和移动数据,我需要通过移动数据检查并提出HTTP请求。我不想打开/关闭wifi。

如果可以通过套接字编程完成,如果两个网络可用,我们可以在蜂窝上传输流量。

任何示例代码或教程链接都将受到高度赞赏。

尝试实现此连接Java类并检查您的移动数据/wifi。

public class Connectivity {

public static NetworkInfo getNetworkInfo(Context context){
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    return cm.getActiveNetworkInfo();
}

public static boolean isConnected(Context context){
    NetworkInfo info = Connectivity.getNetworkInfo(context);
    return (info != null && info.isConnected());
}

public static boolean isConnectedWifi(Context context){
    NetworkInfo info = Connectivity.getNetworkInfo(context);
    return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI);
}

public static boolean isConnectedMobile(Context context){
    NetworkInfo info = Connectivity.getNetworkInfo(context);
    return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_MOBILE);
}

public static boolean isConnectedFast(Context context){
    NetworkInfo info = Connectivity.getNetworkInfo(context);
    return (info != null && info.isConnected() && Connectivity.isConnectionFast(info.getType(),info.getSubtype()));
}

public static boolean isConnectionFast(int type, int subType){
    if(type==ConnectivityManager.TYPE_WIFI){
        return true;
    }else if(type==ConnectivityManager.TYPE_MOBILE){
        switch(subType){
        case TelephonyManager.NETWORK_TYPE_1xRTT:
            return false; // ~ 50-100 kbps
        case TelephonyManager.NETWORK_TYPE_CDMA:
            return false; // ~ 14-64 kbps
        case TelephonyManager.NETWORK_TYPE_EDGE:
            return false; // ~ 50-100 kbps
        case TelephonyManager.NETWORK_TYPE_EVDO_0:
            return true; // ~ 400-1000 kbps
        case TelephonyManager.NETWORK_TYPE_EVDO_A:
            return true; // ~ 600-1400 kbps
        case TelephonyManager.NETWORK_TYPE_GPRS:
            return false; // ~ 100 kbps
        case TelephonyManager.NETWORK_TYPE_HSDPA:
            return true; // ~ 2-14 Mbps
        case TelephonyManager.NETWORK_TYPE_HSPA:
            return true; // ~ 700-1700 kbps
        case TelephonyManager.NETWORK_TYPE_HSUPA:
            return true; // ~ 1-23 Mbps
        case TelephonyManager.NETWORK_TYPE_UMTS:
            return true; // ~ 400-7000 kbps
        /*
         * Above API level 7, make sure to set android:targetSdkVersion 
         * to appropriate level to use these
         */
        case TelephonyManager.NETWORK_TYPE_EHRPD: // API level 11 
            return true; // ~ 1-2 Mbps
        case TelephonyManager.NETWORK_TYPE_EVDO_B: // API level 9
            return true; // ~ 5 Mbps
        case TelephonyManager.NETWORK_TYPE_HSPAP: // API level 13
            return true; // ~ 10-20 Mbps
        case TelephonyManager.NETWORK_TYPE_IDEN: // API level 8
            return false; // ~25 kbps 
        case TelephonyManager.NETWORK_TYPE_LTE: // API level 11
            return true; // ~ 10+ Mbps
        // Unknown
        case TelephonyManager.NETWORK_TYPE_UNKNOWN:
        default:
            return false;
        }
    }else{
        return false;
    }
 }
}

并如下检查:

if(Connectivity.isConnectedWifi(Login.this)){
                //Implement your logic      
 }else if(Connectivity.isConnectedMobile(Login.this)){
                //Implement your logic              
}

更新:

尝试实现第二种方法:

我认为您不能这样做,因为在Android中,只有一个网络在任何时间点都处于活动状态。因此,首先,您需要检查哪个网络处于活动状态,然后如果它是Wi-Fi,然后断开连接,则Android将会退回到另一个将为2G/3G(如果没有其他Wi-Fi网络)可用),然后您可以发送将通过2G/3G网络的请求。IONLINE可能看起来像:

 ConnectivityManager cm = (ConnectivityManager)Context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
 if(ni == null)
//no connectivity, abort
if(ni.getType() == ConnectivityManager.TYPE_WIFI || ni.getType() == ConnectivityManager.TYPE_WIMAX) {
WifiManager wm = (WifiManager)Context.getSystemService(Context.WIFI_SERVICE);
if( wm != null)
wm.disconnect();
//this will force android to fallback to other available n/w which is 3G
}
while(true) {
NetworkInfo ni = cm.getActiveNetworkInfo();
 if(ni != null && ni.getType() == ConnectivityManager.TYPE_MOBILE && ni.isConnected())  {
  //send your http request
 break;

} //睡一段时间,以便Android可以将您连接到其他N/W}

您可能需要循环浏览所有活动的N/W并断开它们,直到找到2G/3G网络为止。我假设只有一个Wi-Fi网络和一个2G/3G网络可用。

在这里,您可以根据自己的要求进行自定义,还可以使用我的Connectivity.java类并整合Hee。

使用以下代码

public class ConnectionDetector {
private Context _context;
public ConnectionDetector(Context context) {
    this._context = context;
}

public boolean isConnectingToInternet() {
    boolean haveConnectedWifi = false;
    boolean haveConnectedMobile = false;
    ConnectivityManager cm = (ConnectivityManager) _context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] netInfo = cm.getAllNetworkInfo();
    for (NetworkInfo ni : netInfo) {
        if (ni.getTypeName().equalsIgnoreCase("WIFI"))
            if (ni.isConnected())
                haveConnectedWifi = true;
        if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
            if (ni.isConnected())
                haveConnectedMobile = true;
    }
    return haveConnectedMobile;
}
}

它只能在移动网络中连接互联网。

最新更新