Bonjour,
je doit livrer un projet android pour se connecter à un NAS via ftp, j'utilise apache common net comme librairie et lorsque je lance l'appli logcat me dit
java.net.ConnectException: localhost/127.0.0.1:21 - connection refused
j'ai bien mis mon serveur en 127.0.0.1:21 et voici mon code du "main"
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107 package com.freedev.freeco; import java.io.IOException; import java.net.InetAddress; import java.net.SocketException; import java.net.UnknownHostException; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPReply; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity { Button valider = null; EditText login = null; EditText pass = null; TextView loginField = null; TextView passField = null; final String EXTRAS_LOGIN = "user_login"; final String EXTRAS_PASS = "passowrd_login"; FTPClient ftp = null; //byte[] adresse = {(byte) 172, 16, 0, 2}; byte[] adresse = {(byte) 127, 0, 0, 1}; String ip = "127.0.0.1"; byte[] binaryIp = null; InetAddress adress = null; /*@InjectView (R.id.valider) Button valider; @InjectView (R.id.loginField) TextView loginField; @InjectView (R.id.passField) TextView passField; @InjectView (R.id.login) EditText login; @InjectView (R.id.pass) EditText pass;*/ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); valider = (Button) findViewById(R.id.valider); loginField = (TextView) findViewById(R.id.loginField); passField = (TextView) findViewById(R.id.passField); login = (EditText) findViewById(R.id.login); pass = (EditText) findViewById(R.id.pass); ftp = new FTPClient(); try{ adress = InetAddress.getByName(ip); } catch (UnknownHostException e) { e.printStackTrace(); } binaryIp = adress.getAddress(); valider.setOnClickListener(clickTableBoutons); } private OnClickListener clickTableBoutons = new View.OnClickListener() { @Override public void onClick(View v) { Intent activity = null; try { ftp.connect(InetAddress.getByAddress(binaryIp), 21); ftp.login(login.toString(), pass.toString()); if(!FTPReply.isPositiveCompletion(ftp.getReplyCode())) { ftp.disconnect(); System.err.println("FTP server refused connection."); System.exit(1); } activity = new Intent(MainActivity.this, StandardActivity.class); activity.putExtra(EXTRAS_LOGIN, login.getText().toString()); activity.putExtra(EXTRAS_PASS, pass.getText().toString()); startActivity(activity); ftp.logout(); } catch (SocketException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ if(ftp.isConnected()){ try { ftp.disconnect(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }; }
Partager