学校里登录校园网关的代码是我之前最开始接触python时学习的东西。不过很简单只要四行代码。
- 将post请求body内容对进行urlencode编码
- 发送到校园网网关的地址
之后设置开机自启动、那样每次开电脑就不用手动打开浏览器登录网关了
这次想要实现android上的自动登录。
首先是查到了PythonInterpreter
但是集成这个东西到android里面StackOverflow感觉笨笨的吧。。好像也不是啥主流的思路。。
以及Qpython
但好像是主要被用来开发android的。。下载出来例子们都是怎么操作android里的相册怎么操作蓝牙啥的。。虽然确实可以用来跑脚本登录网关。。但是这会需要我打开Qpython再执行对应程序。。也不是很开心。。
算了我还是用java访问网关来登录吧祭出Retrofit
我这个水平也就考虑不到什么场景适合用什么框架了。。总共用过它和volley。。后来感觉volley确实被评价地不如它了。。。
public interface networkApis {
@FormUrlEncoded
@POST
Call<ResponseBody> getLogin(@Url String url,
@Field("DDDDD") String id,
@Field("upass")String loginPwd,
@Field("0MKKey")String boo);
}
简单定义一个接口。。很快的。
这个时候重点应该转移到android的事件监听上去
整个环节里尝试过两个类
if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION))
这个是网上大家用的比较多的监听网络状态
/**
* A change in network connectivity has occurred. A default connection has either
* been established or lost. The NetworkInfo for the affected network is
* sent as an extra; it should be consulted to see what kind of
* connectivity event occurred.
* <p/>
* If this is a connection that was the result of failing over from a
* disconnected network, then the FAILOVER_CONNECTION boolean extra is
* set to true.
* <p/>
* For a loss of connectivity, if the connectivity manager is attempting
* to connect (or has already connected) to another network, the
* NetworkInfo for the new network is also passed as an extra. This lets
* any receivers of the broadcast know that they should not necessarily
* tell the user that no data traffic will be possible. Instead, the
* receiver should expect another broadcast soon, indicating either that
* the failover attempt succeeded (and so there is still overall data
* connectivity), or that the failover attempt failed, meaning that all
* connectivity has been lost.
* <p/>
* For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY
* is set to {@code true} if there are no connected networks at all.
*/
@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
有关这个字段的详细信息。我的问题主要是在这个地方
* Instead, the receiver should expect another broadcast soon, indicating either that the failover attempt succeeded
也就是说要是网络状态改变、并且正在尝试进行一个网络连接。。之后它会再次发送一个broadcast来告诉你是否连接成功。。这只是我给出的一个看起来似乎可以解释的答案。实际情况是两次发送过来时NetworkInfo都会显示已连接。。怎么办。我也很无奈
校园网让人难受在如果两次请求发送的时间过于接近。。会让我断掉网。。 这个操作我在postman上也复现到了。
感觉有一百种可以犯的错误。。都是因为我的两次请求过于接近导致的。
当然因为这个操作并不是一个正常可以在浏览器上进行的操作。。于是具体是哪个错我一直不知道。。
switch(Msg){
case 0:
case 1:
if((Msg==1)&&(msga!="")){
switch(msga){
case 'error0':
document.write("本IP不允许Web方式登录<br>The IP does not allow Web-log");
break;
case 'error1':
document.write("本账号不允许Web方式登录<br>The account does not allow Web-log");
break;
case 'error2':
document.write("本账号不允许修改密码<br>This account does not allow change password");
break;
default:
document.write(msga);
break;}
}
else document.write("账号或密码不对,请重新输入<br>Ivalid account or password, please login again");
break;
case 2:
document.write("该账号正在IP为:"+xip+"的机器上使用,<br><br>请点击<a href='a11.htm'>继续</a>断开它的连接并重新输入用户名和密码登陆本机。");
break;
case 3:
document.write("本账号只能在指定地址使用<br>This account can be used on the appointed address only."+pp+xip);
于是选择WifiManager类监听
if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
这是另一个监听wifi连接状态的
它给我请求了八次访问。。给我一种它其实伴随着类似于三次握手协议的变化进行state_change。然而虽然访问次数多。。但是开始的几个很大几率会fail。所以我一度认为其实当最后一次广播时连接确实建立,只有那次才会post成功。事实证明还是年轻了。。多尝试几次就会失败的。。
后来的答案是使用了Handler并进行一定的延时,当之后的广播发送来时clear掉消息队列。
handler.removeMessages(1);
handler.sendEmptyMessageDelayed(1, 50);//一个50毫秒的延时就够了
网友评论