无法在android中连接到ftp但在java中连接

时间:2015-08-26 21:46:44

标签: java android ftp

我从这个blog post获得了完整的代码。我能够使用javac(windows java编译器),但在android studio中,它说无法连接到服务器。 Jar文件已经正确放置在libs文件夹中,编译文件(' libs / jar')函数已经在built.grade中添加了

所有,我做了额外的是创建了FTP对象,并在博客文章中提供了调用方法,并将此代码修复为public boolean ftpConnect(String host, String username, String password, int port)

"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; } }

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()
   }
 }

Gradle文件:

的build.gradle(项目:FTPConnection)

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'
}

built.gradle(模块:应用)

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

我的Android设备的日志太多,所以我将所选日志粘贴到我的代码中,显示连接错误。

日志

@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");
}

MainActivity

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

异常日志

execute(new String[]{"/api/player/55707e1eb7396de50dcef73d"})

0 个答案:

没有答案