我正在尝试在android studio中构建一个FTP应用程序,但我似乎无法让它工作。经过一些调试,我能够避免我的代码中的一些错误,但现在当我尝试连接到我在Ubuntu中制作的本地ftp服务器时,我收到以下消息“错误:无法连接到主机192.168.1.2”(我的ftp服务器从终端从笔记本电脑连接时工作正常)。该应用程序编译和安装很好。以下是MainActivity.java的代码
public boolean bindService(Intent service, ServiceConnection conn, int flags) {
return false;
}
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
connect = (Button) findViewById(R.id.connect_button);
connect.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (isNetworkAvailable()) {
new FTPConnect().execute();
} else {
AlertDialog.Builder b = new AlertDialog.Builder(MainActivity.this);
b.setMessage("Internet connectivity failure.Try again!");
b.show();
}
}
});
}
class FTPConnect extends AsyncTask<Void, Void, Void> {
ProgressDialog pDialog;
@Override
protected void onPreExecute() {
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Connecting...");
pDialog.show();
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
runOnUiThread(new Runnable() {
@Override
public void run() {
ftpClient = new FTPClient();
try {
ftpClient.connect(FTPSERVER, PORT);
ftpClient.login(USERNAME, PASSWORD);
ftpClient.enterLocalPassiveMode();
System.out.println("status :: " + ftpClient.getStatus());
setContentView(R.layout.connected);
textView = (TextView) findViewById(R.id.textView);
FTPFile[] files = ftpClient.listFiles();
DateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
for (FTPFile file : files) {
String details = file.getName();
if (file.isDirectory()) {
details = "[" + details + "]";
}
details += "\t\t" + file.getSize();
details += "\t\t" + dateFormater.format(file.getTimestamp().getTime());
System.out.println(details);
}
} catch (Exception e) {
Log.d(TAG, "Error: could not connect to host " + FTPSERVER);
}
}
});
return null;
}
@Override
protected void onPostExecute(Void result) {
if (null != pDialog && pDialog.isShowing()) {
pDialog.dismiss();
}
super.onPostExecute(result);
}
}
// Check Internet Connection
public boolean isNetworkAvailable() {
ConnectivityManager connectivity = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null) {
return false;
} else {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (NetworkInfo anInfo : info) {
if (anInfo.getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}
logcat的:
07-03 23:43:34.901 16781-16781 / com.example.vagil.ftpclient_1 D / dalvikvm:延迟启用CheckJNI 07-03 23:43:35.051 16781-16781 / com.example.vagil.ftpclient_1 D / ActivityThread:setTargetHeapUtilization:0.25 07-03 23:43:35.051 16781-16781 / com.example.vagil.ftpclient_1 D / ActivityThread:setTargetHeapIdealFree:8388608 07-03 23:43:35.051 16781-16781 / com.example.vagil.ftpclient_1 D / ActivityThread:setTargetHeapConcurrentStart:2097152 07-03 23:43:35.391 16781-16781 / com.example.vagil.ftpclient_1 I / dalvikvm:找不到方法android.view.ViewGroup.onRtlPropertiesChanged,从方法android.support.v7.widget.Toolbar.onRtlPropertiesChanged引用 07-03 23:43:35.391 16781-16781 / com.example.vagil.ftpclient_1 W / dalvikvm:VFY:无法解析虚方法13338:Landroid / view / ViewGroup; .onRtlPropertiesChanged(I)V 07-03 23:43:35.391 16781-16781 / com.example.vagil.ftpclient_1 D / dalvikvm:VFY:将操作码0x6f替换为0x0007 07-03 23:43:35.401 16781-16781 / com.example.vagil.ftpclient_1 I / dalvikvm:找不到方法android.content.res.TypedArray.getChangingConfigurations,从方法android.support.v7.internal.widget中引用。 TintTypedArray.getChangingConfigurations 07-03 23:43:35.401 16781-16781 / com.example.vagil.ftpclient_1 W / dalvikvm:VFY:无法解析虚方法413:Landroid / content / res / TypedArray; .getChangingConfigurations()I 07-03 23:43:35.401 16781-16781 / com.example.vagil.ftpclient_1 D / dalvikvm:VFY:在0x0002处替换操作码0x6e 07-03 23:43:35.401 16781-16781 / com.example.vagil.ftpclient_1 I / dalvikvm:找不到方法android.content.res.TypedArray.getType,从方法android.support.v7.internal.widget引用。 TintTypedArray.getType 07-03 23:43:35.401 16781-16781 / com.example.vagil.ftpclient_1 W / dalvikvm:VFY:无法解析虚方法435:Landroid / content / res / TypedArray; .getType(I)I 07-03 23:43:35.401 16781-16781 / com.example.vagil.ftpclient_1 D / dalvikvm:VFY:在0x0002处替换操作码0x6e 07-03 23:43:35.511 16781-16781 / com.example.vagil.ftpclient_1 D / libEGL:loaded /system/lib/egl/libEGL_adreno200.so 07-03 23:43:35.511 16781-16781 / com.example.vagil.ftpclient_1 D / libEGL:loaded /system/lib/egl/libGLESv1_CM_adreno200.so 07-03 23:43:35.521 16781-16781 / com.example.vagil.ftpclient_1 D / libEGL:loaded /system/lib/egl/libGLESv2_adreno200.so 07-03 23:43:35.521 16781-16781 / com.example.vagil.ftpclient_1 I / Adreno200-EGL :: EGL 1.4 QUALCOMM build:AU_LINUX_ANDROID_JB_REL_2.0.3.04.01.02.21.107_msm8625_JB_REL_2.0.3_CL3357771_release_AU(CL3357771) 建造日期:星期一02/25/13 当地分行: 远程分支:quic / jb_rel_2.0.3 本地补丁:无 重建分支:AU_LINUX_ANDROID_JB_REL_2.0.3.04.01.02.21.107 + NOTHING 07-03 23:43:35.551 16781-16781 / com.example.vagil.ftpclient_1 D / OpenGLRenderer:启用调试模式0 07-03 23:43:54.211 16781-16781 / com.example.vagil.ftpclient_1 I / AppCompatDelegate:Activity的LayoutInflater已经安装了Factory,因此我们无法安装AppCompat的 07-03 23:45:45.431 16781-16781 / com.example.vagil.ftpclient_1 D / MainActivity:错误:无法连接到主机192.168.1.2
答案 0 :(得分:0)
问题解决了。我正在使用runOnUiThread(new Runnable()来运行与ftp服务器的连接。一旦我删除它,我就可以连接。