I searched a lot about this, but no answers were found.
I want to upload a file (with the URL, destination and file name). I have a class that extends the AsyncTast class. It works well with a Wi-Fi connection, but it does not work with mobile data (G, 3G, H)! I can’t understand why, I’m going crazy.
Has anyone had the same or similar problem? Thanks!
I post my code below. thank you
public class AsyncDownloader extends AsyncTask<String, Integer, String> { @Override protected String doInBackground(String... sUrl) { try { Log.v("Downloader", "Source: " + sUrl[0]); Log.v("Downloader", "Destin: " +sUrl[1]+"/" + sUrl[2]); URL url = new URL(sUrl[0]); URLConnection connection = url.openConnection(); connection.connect();
From core business ..
AsyncDownloader downloader = new AsyncDownloader(); downloader.execute("http://....", "...destination...", "...filename...");
In the target directory SOMETIMES I found the file, but it was not fully loaded or it is 0 kB ..
In AndroidManifest.xml, I have:
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE"/> <uses-permission android:name="android.permission.CALL_PHONE" />
I use this function to test the connection (seems to work well):
public boolean isOnline() { ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnectedOrConnecting(); }
LogCat (very long - full): http://pastebin.com/EL4DREwB LogCat (short, significant - application launch time): http://pastebin.com/wPYDQH3P