2015-10-27 82 views
-1

我使用這個代碼:FTP使用哪個IP?

ftpClient.connect(InetAddress.getByName(10.0.0.100)); 
ftpClient.login(user, password); 
ftpClient.changeWorkingDirectory(serverRoad); 
ftpClient.setFileType(FTP.BINARY_FILE_TYPE); 
BufferedInputStream buffIn=null; 
buffIn=new BufferedInputStream(new FileInputStream(file)); 
ftpClient.enterLocalPassiveMode(); 
ftpClient.storeFile("test.txt", buffIn); 
buffIn.close(); 
ftpClient.logout(); 
ftpClient.disconnect(); 

而且我的項目中出現死機此行

ftpClient.connect(InetAddress.getByName(server)); 

所以服務器的IP是是假的,我用從PC的IP服務器所在運行。 IP:10.0.0.100 這個IP有什麼問題?

順便說一下我使用Apache的百科全書

然後拋出我:

 D: [NET] android_getaddrinfofornet+,hn 16(0x6674703a2f2f31),sn(),hints(known),family 0,flags 4 
D: [NET] android_getaddrinfofornet-, err=8 
W: android.os.NetworkOnMainThreadException 
W:  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1155) 
W:  at java.net.InetAddress.lookupHostByName(InetAddress.java:418) 
W:  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252) 
W:  at java.net.InetAddress.getByName(InetAddress.java:305) 
W:  at com.example.mathias.newproject.MainActivity.connnectingwithFTP(MainActivity.java:89) 
W:  at com.example.mathias.newproject.MainActivity$2.onClick(MainActivity.java:52) 
W:  at android.view.View.performClick(View.java:4785) 
W:  at android.view.View$PerformClick.run(View.java:19858) 
W:  at android.os.Handler.handleCallback(Handler.java:739) 
W:  at android.os.Handler.dispatchMessage(Handler.java:95) 
W:  at android.os.Looper.loop(Looper.java:155) 
W:  at android.app.ActivityThread.main(ActivityThread.java:5696) 
W:  at java.lang.reflect.Method.invoke(Native Method) 
W:  at java.lang.reflect.Method.invoke(Method.java:372) 
W:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028) 
W:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823) 

清單:

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

    <uses-permission android:name="android.permission.INTERNET"/> 
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme" > 


     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" 
      android:theme="@style/AppTheme.NoActionBar" > 


      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 
+0

你有互聯網的權限在您的清單?請分享logcat – Santiago

+0

分享日誌以及您已經聲明變量'server'的片段 – darthvading

+0

添加了logcat和manifest @Santiago – Ribisl

回答

0

從應用程序中刪除uses-permission標籤並將其移動到manifest

check here how a manifest must be written

<manifest> 

    <uses-permission /> 
    <permission /> 
    <permission-tree /> 
    <permission-group /> 
    <instrumentation /> 
    <uses-sdk /> 
    <uses-configuration /> 
    <uses-feature /> 
    <supports-screens /> 
    <compatible-screens /> 
    <supports-gl-texture /> 

    <application> 

      <activity> 
      <intent-filter> 
       <action /> 
       <category /> 
       <data /> 
      </intent-filter> 
      <meta-data /> 
     </activity> 

     <activity-alias> 
      <intent-filter> . . . </intent-filter> 
      <meta-data /> 
     </activity-alias> 

     <service> 
      <intent-filter> . . . </intent-filter> 
      <meta-data/> 
     </service> 

     <receiver> 
      <intent-filter> . . . </intent-filter> 
      <meta-data /> 
     </receiver> 

     <provider> 
      <grant-uri-permission /> 
      <meta-data /> 
      <path-permission /> 
     </provider> 

     <uses-library /> 

    </application> 

</manifest> 
+0

工作但不完全,我有新的logcat故障 – Ribisl

+0

你有幾個「錯誤」的東西在代碼中,現在,新的錯誤是:'NetworkOnMainThreadException',因爲你在'Main''Thread'上做了'Network'相關的操作'On',因此拋出了'Exception' ......如果你google了這些條款,你會發現[AsyncTask](http://developer.android.com/reference/android/os/AsyncTask.html),然後繼續下一期。但首先谷歌他們,甚至使用堆棧溢出搜索...最後,如果一個答案幫助你解決這個問題,請點擊左邊的箭頭附近的'v'符號,以幫助其他人找到如何解決他們的問題 – Bonatti

+0

非常感謝你,現在它完美無缺! – Ribisl

0

你可能已經錯過了你的AndroidManifest.xml內此權限。

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
+0

同時使用:<使用權限android:name =「android.permission.INTERNET」/>'.......... ''.........'<使用權限android:name =「android.permission.ACCESS_WIFI_STATE」/>''' – Bonatti