我在我的应用中使用以下代码:
if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
...
通常它工作正常,但从wifi切换到3G后我得到以下异常:
21:04:21.358: I/WifiService(2277): setShowAccessPointListDialog():false
21:04:21.368: I/WifiService(2277): setWifiEnabledDialog(): false
21:04:21.393: I/WifiHW(2277): wifi_stop_supplicant: Enter
21:04:21.393: I/WifiHW(2277): wifi_stop_supplicant: connecting supplicant is done
21:04:21.393: I/WifiHW(2277): wifi_stop_supplicant: status of supplicant(connected), count(49)
21:04:21.393: I/wpa_supplicant(22752): CTRL-EVENT-TERMINATING - signal 15 received
21:04:21.393: I/wpa_supplicant(22752): CTRL-EVENT-TERMINATING - signal 0 received
21:04:21.393: I/WifiMonitor(2277): Event [CTRL-EVENT-TERMINATING - signal 15 received]
21:04:21.393: V/WifiStateTracker(2277): Connection to supplicant lost
21:04:21.418: I/wpa_supplicant(22752): CTRL-EVENT-STATE-CHANGE id=1 state=0 BSSID=00:00:00:00:00:00
21:04:22.443: I/dhcpcd(22766): received SIGTERM, stopping
21:04:22.508: I/WifiHW(2277): wifi_stop_supplicant: exit normally
21:04:22.508: D/WifiStateTracker(2277): Reset connections and stopping DHCP
21:04:22.513: I/WifiHW(2277): wifi_close_supplicant_connection: enter
21:04:22.513: I/WifiHW(2277): wifi_close_supplicant_connection: exit
21:04:22.528: D/NetworkStateTracker(2277): setDetailed state, old =CONNECTED and new state=DISCONNECTED
21:04:22.528: D/ConnectivityService(2277): ConnectivityChange for WIFI: DISCONNECTED/DISCONNECTED
21:04:22.528: D/ConnectivityService(2277): getMobileDataEnabled returning true
21:04:22.533: D/DataConnectionTracker(2332): setEnabled(0, true) with old state = false and enabledCount = 0
21:04:22.558: V/ConnectivityService(2277): Attempting to switch to mobile
21:04:22.578: D/MobileDataStateTracker(2277): default Received state= DISCONNECTED, old= DISCONNECTED, reason= apnSwitched, apnTypeList= default
21:04:22.583: D/Tethering(2277): Tethering got CONNECTIVITY_ACTION
21:04:22.583: D/Tethering(2277): MasterInitialState.processMessage what=3
21:04:22.583: E/HierarchicalStateMachine(2277): TetherMaster - unhandledMessage: msg.what=3
21:04:22.618: D/MobileDataStateTracker(2277): default Received state= DISCONNECTED, old= DISCONNECTED, reason= apnSwitched, apnTypeList= default
21:04:22.638: D/GpsLocationProvider(2277): updateNetworkState available info: NetworkInfo: type: WIFI[], state: DISCONNECTED/DISCONNECTED, reason: (unspecified), extra: (none), roaming: false, failover: false, isAvailable: false
21:04:22.658: D/WifiStateTracker(2277): Disabling interface
21:04:22.658: I/WifiHW(2277): wifi_unload_driver: enter
21:04:22.663: D/MobileDataStateTracker(2277): default Received state= DISCONNECTED, old= DISCONNECTED, reason= apnSwitched, apnTypeList= default
21:04:22.723: D/MobileDataStateTracker(2277): default Received state= CONNECTING, old= DISCONNECTED, reason= apnSwitched, apnTypeList= default,supl
21:04:22.723: D/NetworkStateTracker(2277): setDetailed state, old =DISCONNECTED and new state=CONNECTING
21:04:22.728: D/ConnectivityService(2277): ConnectivityChange for mobile: CONNECTING/CONNECTING
21:04:22.773: I/GTalkService(22533): [ServiceAutoStarter] --- connectivity changed
21:04:22.773: I/GTalkService(22533): [ServiceAutoStarter] --- start GTalk service ---
21:04:22.883: D/NetworkLocationProvider(2277): onDataConnectionStateChanged 3
21:04:22.888: D/NetworkLocationProvider(2277): onDataConnectionStateChanged 3
21:04:22.893: D/NetworkLocationProvider(2277): onDataConnectionStateChanged 3
21:04:22.893: D/NetworkLocationProvider(2277): onDataConnectionStateChanged 3
21:04:23.453: E/WifiHW(2277): [WIFI] Unload Driver
21:04:23.453: I/WifiHW(2277): wifi_unload_driver: exit
21:04:23.493: I/DBG_WSS_DM(22809): [wssStartService.java Line:1413] ProtoIsWIFIConnected WiFi DisConnected
21:04:23.588: I/DBG_WSS_DM(22809): [wssTelephonyData.java Line:131] getInstance PHONE_TYPE_GSM
21:04:23.758: V/SettingsProvider(2277): secure <- value=0 name=wifi_on
21:04:23.758: V/SettingsProvider(2277): property: sys.settings_secure_version=42
21:04:23.768: V/SettingsProvider(2277): notifying: content://settings/secure/wifi_on
21:04:24.483: D/Tethering(2277): pdp0 is not a tetherable iface, ignoring
21:04:24.538: E/DataRouter(2171): [*] Received suspend/ resume event but DUN is not up so neglect
21:04:24.548: D/MobileDataStateTracker(2277): replacing old mInterfaceName (pdp0) with pdp0 for hipri
21:04:24.553: D/MobileDataStateTracker(2277): replacing old mInterfaceName (pdp0) with pdp0 for supl
21:04:24.553: D/MobileDataStateTracker(2277): default Received state= CONNECTED, old= CONNECTING, reason= apnSwitched, apnTypeList= default,supl
21:04:24.563: D/MobileDataStateTracker(2277): CONNECTED interface name pdp0
21:04:24.563: D/MobileDataStateTracker(2277): -> net.pdp0.gw: 100.97.40.1
21:04:24.563: D/MobileDataStateTracker(2277): -> mDefaultGatewayAddr: 19423588
21:04:24.563: D/MobileDataStateTracker(2277): -------------------------
21:04:24.563: D/MobileDataStateTracker(2277): @net.pdp0.gw: 100.97.40.1
21:04:24.563: D/MobileDataStateTracker(2277): @net.pdp1.gw:
21:04:24.563: D/MobileDataStateTracker(2277): @net.pdp2.gw:
21:04:24.563: D/MobileDataStateTracker(2277): -------------------------
21:04:24.568: D/NetworkStateTracker(2277): setDetailed state, old =CONNECTING and new state=CONNECTED
21:04:24.568: V/NetworkStateTracker(2277): Setting TCP values: [4094,87380,110208,4096,16384,110208] which comes from [net.tcp.buffersize.umts]
21:04:24.568: D/ConnectivityService(2277): ConnectivityChange for mobile: CONNECTED/CONNECTED
21:04:24.598: I/DBG_WSS_DM(22809): [wssTelephonyData.java Line:131] getInstance PHONE_TYPE_GSM
21:04:24.598: D/ConnectivityService(2277): adding dns 10.74.210.210 for mobile
21:04:24.608: D/NetworkStateTracker(2277): addDefaultRoute for mobile(pdp0), GatewayAddr=19423588 mDefaultRouteSet=false
21:04:24.608: D/NetworkStateTracker(2277): addDefaultRoute for mobile (pdp0), GatewayAddr=19423588
21:04:24.608: D/ConnectivityService(2277): adding dns 10.74.210.211 for mobile
21:04:24.623: D/Tethering(2277): Tethering got CONNECTIVITY_ACTION
21:04:24.628: D/Tethering(2277): MasterInitialState.processMessage what=3
21:04:24.628: E/HierarchicalStateMachine(2277): TetherMaster - unhandledMessage: msg.what=3
21:04:24.733: D/Smack(22533): [XMPPConn] close connection, notifyClosed=false
21:04:24.748: D/GpsLocationProvider(2277): updateNetworkState available info: NetworkInfo: type: mobile[UMTS], state: CONNECTED/CONNECTED, reason: apnSwitched, extra: internet.t-mobile, roaming: false, failover: false, isAvailable: true
21:04:24.798: I/DBG_WSS_TP(22809): [wssHttpAdapter.java Line:1722] getIsConnected connect status is false
21:04:24.808: I/DBG_WSS_DM(22809): [wssStartService.java Line:1413] ProtoIsWIFIConnected WiFi DisConnected
21:04:24.808: D/NetworkLocationProvider(2277): onDataConnectionStateChanged 3
21:04:24.923: W/Smack/Packet(22533): notify conn break (IOEx), close connection
21:04:24.928: I/GTalkService(22533): [ServiceAutoStarter] --- connectivity changed
21:04:24.928: I/GTalkService(22533): [ServiceAutoStarter] --- start GTalk service ---
21:04:32.948: I/DBG_WSS_TP(22809): [wssHttpAdapter.java Line:1722] getIsConnected connect status is false
21:04:32.988: D/NetworkLocationProvider(2277): onDataConnectionStateChanged 3
21:04:36.008: I/DBG_WSS_TP(22809): [wssHttpAdapter.java Line:1722] getIsConnected connect status is false
21:04:36.013: D/NetworkLocationProvider(2277): onDataConnectionStateChanged 3
21:04:41.868: V/MyAppIntentService(22454): MyAppIntentService
21:04:41.943: V/MyAppIntentService(22454): myAppUpdate isOnline
21:04:42.003: V/MyAppIntentService(22454): myAppUpdate moveToNext
21:05:12.493: W/IInputConnectionWrapper(22454): getExtractedText on inactive InputConnection
21:05:12.498: W/IInputConnectionWrapper(22454): getExtractedText on inactive InputConnection
21:05:17.643: D/GoogleLoginService(22533): onBind: Intent { act=android.accounts.AccountAuthenticator cmp=com.google.android.gsf/.loginservice.GoogleLoginService }
21:06:56.483: D/NativeCrypto(22533): Freeing OpenSSL session
21:26:51.628: W/System.err(22454): java.net.SocketException: Connection timed out
21:26:51.668: W/System.err(22454): at org.apache.harmony.luni.platform.OSNetworkSystem.readSocketImpl(Native Method)
21:26:51.673: W/System.err(22454): at org.apache.harmony.luni.platform.OSNetworkSystem.read(OSNetworkSystem.java:358)
21:26:51.683: W/System.err(22454): at org.apache.harmony.luni.net.PlainSocketImpl.read(PlainSocketImpl.java:561)
21:26:51.688: W/System.err(22454): at org.apache.harmony.luni.net.SocketInputStream.read(SocketInputStream.java:61)
21:26:51.703: W/System.err(22454): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.readln(HttpURLConnectionImpl.java:1279)
21:26:51.708: W/System.err(22454): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.readServerResponse(HttpURLConnectionImpl.java:1351)
21:26:51.718: W/System.err(22454): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.sendRequest(HttpURLConnectionImpl.java:1339)
21:26:51.728: W/System.err(22454): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.doRequestInternal(HttpURLConnectionImpl.java:1656)
21:26:51.733: W/System.err(22454): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.doRequest(HttpURLConnectionImpl.java:1649)
21:26:51.743: W/System.err(22454): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:1374)
21:26:51.748: W/System.err(22454): at com.dummy.myApp.MyAppIntentService.myAppUpdate(MyAppIntentService.java:117)
21:26:51.753: W/System.err(22454): at com.dummy.myApp.MyAppIntentService.onHandleIntent(MyAppIntentService.java:85)
21:26:51.758: W/System.err(22454): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:59)
21:26:51.768: W/System.err(22454): at android.os.Handler.dispatchMessage(Handler.java:99)
21:26:51.773: W/System.err(22454): at android.os.Looper.loop(Looper.java:123)
21:26:51.783: W/System.err(22454): at android.os.HandlerThread.run(HandlerThread.java:60)
21:26:51.783: V/MyAppIntentService(22454): myAppUpdate moveToNext
21:26:53.943: I/DBG_WSS_TP(22809): [wssHttpAdapter.java Line:1722] getIsConnected connect status is false
21:26:53.948: D/NetworkLocationProvider(2277): onDataConnectionStateChanged 3
21:26:55.723: V/MyAppIntentService(22454): myAppUpdate HTTP_OK
21:26:56.583: V/MyAppIntentService(22454): myAppUpdate moveToNext
21:26:57.943: V/MyAppIntentService(22454): myAppUpdate HTTP_OK
21:26:58.773: V/MyAppIntentService(22454): myAppUpdate moveToNext
21:26:59.983: V/MyAppIntentService(22454): myAppUpdate HTTP_OK
21:27:00.893: V/MyAppIntentService(22454): myAppUpdate moveToNext
21:27:02.253: V/MyAppIntentService(22454): myAppUpdate HTTP_OK
为什么在将连接类型从wifi更改为3G后它无效?当从3G切换到wifi时,一切正常。
异常后它工作正常(HTTP_OK)。当我在异常发生之前杀死MyAppIntentService并再次重启时,它也可以正常工作。
答案 0 :(得分:0)
可能是您的运营商阻止了连接。测试以使用浏览器获取资源。
或者您的连接非常慢,请尝试更改: HttpConnectionParams.setConnectionTimeout(httpParameters,CONNECTION_TIMEOUT);
答案 1 :(得分:0)
我找到了解决方案here。添加所描述的变通方法后,它可以工作:
...
httpConnection.setRequestProperty("connection", "close");
if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
...