學校里登錄校園網關的代碼是我之前最開始接觸python時學習的東西苗踪。不過很簡單只要四行代碼跪者。
- 將post請求body內容對進行urlencode編碼
- 發(fā)送到校園網網關的地址
之后設置開機自啟動、那樣每次開電腦就不用手動打開瀏覽器登錄網關了
這次想要實現(xiàn)android上的自動登錄妹卿。
首先是查到了PythonInterpreter
但是集成這個東西到android里面StackOverflow感覺笨笨的吧旺矾。。好像也不是啥主流的思路夺克。箕宙。
以及Qpython
但好像是主要被用來開發(fā)android的。铺纽。下載出來例子們都是怎么操作android里的相冊怎么操作藍牙啥的柬帕。。雖然確實可以用來跑腳本登錄網關狡门。陷寝。但是這會需要我打開Qpython再執(zhí)行對應程序。其馏。也不是很開心凤跑。。
算了我還是用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的事件監(jiān)聽上去
整個環(huán)節(jié)里嘗試過兩個類
if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION))
這個是網上大家用的比較多的監(jiān)聽網絡狀態(tài)
/**
* 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
也就是說要是網絡狀態(tài)改變蹈垢、并且正在嘗試進行一個網絡連接慷吊。。之后它會再次發(fā)送一個broadcast來告訴你是否連接成功曹抬。溉瓶。這只是我給出的一個看起來似乎可以解釋的答案。實際情況是兩次發(fā)送過來時NetworkInfo都會顯示已連接。堰酿。怎么辦疾宏。我也很無奈
校園網讓人難受在如果兩次請求發(fā)送的時間過于接近。触创。會讓我斷掉網坎藐。。 這個操作我在postman上也復現(xiàn)到了哼绑。
感覺有一百種可以犯的錯誤岩馍。。都是因為我的兩次請求過于接近導致的抖韩。
當然因為這個操作并不是一個正持鳎可以在瀏覽器上進行的操作。茂浮。于是具體是哪個錯我一直不知道赦肋。。
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'>繼續(xù)</a>斷開它的連接并重新輸入用戶名和密碼登陸本機囱井。");
break;
case 3:
document.write("本賬號只能在指定地址使用<br>This account can be used on the appointed address only."+pp+xip);
于是選擇WifiManager類監(jiān)聽
if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
這是另一個監(jiān)聽wifi連接狀態(tài)的
它給我請求了八次訪問驹尼。。給我一種它其實伴隨著類似于三次握手協(xié)議的變化進行state_change庞呕。然而雖然訪問次數(shù)多新翎。。但是開始的幾個很大幾率會fail住练。所以我一度認為其實當最后一次廣播時連接確實建立地啰,只有那次才會post成功。事實證明還是年輕了讲逛。亏吝。多嘗試幾次就會失敗的。盏混。
后來的答案是使用了Handler并進行一定的延時蔚鸥,當之后的廣播發(fā)送來時clear掉消息隊列。
handler.removeMessages(1);
handler.sendEmptyMessageDelayed(1, 50);//一個50毫秒的延時就夠了