我在一个安卓应用程序中使用wifi 4.1,并试图让我的应用程序的HttpClient
请求被其中一个“你需要登录或支付wifi”的屏幕拦截。
我想弹出一个网页视图,允许用户登录。
我一直在尝试拦截httpClient中的重定向,但没有成功。
这就是我目前正在尝试的:
this.client = new DefaultHttpClient(connectionManager, params);
((DefaultHttpClient) this.client).setRedirectStrategy(new DefaultRedirectStrategy() {
public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) {
boolean isRedirect = Boolean.FALSE;
try {
isRedirect = super.isRedirected(request, response, context);
} catch (ProtocolException e) {
Log.e(TAG, "Failed to run isRedirected", e);
}
if (!isRedirect) {
int responseCode = response.getStatusLine().getStatusCode();
if (responseCode == 301 || responseCode == 302) {
throw new WifiLoginNeeded();
// The "real implementation" should return true here..
}
} else {
throw new WifiLoginNeeded();
}
return isRedirect;
}
});
然后在我的活动中:
try {
response = httpClient.get(url);
} catch (WifiLoginNeeded e){
showWifiLoginScreen();
}
其中show wifi屏幕执行以下操作:
Uri uri = Uri.parse("http://our-site.com/wifi-login");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
想法是这样的:
问题是,我从来没有得到过WifiLoginNeeded异常。使用Android实现这一点的首选方法是什么?
发布于 2013-02-27 05:13:00
作为更一般的情况:您可以在您的公共API中包含一些内容,如“Hello World”或“Ping”消息。
GET http://api.example.com/1.0/ping HTTP/1.1
Accepts: text/json
Content-Type: text/json
{ ping: "ok", currentAPIVersion: 1.1, minAPIVersion: 1.0 }
尝试连接并“ping”您的服务器。如果响应没有以您期望的格式返回,那么抛出相同的URL作为用户使用他们选择的web浏览器处理结果的意图。
这也将处理:公司代理(“company XYZ禁止您!”),以及将来您自己的API中的不兼容更改(“对不起,我们不再支持那个12年前的API版本。请在此处下载更新。“)
发布于 2012-10-25 20:42:52
试试这个小的解决方案吧,它会有帮助的
public boolean haveNetworkConnection() {
cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
Boolean haveConnectedWifi = false;
Boolean haveConnectedMobile = false;
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 haveConnectedWifi || haveConnectedMobile;
}
发布于 2012-03-27 16:02:04
请检查以下代码。检查您使用的是哪种连接可能对您很有用。
WifiManager lWifiManager = (WifiManager) OpenYouTubePlayerActivity.this.getSystemService(Context.WIFI_SERVICE);
TelephonyManager lTelephonyManager = (TelephonyManager) OpenYouTubePlayerActivity.this.getSystemService(Context.TELEPHONY_SERVICE);
////////////////////////////
// if we have a fast connection (wifi or 3g)
if( (lWifiManager.isWifiEnabled() && lWifiManager.getConnectionInfo() != null && lWifiManager.getConnectionInfo().getIpAddress() != 0) ||
( (lTelephonyManager.getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS ||
/* icky... using literals to make backwards compatible with 1.5 and 1.6 */
lTelephonyManager.getNetworkType() == 9 /*HSUPA*/ ||
lTelephonyManager.getNetworkType() == 10 /*HSPA*/ ||
lTelephonyManager.getNetworkType() == 8 /*HSDPA*/ ||
lTelephonyManager.getNetworkType() == 5 /*EVDO_0*/ ||
lTelephonyManager.getNetworkType() == 6 /*EVDO A*/)
&& lTelephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED)
){
//Do some thing here
}
https://stackoverflow.com/questions/9831266
复制相似问题