I got complete code from this blog post. I am able to get this working with javac(windows java compiler) but in android studio it says couldn't connect to the server. Jar files has been placed rightly in libs folder, compile files('libs/jar') function has been added in built.grade
All, I did extra is created FTP object and called methods provided on the blog post and fixed the this code in OnCreate()
:
public boolean ftpConnect(String host, String username,
String password, int port)
I have also added Internet permission my internet is also on. No errors are showing exception the exception. "Error: could not connect to host " + host
public class Ftp {
public FTPClient mFTPClient = null;
// Connect to the FTP server
public boolean ftpConnect(String host, String username, String password, int port){
try{
mFTPClient = new FTPClient();
// connecting to the host
Log.d("before","got entered");
mFTPClient.connect(host, port);
Log.d("after connect()", "didn't got connected");
// now check the reply code, if positive mean connection success
if(FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())){
//login using username & password
Log.d("Final_Message", "Didn't got positive reply for connection");
boolean status = mFTPClient.login(username, password);
/*Set File Transfer
To avoid corruption issue you must specified a correct transfer mode,
such as ASCII_FILE_TYPE, BINARY_FILE_TYPE, etc. Here, I use Binary file type
for transferring text, image, and compressed files.
*/
mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
mFTPClient.enterLocalPassiveMode();
return status;
}
}catch (Exception e){
Log.d("ERRO", "Error: could not connect to host" + host);
}
return false;
}
public boolean ftpUpload(/*String srcFilePath, String desFileName, String desDirectory*/){
boolean status = false;
try{
FileInputStream srcFileStream = new FileInputStream("abhimanyu.txt"/*srcFilePath*/);
status = mFTPClient.storeFile("abhimanyu", srcFileStream);
// change working directory to the destrination directory
/*if(ftpChangeDirectory(desDirectory)){
status = mFTPClient.storeFile(desFileName, srcFileStream);
}*/
}catch (Exception e){
Log.d("ERRO", "upload filed");
}
return status;
}
}
Gradle files:
build.gradle(Project:FTPConnection)
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
built.gradle(Module:app)
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "23.0.0 rc3"
defaultConfig {
applicationId "com.muchmore.www.ftpconnection"
minSdkVersion 10
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard- android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile files('libs/commons-net-3.1.jar')
compile 'com.android.support:appcompat-v7:22.1.1'
}
There are too many logs for my android device so I am pasting the selected logs for my code which shows error in connection.
LOGS
08-27 16:09:49.840 28535-28535/? D/before﹕ got entered
08-27 16:09:49.840 28535-28535/? D/libc-netbsd﹕ [getaddrinfo]: hostname=xxxxx; servname=(null); cache_mode=(null), netid=0; mark=0
08-27 16:09:49.840 28535-28535/? D/libc-netbsd﹕ [getaddrinfo]: ai_addrlen=0; ai_canonname=xxxxx; ai_flags=4; ai_family=0
08-27 16:09:49.840 28535-28535/? D/ERRO﹕ Error: could not connect to hostcp.mdurtk.in
08-27 16:09:49.840 28535-28535/? D/ERRO﹕ upload filed
08-27 16:09:49.840 28535-28535/? I/Activity﹕ Activity.onPostResume() called
08-27 16:09:49.850 28535-28556/? D/OpenGLRenderer﹕ Use EGL_SWAP_BEHAVIOR_PRESERVED: true
08-27 16:09:49.850 28535-28535/? D/Atlas﹕ Validating map...
08-27 16:09:49.860 1246-4970/? D/SplitWindow﹕ check instance of lgWin Window{1395dc05 u0 com.muchmore.www.ftpconnection/com.muchmore.www.ftpconnection.MainActivity}
08-27 16:09:49.860 1246-4970/? V/WindowManager﹕ Adding window Window{1395dc05 u0 com.muchmore.www.ftpconnection/com.muchmore.www.ftpconnection.MainActivity} at 4 of 14 (before Window{23ed7caa u0 Starting com.muchmore.www.ftpconnection})
08-27 16:09:49.880 28535-28556/? I/Adreno﹕ QUALCOMM build : 76ae100, Ie081474390
MainActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Ftp obj = new Ftp();
obj.ftpConnect("******", "*****", "*****", 21);
obj.ftpUpload();
TextView f = (TextView)findViewById(R.id.message);
f.setText("Uploaded to server");
}
Exception Logs
08-27 16:50:44.953 8963-8963/? D/ERRO﹕ Exception log starts here
08-27 16:50:44.953 8963-8963/? W/System.err﹕ android.os.NetworkOnMainThreadException
08-27 16:50:44.953 8963-8963/? W/System.err﹕ at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
08-27 16:50:44.953 8963-8963/? W/System.err﹕ at java.net.InetAddress.lookupHostByName(InetAddress.java:426)
08-27 16:50:44.953 8963-8963/? W/System.err﹕ at java.net.InetAddress.getAllByNameImpl(InetAddress.java:255)
08-27 16:50:44.953 8963-8963/? W/System.err﹕ at java.net.InetAddress.getByName(InetAddress.java:308)
08-27 16:50:44.953 8963-8963/? W/System.err﹕ at org.apache.commons.net.SocketClient.connect(SocketClient.java:192)
08-27 16:50:44.953 8963-8963/? W/System.err﹕ at com.muchmore.www.ftpconnection.Ftp.ftpConnect(Ftp.java:23)
08-27 16:50:44.953 8963-8963/? W/System.err﹕ at com.muchmore.www.ftpconnection.MainActivity.onCreate(MainActivity.java:17)
08-27 16:50:44.953 8963-8963/? W/System.err﹕ at android.app.Activity.performCreate(Activity.java:6093)
08-27 16:50:44.953 8963-8963/? W/System.err﹕ at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
08-27 16:50:44.953 8963-8963/? W/System.err﹕ at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
08-27 16:50:44.953 8963-8963/? W/System.err﹕ at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2434)
08-27 16:50:44.953 8963-8963/? W/System.err﹕ at android.app.ActivityThread.access$800(ActivityThread.java:162)
08-27 16:50:44.953 8963-8963/? W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)
08-27 16:50:44.953 8963-8963/? W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
08-27 16:50:44.953 8963-8963/? W/System.err﹕ at android.os.Looper.loop(Looper.java:135)
08-27 16:50:44.953 8963-8963/? W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5424)
08-27 16:50:44.953 8963-8963/? W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
08-27 16:50:44.953 8963-8963/? W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
08-27 16:50:44.953 8963-8963/? W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:913)
08-27 16:50:44.953 8963-8963/? W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:706)
08-27 16:50:44.953 8963-8963/? D/ERRO﹕ Error: could not connect to hostcp.mdurtk.in
08-27 16:50:44.953 8963-8963/? D/ERRO﹕ upload failed
08-27 16:50:44.953 8963-8963/? W/System.err﹕ java.io.FileNotFoundException: abhimanyu.txt: open failed: ENOENT (No such file or directory)
08-27 16:50:44.963 8963-8963/? W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:456)
08-27 16:50:44.963 8963-8963/? W/System.err﹕ at java.io.FileInputStream.<init>(FileInputStream.java:76)
08-27 16:50:44.963 8963-8963/? W/System.err﹕ at java.io.FileInputStream.<init>(FileInputStream.java:103)
08-27 16:50:44.963 8963-8963/? W/System.err﹕ at com.muchmore.www.ftpconnection.Ftp.ftpUpload(Ftp.java:177)
08-27 16:50:44.963 8963-8963/? W/System.err﹕ at com.muchmore.www.ftpconnection.MainActivity.onCreate(MainActivity.java:18)
08-27 16:50:44.963 8963-8963/? W/System.err﹕ at android.app.Activity.performCreate(Activity.java:6093)
08-27 16:50:44.963 8963-8963/? W/System.err﹕ at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
08-27 16:50:44.963 8963-8963/? W/System.err﹕ at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
08-27 16:50:44.963 8963-8963/? W/System.err﹕ at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2434)
08-27 16:50:44.963 8963-8963/? W/System.err﹕ at android.app.ActivityThread.access$800(ActivityThread.java:162)
08-27 16:50:44.963 8963-8963/? W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)
08-27 16:50:44.963 8963-8963/? W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
08-27 16:50:44.963 8963-8963/? W/System.err﹕ at android.os.Looper.loop(Looper.java:135)
08-27 16:50:44.963 8963-8963/? W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5424)
08-27 16:50:44.963 8963-8963/? W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
08-27 16:50:44.963 8963-8963/? W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
08-27 16:50:44.963 8963-8963/? W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:913)
08-27 16:50:44.963 8963-8963/? W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:706)
08-27 16:50:44.963 8963-8963/? W/System.err﹕ Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
08-27 16:50:44.963 8963-8963/? W/System.err﹕ at libcore.io.Posix.open(Native Method)
08-27 16:50:44.963 8963-8963/? W/System.err﹕ at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
08-27 16:50:44.963 8963-8963/? W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:442)
08-27 16:50:44.963 8963-8963/? W/System.err﹕ ... 17 more