2015年10月29日 星期四

[Android] java.io.FileNotFoundException AND android.os.NetworkOnMainThreadException!!

1.Android 4.0 has a new Exception in NetWork part.So if you request it.
You will get a android.os.NetworkOnMainThreadException!!

Android4.0在網路的部份多了一個新的Exception,
叫做android.os.NetworkOnMainThreadException
意思就是說:網路的活動跑在主要執行緒上了,很貼心的告訴你,這樣子你的APP可能會因為網路的活動等待回應太久,而被系統強制關閉(收到ANR)。

Solution:(put it in main )
StrictMode
    .setThreadPolicy(new StrictMode.ThreadPolicy.Builder()  
    .detectDiskReads()  
    .detectDiskWrites()  
    .detectNetwork()   // or .detectAll() for all detectable problems  
    .penaltyLog()  
    .build());  



2.Android 4.0 hope developer use post to request ,But if will occur
Java.io.FileNotFoundException

4.0中設置httpCon.setDoOutput(true),将導致請求以post方式提交
然後就會出現 java.io.FileNotFoundException

Soluation:
Delete-->httpCon.setDoOutput(true);

沒有留言:

張貼留言