顯示具有 androidNet 標籤的文章。 顯示所有文章
顯示具有 androidNet 標籤的文章。 顯示所有文章

2012年12月1日 星期六

[AndroidNet] IllegalArgumentException when http request

Error Message

java.lang.IllegalArgumentException: Illegal character in fragment at index 77: http://host/~!@#@#@$^%^%&2
at java.net.URI.create(URI.java:769)
at org.apache.http.client.methods.HttpPost.(HttpPost.java:79)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:574)
at java.lang.Thread.run(Thread.java:1020)


Solution

request url + URLEncoder.encode(parameters)

2012年6月3日 星期日

[Android] WebView WebViewClient

  • WebViewClient.onLoadResource
    • Anything loaded in webview will pass by.
  • WebViewClient.shouldOverrideUrlLoading
    • Actions occured in webview will pass by.
    • But! Standard links and server redirects trigger shouldOverrideUrlLoading(); window.location does not.

2012年4月21日 星期六

[AndroidNet] HTTP API

Android includes two HTTP clients: HttpURLConnection and Apache HTTP Client. Both support HTTPS, streaming uploads and downloads, configurable timeouts, IPv6 and connection pooling.

  • android.net.http.AndroidHttpClient - API 8 - 主要使用 Apache 的 HttpClient
    • Implementation of the Apache DefaultHttpClient that is configured with reasonable default settings and registered schemes for Android, and also lets the user add HttpRequestInterceptor classes. 
    • Don't create this directly, use the newInstance(String) factory method.

Their implementation is stable and they have few bugs. But Android Team is not actively working on Apache HTTP Client.


  • abstract java.net.URLConnection
    • Instances of URLConnection are not reusable: you must use a different instance for each connection to a resource.
    • An URLConnection for HTTP (RFC 2616) used to send and receive data over the web. Data may be of any type and length.
  • java.net.HttpURLConnection extends URLConnection
    • This class may be used to send and receive streaming data whose length is not known in advance.
    • Is a general-purpose, lightweight HTTP client suitable for most applications.
    • Prior to Froyo, HttpURLConnection had some frustrating bugs.
    • Work around this by disabling connection pooling (Refer:  Android’s HTTP Clients | Android Developers Blog)
    • In Gingerbread, we added transparent response compression.
    • In Ice Cream Sandwich, we are adding a response cache.

  • How to choose?
    • Eclair and Froyo  - Apache HTTP client that has fewer bugs.
    • For Gingerbread and better -  HttpURLConnection.
      • Its simple API and small size makes it great fit for Android.
      • Transparent compression and response caching reduce network use, improve speed and save battery.
      • Android Team will be spending our energy going forward.

* Reference
- Android’s HTTP Clients | Android Developers Blog
- Android 上的 HTTP 服務相關函式 (I)
- Android 上的 HTTP 服務相關函式 (II)
- Android 上的 HTTP 服務相關函式 (III)

2011年10月20日 星期四

[Android] Is network available?

Error Message

Occurred on Desire API 2.2

當關閉行動網路時,透過以下的方法判斷是否有網路,卻回傳 true?!

public static boolean isNetworkAvailable(Context context) {  
    ConnectivityManager connectivity = (ConnectivityManager) context  
        .getSystemService(Context.CONNECTIVITY_SERVICE);  
        if (connectivity == null) {  
        } else {  
            NetworkInfo[] info = connectivity.getAllNetworkInfo();  
            if (info != null) {  
                for (int i = 0; i < info.length; i++) {  
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {  
                        return true;  
                    }  
                }  
            }  
        }  
        return false;  
    }  

若使用飛安模式則是回傳 false,但此情況僅出現幾次, 之後則模擬不出來,所以是否有疑漏其他操作行為?


Solution

在 API 2.1, 2.2, 2.3.3 的手機上測以下三種模式下所對應的結果:

飛安模式
  • NetworkInfo: [Landroid.net.NetworkInfo;@xxxxx, 
  • activeInfo: null 
  • type: MOBILE, state: DISCONNECTED, detailed state: DISCONNECTED, isAvailable: false, isConnected: false 
  • 其他 type 的 values 皆為以下 
    • state: UNKNOWN, detailed state: IDLE, isAvailable: false, isConnected: false 


開啟行動網路 
  • NetworkInfo: [Landroid.net.NetworkInfo;@xxxx, 
  •  activeInfo: NetworkInfo: type: MOBILE[UMTS], state: CONNECTED/CONNECTED, reason: dataEnabled, extra: internet, roaming: false, failover: false, isAvailable: true 
  • type: MOBILE, state: CONNECTED, detailed state: CONNECTED, isAvailable: true 
  • 其他 type 的 values 則為以下 
    • state: UNKNOWN or DISCONNECTED, detailed state: IDLE/DISCONNECTED/CONNECTED, isAvailable: true(few types are true), isConnected: false(few types are true) 


關閉行動網路 
  • NetworkInfo: [Landroid.net.NetworkInfo;@444b0530, 
  • activeInfo: null 
  • type: MOBILE, state: DISCONNECTED, detailed state: DISCONNECTED, isAvailable: true, isConnected: false 
  • 其他 type 的 values 皆為以下
    • state: UNKNOWN/DISCONNECTED, detailed state: IDLE/DISCONNECTED, isAvailable: true(few types are false), isConnected: false 


所以 state 結果應該和我們的需求一致 ,因此猜測是否得到的是狀態正在轉換時的值,試著監聽網路狀態的改變,以下方式能得到和操作一致的結果,但不確定此種方式是否正確。
// briefly
TelephonyManager.listen(new PhoneStateListener() { 
    @Override  
    public void onDataConnectionStateChanged(int state) {  
        switch(state){  
            case TelephonyManager.DATA_DISCONNECTED:
                break;  
            case TelephonyManager.DATA_CONNECTING:
                break;  
            case TelephonyManager.DATA_CONNECTED: 
                break;  
        }  
    }                     
}, PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);  

另一種則是 onReceiver action "android.net.conn.CONNECTIVITY_CHANGE" 
但在改變任何狀態時皆收不到相關的 action!!!?


Pending Issues
監聽網路狀態 Using receiver to listen action "android.net.conn.CONNECTIVITY_CHANGE"


* Reference
- Android – 判斷手機是否連上網路 (ConnectivityManager)
- network: android 网络判断 **
- android之ConnectivityManager简介,网络连接状态
- NetworkInfo - ConnectivityManager
- 网络连接状态的监听和判断(android,wifi,Gprs)
- Android判断网络状态方法详解
- 监听手机的网络连接状态