i'm newbie on android,
I'm trying to upload file from sdcard to ftp server using android, the code seems to be correct. While debbuging it says that my connet and upload functions to the server can not access the classes given by org.appache.commons.net (no source found in those classes), knowing that i have this org.appache.commons.net jar in the libs foldre, the project build path and the user libraries.
Should i use another librarie or, is there any other work to do ?
The code
public boolean ftpConnect(String host, String username,String password, int port){
try {
mFTPClient = new FTPClient();
mFTPClient.connect(host, port);
mFTPClient.enterLocalPassiveMode();
if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
// login using username & password
boolean status = mFTPClient.login(username, password);
Log.w("into the ftpUpload","!!");
mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
mFTPClient.enterLocalPassiveMode();
Log.w("into the ftpUpload","!!");
return status;
}
} catch(Exception e) {
Log.d(TAG, "Error: could not connect to host " + host );
}
return false;}
call this methode in another class using
FtpClient FTP = new FtpClient();
FTP.ftpConnect("..*.", "", "*", 21);
I'm doing it juste like several posts propose it, concerning the lib i added the org.apache.commons.net jar to the lib file and to the java build path..
The exception is given exactly here: mFTPClient.connect(host, port); (btw there are no logs).. Does anybody have an idea what can be the problem with that? I tried lot of codes that works for others but it fail to connect to the ftp server every time. Thanks for your responses.
Related
Hope you are doing well
I have a new project and I need to link it to firebase , but unfortenetlly firebase is not supported in my country
So , I need to find a way to include a proxy to my app , I have a NordVPN account and I hope that I can link it to my project.
I have tried so many codes ;
public MainActivity(String[] MainActivity) {
System.getProperties().put("https.proxyHost", "*****");
System.getProperties().put("https.proxyPort", "******");
System.getProperties().put("https.proxyUser", "******");
System.getProperties().put("https.proxyPassword", "******");
String host = "nl.socks.nordhold.net";
String port = "80";
System.out.println("Using proxy: " + host + ":" + port);
System.setProperty("http.proxyHost", host);
System.setProperty("http.proxyPort", port);
System.setProperty("http.nonProxyHosts", "localhost|127.0.0.1");
Nothing works , please advice
Thank you
we have an app under xamarin android build with visual studio 2017.
this app works since three years without any problems.
since two weeks and I don't know why actually some device can't sync with our back end.
It's really strange because nothing has change in this part .
this error does not appear on all devices but on one or two from time to time
we use the dll httpClient for to sync the datas with our backend.
If i put a break point inside the postAsync I have an exception with this -> Cannot access a disposed object. Object name: 'System.Net.Sockets.NetworkStream
Any one has an idea about how to solve this ? also what does it meam ?
Here is it the code of the postAsync method :
thanks for our time and comment guys
public override HttpResult ExecutePost(Uri target, string body)
{
var client = new HttpClient();
client.MaxResponseContentBufferSize = MaxHttpResponseBufferSize;
try
{
var requestContent = new StringContent(body, RequestContentEncoding, RequestContentType);
var response = client.PostAsync(target, requestContent).Result;
if (response.IsSuccessStatusCode)
{
var content = response.Content.ReadAsStringAsync().Result;
return new HttpResult(content, null, null);
}
return new HttpResult(null, "Response is empty", response.StatusCode.ToString());
}
catch (Exception e)
{
return new HttpResult(null, "Problem with the HttpPost", e.Message);
}
}
I experienced the same issue. Have been battling for 6 hours on this issue.
If you read the error, I was getting (Failed to connect to localhost/127.0.0.1:7113). If you put localhost in your browser or swagger tool it will work but if you put https://127.0.0.1:7113/api/weatherforecast in your browser it will not work. It will give you a certificate problem.
So I think you have to resolve 127.0.0.1 to localhost with https certificate on your local dev machine.
I'm building a MAUI app with Visual Studio 2022 Preview.
So I solved this issue by deploying my API to AZURE.
Then update to the azure url for example:
string apiUrl = "https://weatherforecast.azurewebsites.net/api/weatherforecast";
and then it worked brilliantly. Like super brilliantly.
Here is my code:
public void LoginAsync()
{
HttpClient client = new HttpClient();
string apiUrl = "https://weatherforecast.azurewebsites.net/api/weatherforecast";
UserCredentials.EmailAddress = LoginUIEntity.EmailAddress;
UserCredentials.Password = LoginUIEntity.Password;
string serialized = JsonConvert.SerializeObject(UserCredentials);
var inputMessage = new HttpRequestMessage
{
Content = new StringContent(serialized, Encoding.UTF8, "application/json")
};
inputMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
try
{
var message = client.PostAsync(apiUrl, inputMessage.Content).Result;
if (message.IsSuccessStatusCode)
{
var apiResponse = message.Content.ReadAsStringAsync();
UserCredentials = JsonConvert.DeserializeObject<UserCredentials>(apiResponse.Result);
if (UserCredentials.IsValid)
{
UserCredentials.IsLoggedIn = true;
}
else
{
ErrorMessage = "Invalid credentials supplied.";
}
}
}
catch (Exception ex)
{
ErrorMessage = "An error has occurred. Please contact support if the error persists.";
}
}
}
thanks for the link your provide.
I've try up the buffer on the postasync / try to sync in wifi OR 3G / delete special character in json / ...
but nothing work
we have move the prod database to the test and try to sync the data to the test database with postman. with postman the result was ENTITY TOO LARGE !
Json is size > 1.2 mega and the default value inside IIS is set to 1 mega
Here is it the problem ...
thanks problem solve
I am trying to enable TOR support on my own XMPP app in android. I am using orbot as TOR proxy and I have the following code to connect app socket:
socket = new Socket(new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("127.0.0.1", 9050)));
socket.connect(addr, Config.SOCKET_TIMEOUT * 1000);
but I am getting Malformed reply from SOCKS server even that my Orbot is up and running. I believe that this error is thrown when app cant access proxy server or mentioned server is not SOCKS proxy.
I have also tried to use jsocks:
Socks5Proxy sProxy = new Socks5Proxy("127.0.0.1", 9050);
sProxy.resolveAddrLocally(false);
String host = account.getServer().toString();
int port = 5222;
System.out.println(host + ":" + port);
try {
socket = new SocksSocket(sProxy, host, port);
}catch(SocksException sock_ex){
System.err.println("SocksException:"+sock_ex);
}
System.out.println("here we are");
where host is "jabbim.com"
But I never get to "here we are" println so it looks like my app hangs somewhere on creating SocksSocket but I am not getting any errors either. When I debug it it hangs on impl.getInputStream somewhere in jsocks (PlainSocketImpl I believe)
Any idea how to fix this?
Thanks in forward
I solved this using
compile "org.igniterealtime.smack:smack-android-extensions:4.1.4"
compile "org.igniterealtime.smack:smack-tcp:4.1.4"
and then connecting using this code:
final Random rndForTorCircuits = new Random();
String user = rndForTorCircuits.nextInt(100000) + "";
String pass = rndForTorCircuits.nextInt(100000) + "";
ProxyInfo proxyInfo = new ProxyInfo(ProxyInfo.ProxyType.SOCKS5, "127.0.0.1", 9050, user, pass);
socket = proxyInfo.getSocketFactory().createSocket(addr.getHostName(), addr.getPort());
by using this you get socket that is already connected to proxy.
I'm using this library to connect to my FTP-server, which works well. And if I want to upload a file to my FTP, I'm using this code below, which also work great.
FileInputStream srcFileStream = new FileInputStream(mFile);
// change working directory to the destination directory
if (ftpChangeDirectory(desDirectory)) {
status = mFTPClient.storeFile(mFile.getName(), srcFileStream);
} else {
ftpMakeDirectory(desDirectory);
if (ftpChangeDirectory(desDirectory))
status = mFTPClient.storeFile(mFile.getName(), srcFileStream);
}
srcFileStream.close();
There is one problem thought. If I want to upload multiple files to my FTP, and I put it in a loop, it only uploads the first one, and then stops, using this code:
for(ImageItem mItem : mItems) {
Log.d("test", "start" + mItems.indexOf(mItem));
FileInputStream srcFileStream = new FileInputStream(mItem.getImageFile());
// change working directory to the destination directory
if (ftpChangeDirectory(desDirectory)) {
status = mFTPClient.storeFile(mItem.getImageFile().getName(), srcFileStream);
} else {
ftpMakeDirectory(desDirectory);
if(ftpChangeDirectory(desDirectory))
status = mFTPClient.storeFile(mItem.getImageFile().getName(), srcFileStream);
}
srcFileStream.close();
}
Anyone knows how it shall be done?
All help is greatly appreciated!
Thanks in advance,
Måns
This ftp library work asynchronously so you can't do it in this way. It sends only the first one because the state the others will find the stream busy. You should use AsyncTask and isert your "for" in the function of doInBackground(String... args)
Running following code on emulator. Connecting to filezilla server on windows development host using ip address 10.0.2.2.
FTPClient ftp = new FTPClient();
try
{
ftp.connect("10.0.2.2", 21);
ftp.login(username, password);
String REMOTE_DEFAULT_INITIAL_DIRECTORY = ftp.currentDirectory();
File mDirectory = new File(REMOTE_DEFAULT_INITIAL_DIRECTORY);
File[] listing = mDirectory.listFiles();
}
catch (Exception e)
{
e.printStackTrace();
}
Problem: The File[] contains listing of files on android client instead of server.
Thanks
Imdad
I use the same library for my Android project and to get the listing I use the following:
FTPClient ftpClient = new FTPClient();
ftpClient.connect(serverName);
ftpClient.login(username, password);
ftpClient.setPassive(true);
ftpClient.setType(FTPClient.TYPE_BINARY);
final FTPFile[] remoteFiles = ftpClient.list();
Android Emulator dosen't support FTP connection...
Because, Emulator is fixed port to 5554 ~ 5555?
FTP prot is 21..
So, you just run on real device.