无法连接到FTP中的Andr​​oid [英] Unable to connect to FTP in Android

查看:99
本文介绍了无法连接到FTP中的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经检查了其他的答案,他们没有帮我这个错误。也许我在做别的事情错了。

I've checked the other answers and they didn't help me with this error. Maybe I'm doing something else wrong.

下面是我的code:

    void uploadPic() throws FileNotFoundException
{
    FileInputStream fis = new FileInputStream(path);
    FTPClient con = new FTPClient();
    int bytesAvailable;
    try
    {
        con.connect("ftp://ftp.drivehq.com/");
        Toast.makeText(this, "Connected to FTP", Toast.LENGTH_SHORT).show();
        if (con.login("x", "x"))
        {
            Toast.makeText(this, "Logged in", Toast.LENGTH_SHORT).show();
            con.enterLocalPassiveMode(); // Active mode doesn't really work on Android
            bytesAvailable = fis.available();
            byte[] barray = new byte[bytesAvailable];
            fis.read(barray);
            ByteArrayInputStream in = new ByteArrayInputStream(barray);
            boolean result = con.storeFile("/CameraUpload.jpg", in);
            in.close();
            if (result) Log.v("Upload Result", "Succeeded");
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

我添加INTERNET权限到我的项目。
在logcat中显示了这些错误:

I've added INTERNET permission to my project. The logcat shows these errors:

android.os.NetworkOnMainThreadException
W/System.err(17531):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
W/System.err(17531):    at java.net.InetAddress.lookupHostByName(InetAddress.java:391

我连接到通过Wifi网络。

I'm connected to the internet via Wifi.

推荐答案

这似乎异常被抛出,当您试图从你的主线程执行网络操作(如FTP)。这是不允许出于性能原因(以便执行其可能需要一段时间的操作时,应用程序不出现锁定到用户)。假设你使用的是蜂窝或更高版本,你需要移动code,使连接到自己的子线程。

That exception seems to be thrown when you try to perform network operations (such as FTP) from your main thread. This is not allowed for performance reasons (so that the application doesn't appear to lock up to the user, when performing an action which may take a while). Assuming you are using Honeycomb or higher, you would need to move the code that makes the connection into its own child thread.

http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

http://developer.android.com/guide/practices/design/ responsiveness.html

这篇关于无法连接到FTP中的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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