android


Android app doesn't work on Android 4


I created an Android project on 2.3.3 and tried it on mobile 2.3.3, everything works OK. It didn't work on mobile 4, so I re-built for Android 4, but I have the same problem.
This is the code:
public void FTP_Download(){
String server = "192.168.1.135";
int port = 21;
String user = "pc1";
String pass = "1551";
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
ftpClient.login(user, pass);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
Toast.makeText(getBaseContext(), "download starting.",Toast.LENGTH_LONG).show();
// APPROACH #1: using retrieveFile(String, OutputStream)
String remoteFile1 = "i.xml";
File downloadFile1 = new File("sdcard/i.xml");
OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadFile1));
boolean success = ftpClient.retrieveFile(remoteFile1, outputStream1);
outputStream1.close();
if (success) {
Toast.makeText(getBaseContext(), "File #1 has been downloaded successfully.",Toast.LENGTH_LONG).show();
}
} catch (IOException ex) {
System.out.println("Error: " + ex.getMessage());
ex.printStackTrace();
} finally {
try {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
Note:
I use commons-net-3.1 to connect.
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()).
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.
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.
public class MyAsyncTask extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... urls) {
String url = urls[0]
try {
//Connection request code, in your case ftp
} catch (Exception e) {
//Catch code
}
}
#Override
protected void onPostExecute(String result) {
//Code to de After the connection is done
}
}
Then, in the Activity I call the Asyn Task
String url = "http://...";
new MyAsyncTask().execute(url);
Edit
Here it's explained how to use Async Task, with an example
http://developer.android.com/reference/android/os/AsyncTask.html
I added this code, and all thing OK
StrictMode.ThreadPolicy policy = new StrictMode.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.

Related Links

Titanium view being pushed up when keyboard shows up
ViewPager should'n scroll
Getting http 404 error in android simulator for couch base lite during replication
Click on notification doesn't start a specific activity
Enable users to set their own logo in app
HTML5 sound problems on Android
Share a Post in native Android app directly through Graph API without any UI would block my FB app?
Algorithm to verify whether a point is circled
how to accept a value from an user in an android application developing eclipse?
Changing custom gridview bg color
Android NDK mixing C and C++ error undefined reference to mult(int, int)
Modify all buttons text size programatically?
How can I implement a list view with long items?
Cannot change button's layout_margin from styles?
recycling listview with checkbox
android widget or app for reliable repeated tasks

Categories

HOME
string
mjml
mysql-workbench
themes
solver
dojo
cakephp-3.4
emscripten
pyqt4
gembox-spreadsheet
nuget-package
gluon-mobile
angularfire2
webix
google-chrome-app
dkim
resultset
alamofire
xcodebuild
coin3d
linear-algebra
android-7.0-nougat
jersey-1.0
pyramid
cfml
zxing
resourcebundle
pylons
kudu
sendbird
vue-chartjs
hibernate-search
swagger-php
openerp-7
lint
sql-server-ce
password-protection
diacritics
google-data-api
news
visa
glm-math
lpsolve
android-checkbox
wc
stylecop
strftime
opencmis
textsum
automapper-5
amq
compiler-design
cd-burning
structuremap4
rmongo
polyline
visual-studio-2008-sp1
stax
pacemaker
mta
scaffolding
simevents
observablecollection
qiime
ellucian-scribe
aspen
mobiledoc-kit
relativelayout
directdraw
linklabel
doctype
zscript
rapidsvn
packet-sniffers
big-ip
iso-prolog
rickshaw
linkedin-jsapi
unix-socket
mobility
matrix-inverse
alternate
buffering
dsym
square-bracket
artemis
isqlquery
hidapi
route-provider
kaleidoscope
cascadingdropdown
wimax
frameworkelementfactory
subgraph
usertype
jsr286
relative
ikimagebrowserview
entitydatasource
rmdir
non-clustered-index
message-passing
addchild
request-headers
purepdf
alsb
int64

Resources

Encrypt Message