使用 GSM 网络而非 WiFi 建立 HTTP 连接

发布于 2024-09-25 21:42:08 字数 169 浏览 2 评论 0原文

我需要使用 GSM 连接而不是 Wifi 在 Android 上发出 HTTP 请求。

我当前的解决方案是断开所有 Wi-Fi 连接并执行请求。还有更好的解决办法吗?我在API中找不到任何相关方法(我查看了包org.apache.http,但似乎完全不知道应该使用什么类型的连接)。

I need to make HTTP request on Android using GSM connection, not Wifi.

My current solution is to disconnect from all wi-fi connections and perform a request. Is there any better solution? I could not find any relevant methods in the API (I looked in package org.apache.http, but it seems it is completely unaware of what type of connection should be used).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挥剑断情 2024-10-02 21:42:08

如果应用程序需要在启动时或用户触发此操作时立即通过 GSM 提交 HTTP 请求,那么按照问题中的建议进行操作是完美的。

但如果应用程序需要在连接类型为 GSM 时提交 HTTP 请求,那就有点不同了。在最后一种情况下我会这样做:

private static boolean isOnlineUsingGsm(Context ctx) {
    final ConnectivityManager connectManager = (ConnectivityManager)ctx.getSystemService(Context.CONNECTIVITY_SERVICE);

    // Return true if connected thru GSM
    return connectManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED;
}

private void somewhereInTheCode() {
    if (isOnlineUsingGsm(context)) {
        sendHttpRequest();
    }
    // else don't send it
}

Doing as proposed in the question is perfect should the application need to submit the HTTP request thru GSM now, at the time it is started or at the time the user triggers this action.

But if the app needs to do submit an HTTP request when the connection type is GSM that's a bit different. In that last case I would do that way:

private static boolean isOnlineUsingGsm(Context ctx) {
    final ConnectivityManager connectManager = (ConnectivityManager)ctx.getSystemService(Context.CONNECTIVITY_SERVICE);

    // Return true if connected thru GSM
    return connectManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED;
}

private void somewhereInTheCode() {
    if (isOnlineUsingGsm(context)) {
        sendHttpRequest();
    }
    // else don't send it
}
纸伞微斜 2024-10-02 21:42:08

但它似乎完全不知道应该使用什么类型的连接

正确使用哪种类型的连接 - 间接是你的朋友 - 位置服务(例如)是一样的

but it seems it is completely unaware of what type of connection should be used

correct - indirection is your friend - Location Services (for example) are just the same

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文