0

I used "HttpClient" and "URLConnection" to get json dat from server, and it's working fine when app start. The problem is, when I leave my phone for about more than 20 minutes there without any operation, then I start the app, it give me "UnknownHostException" at logcat, and no validate data response. This time, the wifi icon at notification bar shows good quality of wireless connection.

So I closed the app, and restart it for many times, then it's ok.

I have googled a lot to know how to solve this one, but failed. I am really confused about this. I got this one - http://www.net.princeton.edu/android/android-stops-renewing-lease-keeps-using-IP-address-11236.html, I am not sure it triggered by this bug or not.

I tried many methods from the internet - like "turn off and on wifi at codes" all not working. I just wondering if this one is a bug on certain phone or android system, or something wrong with my code, any advices are appreciated.

2012.12.10 Updated

I tried to call it again after 2 seconds each, and total 5 times to wait for the phone wake up from sleep, it's working fine on my phone.

// all these codes are running on a worker thread (not main thread)
int timeout = 5;

if (json == null) {
  do {
     Thread.sleep (2000);
     // call it agina...
     if (json != null) {
        break;
     }
  } while (--timeout >= 0);

}

if (json == null) {
    // do something if it's not working at last
}
Tom
  • 4,612
  • 4
  • 32
  • 43
  • I've had this happen alot, I think it takes the wifi a few seconds to wake upon going to sleep. This would happen no matter what app you use. You could try using a wifi lock if it's really that important. Otherwise just catch the exception and try it again – Jameo Dec 05 '12 at 01:10
  • Thanks, I think you are right. Maybe just retry it after some time.. – Tom Dec 05 '12 at 01:21
  • After tried many times, I found you are right, and I just try wait 2 seconds each to call it again total 5 times, and it works fine on my phone. – Tom Dec 09 '12 at 18:26

1 Answers1

0

Best Way to solve this problem is to use WakeLock. It makes your application to stay on the device so that you cant get into these kind of problems use this android manifest file:

<uses-permission android:name="android.permission.WAKE_LOCK"/>

G M Ramesh
  • 3,420
  • 9
  • 37
  • 53
  • Thank you. It's not working on my phone though, I did it with a simple way just call it after 2 seconds each, and 5 times total to wait the phone wake back, and it's working fine on my phone now.. – Tom Dec 09 '12 at 18:28