我有一个"无法连接到主机" logcat的消息,同时试图连接到FTP服务器,我究竟做错了什么? [英] I have a "could not connect to host" Logcat message while trying to connect to FTP server, what am I doing wrong?

查看:174
本文介绍了我有一个"无法连接到主机" logcat的消息,同时试图连接到FTP服务器,我究竟做错了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发在Android上,连接到FTP服务器上传和下载文件的应用程序。为了使连接我使用基于和Apache的百科全书网库的FTPClient类我工作的Eclipse。

但我得到我的logcat以下消息:

  07-04 21:11:44.196:D / USB虚拟(14708):错误:无法连接到主机ftp://xxx.xxx

以下是我的清单权限:

 <采用-SDK安卓的minSdkVersion =7/>
<使用许可权的android:NAME =android.permission.WRITE_EXTERNAL_STORAG​​E/>
<使用许可权的android:NAME =android.permission.INTERNET对/>

我真的失去了,为什么我不能连接到我的FTP服务器,在控制台有是没有错误显示。我缺少的东西吗?我要感谢任何帮助。

我加入我使用连接到我的FTP服务器code:

 公共布尔ftpConnect(字符串主机,字符串username,字符串密码,
        INT口){
    尝试{
        mFTPClient =新FTPClient();
                    //主机ftp://looner-project.zxq.net
        mFTPClient.connect(主机);
        mFTPClient.connect(InetAddress.getByName(主机));
        如果(FT preply.isPositiveCompletion(mFTPClient.getReply code())){            布尔状态= mFTPClient.login(用户名,密码);
            mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
            mFTPClient.enterLocalPassiveMode();            返回状态;
        }
    }赶上(例外五){
        Log.d(TAG,错误:无法连接到主机+主机);
    }    返回false;
}


解决方案

检查事项...

1 检查您的网络连接。

2 您提供的URL ftp站点正确的?

3 如果它的密码保护,你是正确的给他们?

4。使用命令提示符检查。给下面的命令来检查一下。

  FTP www.your_ftp_site.com

你会被提示输入用户名和密码,上面的命令后。

我还附上我的code这在我以前的音频文件上传到服务器,以明确...

 进口的java.io.File;    进口java.io.FileInputStream中;    进口java.io.IOException异常;    进口org.apache.commons.net.ftp.FTP;    进口org.apache.commons.net.ftp.FTPClient;    进口android.util.Log;    公共类MyOwn {        公共无效goforIt(){
            FTPClient CON = NULL;            尝试
            {
                CON =新FTPClient();
                con.connect(www.mysamle.net); //它的虚拟地址                如果(con.login(uju495,史蒂夫·乔布斯!!))
                {
                    con.enterLocalPassiveMode(); // 很重要                    con.setFileType(FTP.BINARY_FILE_TYPE); // 很重要
                    字符串数据=/sdcard/Vivekm4a.m4a;                    在的FileInputStream =新的FileInputStream(新文件(数据));
                    布尔结果= con.storeFile(/广告/ Vivekm4a.m4a中);
                    附寄();
                    如果(结果)Log.v(上传结果,成功);
                    con.logout();
                    con.disconnect();
                }
            }
            赶上(例外五)
            {
                e.printStackTrace();
            }
        }    }

I'm developing an application on Android that connects to a FTP server to upload and download files. To make the connection I'm using the apache commons-net library's FTPClient class based on this and I'm working on Eclipse.

But I get the following message on my Logcat:

07-04 21:11:44.196: D/USB Virtual(14708): Error: could not connect to host ftp://xxx.xxx

The following is my manifest permissions:

<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>

I'm really lost as to why I can't connect to my FTP server, in the console there're no errors showing. Am I missing something? I would thank any help.

I'm adding the code I'm using to connect to my FTP server:

public boolean ftpConnect(String host, String username, String password,
        int port) {
    try {
        mFTPClient = new FTPClient();
                    //host is ftp://looner-project.zxq.net
        mFTPClient.connect(host);
        mFTPClient.connect(InetAddress.getByName(host));


        if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {

            boolean status = mFTPClient.login(username, password);
            mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
            mFTPClient.enterLocalPassiveMode();

            return status;
        }
    } catch (Exception e) {
        Log.d(TAG, "Error: could not connect to host " + host);
    }

    return false;
}

解决方案

Things to check...

1. Check your internet connection.

2. Are you giving the Url to the ftp site correct ??

3. If its password protected, are you giving them correctly ??

4. Using command prompt check it. Give the below command to check it.

ftp www.your_ftp_site.com

after the above command you will be prompted for username and password.

I am also attaching my code which i used to upload audio file to the server to make it clear...

    import java.io.File;

    import java.io.FileInputStream;

    import java.io.IOException;

    import org.apache.commons.net.ftp.FTP;

    import org.apache.commons.net.ftp.FTPClient;

    import android.util.Log;



    public class MyOwn {

        public void goforIt(){


            FTPClient con = null;

            try
            {
                con = new FTPClient();
                con.connect("www.mysamle.net");                 // Its dummy Address

                if (con.login("uju495", "Stevejobs!!"))
                {
                    con.enterLocalPassiveMode();                   // Very Important

                    con.setFileType(FTP.BINARY_FILE_TYPE);        //  Very Important
                    String data = "/sdcard/Vivekm4a.m4a";

                    FileInputStream in = new FileInputStream(new File(data));
                    boolean result = con.storeFile("/Ads/Vivekm4a.m4a", in);
                    in.close();
                    if (result) Log.v("upload result", "succeeded");
                    con.logout();
                    con.disconnect();
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }   
        }

    }

这篇关于我有一个&QUOT;无法连接到主机&QUOT; logcat的消息,同时试图连接到FTP服务器,我究竟做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆