Android App Doesn't Work On Android 4
Solution 1:
In android version 2.3 above you can not start internet connection from main UI thread. Instead you should use AsyncTask. I assumed you are not using AsyncTask. If you are, then post the code and log cat also. Some examples of other operations that ICS and HoneyComb won't allow you to perform on the UI thread are:( from link posted in comment below ) -
- Opening a Socket connection (i.e. new Socket()).
- HTTP requests (i.e. HTTPClient and HTTPUrlConnection).
- Attempting to connect to a remote MySQL database.
- Downloading a file (i.e. Downloader.downloadFile()).
Solution 2:
You should not use the main UI Thread to start a network connection or read/write data from it as @phazorRise explained it. But I strongly disagree with using an AsyncTask to perform your download. AsyncTask have been designed for short living operations and downloading a file doesn't belong to that category.
The most relevant way to achieve your goal, if your files are big (and I assume it depends on users, so we can say they are big) is to use a service to download the files.
I invite you to have a look at RoboSpice, it will give your app robustness for networking and it's really the most interesting library for network requests on Android.
Here is an inforgraphics to get familiarized with alternatives and understand why using a service is better than any other technology.
Solution 3:
When I use "internet conections" programming for andoid 4, I do an Async Task as follows:
You can put this Class code into the same file as principal file to intercatue with global variables or functions.
publicclassMyAsyncTaskextendsAsyncTask<String, Void, String> {
@OverrideprotectedStringdoInBackground(String... urls) {
String url = urls[0]
try {
//Connection request code, in your case ftp
} catch (Exception e) {
//Catch code
}
}
@OverrideprotectedvoidonPostExecute(String result) {
//Code to de After the connection is done
}
}
Then, in the Activity I call the Asyn Task
Stringurl="http://...";
newMyAsyncTask().execute(url);
Edit Here it's explained how to use Async Task, with an example
http://developer.android.com/reference/android/os/AsyncTask.html
Solution 4:
I added this code, and all thing OK
StrictMode.ThreadPolicypolicy=newStrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Note: I taked the code from @user1169115 comment in another post. This isn't the best soluation, but I don't know why asynctask isn't work, so I don't have another choice.
Post a Comment for "Android App Doesn't Work On Android 4"