Skip to content
Advertisement

FTP connection java

I’m trying to upload the file to a server. What is the way for uploading a file to a server through FTP?

i wrote this class:

serverconnect.java:

JavaScript

and this is the mainActivity ( only the relevant code):

JavaScript

and when i install the app on my phone i get an error: “unfortanly your app must stopp…”

the new code:

JavaScript

the new logcat:

JavaScript

Advertisement

Answer

Note: AsyncTask class was deprecated in API level 30. Please use java.util.concurrent instead.

The problem is the fact that you’re trying to make a network call on your main thread. Which is not allowed on Android 3.0 or higher.

You should solve this by calling the FTP server on a different thread. A good way to do this is to use AsyncTask:

JavaScript

Then you can run this background thread using the following code in the main thread:

JavaScript

EDIT:

If you want to pass parameters between the different methods, you can change the AsyncTask<Void, Void, Void> superclass initialization.

For example AsyncTask<String, Double, Integer> will make it possible to pass a String variable to the doInBackground method, keep track of progress using a double and use a integer as the result type(the result type is the return type of doInBackground, which will be sent to onPostExecute as a parameter).

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement