0

我正在制作我的第一个 Android 应用程序,但我的代码产生了我无法理解的非常奇怪的行为。我用谷歌三天了,没有解决办法。

如果我在我的 AndroidManifest.xml 中写入除“8”以外的 minSdkVersion,我将无法连接到我的 FTP 服务器。另外,如果我使用 minSdkVersion "8" 和 targetSdkVersion 而不是 "8"。如果我不使用 minSdkVersion 和 targetSdkVersion 一切正常。简而言之:

  • 最小 8,目标 8 = ok
  • 至少 8,没有目标 = 好的
  • 没有最小值,没有目标 = 好的
  • 8 分钟,目标 17 = 失败
  • 分钟 10,目标 10 = 失败
  • 等等...

这是我的 AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.package"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8"
          android:targetSdkVersion="17" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />    
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.package.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="com.examplepackage.Settings"></activity>
    <activity android:name="com.schmidgroup.sgfax.Tools"></activity>
    <activity android:name="com.schmidgroup.sgfax.MyFTP"></activity>
    <activity android:name="com.schmidgroup.sgfax.Printing"></activity>
    <activity android:name="com.schmidgroup.sgfax.Listings"></activity>
    <activity android:name="com.schmidgroup.sgfax.Blue"></activity>
 </application>
</manifest>

我的 MainActivity.Java:

package com.example.package;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.content.Intent;
import android.view.MenuItem;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Button for Bluetooth-Connection
    Button verbinden = (Button) findViewById(R.id.button1);
    verbinden.setText(R.string.select_device);
    verbinden.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            Intent start = new Intent(getApplicationContext(), Blue.class);
            startActivity(start);
        }
    } );
}
}

我的 Blue.Java 类做了一些蓝牙工作,然后启动 Listings.Java:

package com.example.package;

import java.io.IOException;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

public class Listings extends Activity {

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listings);

    // Connect zum FTP-Server
    MyFTP conn = new MyFTP();
    conn.ftpConnect(host, user, pass, 21);  
            [... some more FTP stuff which don't work in cause of failing Ftp Connection...]    
}
}

最后是 MyFTP.Java:

package com.example.package;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.commons.net.ftp.*;
import android.content.Context;
import android.util.Log;

public class MyFTP {

private static void showServerReply(FTPClient FTPClient) {
    String[] replies = FTPClient.getReplyStrings();
    if (replies != null && replies.length > 0) {
        for (String aReply : replies) {
            System.out.println("SERVER: " + aReply);
        }
    }
}

//Now, declare a public FTP client object.
private static final String TAG = "MyFTPClient";
public FTPClient mFTPClient = null; 

//Method to connect to FTP server:
public boolean ftpConnect(String host, String username,
                          String password, int port)
{
    try {
        mFTPClient = new FTPClient();
        // connecting to the host
        mFTPClient.connect(host, port);
        showServerReply(mFTPClient);
        Log.i(TAG, "Successful conntected to " + host);
        // now check the reply code, if positive mean connection success
        if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
            // login using username & password
            boolean status = mFTPClient.login(username, password);
            Log.i(TAG, "Succesful logged in to " + host);
            /* Set File Transfer Mode
             *
             * To avoid corruption issue you must specified a correct
             * transfer mode, such as ASCII_FILE_TYPE, BINARY_FILE_TYPE,
             * EBCDIC_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(TAG, "Error: could not connect to host " + host );
    }

    return false;
} 
    [...and some more stuff...]

谁能看到我做错了什么?谢谢!

4

1 回答 1

0

You shouldn't make any network operations on your main UI thread. watch this question about StrictMode Policy in Android 3.0 and higher.

于 2013-09-10T08:49:55.603 回答