Send XML file to FTP/HTTPS server - android

I create small tracking app which get coordinates write them to xml file and then send this file with that data to ftp server. I am using function "SendFileGPS" to send that file but apk all the time crashes and it doesn't work. This function is using additional library common net 1.4 and it is quite strange because all the time when I start Android Studio I must download this library from web by Android Studio. And I got some additional questions.
I was thinking that maybe not only this function is a problem, how should look this server I mean how proper this server to get file from this app?
There is a another way to send this file without using additional libs ?
How it should look if I want send this file to another type of server (HTTPS)?
public void SendFileGPS()
{
FTPClient mFTP = new FTPClient();
try {
// Connect to FTP Server
mFTP.connect("here I put adress IP");
mFTP.login("here I put login", "here I put pass");
mFTP.setFileType(FTP.BINARY_FILE_TYPE);
mFTP.enterLocalPassiveMode();
// Prepare file to be uploaded to FTP Server
File file = new File("/path/to/MyXmlFileName.xml");
FileInputStream ifile = new FileInputStream(file);
// Upload file to FTP Server
mFTP.storeFile("filetotranfer",ifile);
mFTP.disconnect();
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

Related

Passing an image from webview to application (IOS and Android)

I'm building hybrid applications that rely on 2-way communication between javascript in a webview and the hosting application.
Attitudes differ somewhat as in IOS the JS can send a message to swift (using WKWebView), that listens through
userContentController(userContentController: WKUserContentController,
didReceiveScriptMessage message: WKScriptMessage)
when implementing the WKScriptMessageHandler protocol,
whereas in Android the JS can actually call an Android method that has #JavascriptInterface annotation, after calling addJavascriptInterface().
Both approaches are OK for me, as I'm passing around data using JSON strings. Question is, what if I need to pass a media file, say an image or video, from the web page to the application? should I just pass a bitmap inside the json? Seems a little naive... recommendations?
edit: when passing an image from the application to the webpage I save the file to the file system and send the filename to the webview. Can it be done the other way around? Can javascript save to the hosting mobile device file system?
You have to host(in case of webapp) or store(in case of mobile app) the image and pass the image url, not exactly the image.
Almost all api that uses images bitmap also takes image url.
regards
Ashish
To answer your second question which is there are comments, use the following code.
Here the html content is your binary content:
FileWriter imageFileWriter = null;
BufferedWriter imageBufferedWriter = null;
ABOUtil.createDir(InMemoryDataStructure.FILE_PATH.getFileDirForimage());
File imageFileDir = new File(InMemoryDataStructure.FILE_PATH.getFileDirForimage());
String imageName = "/finalimage"+ filename + jpg
File mimageFile = new File(imageFileDir, imageName);
try {
imageFileWriter = new FileWriter(mimageFile, false);
imageBufferedWriter = new BufferedWriter(imageFileWriter);
StringBuilder sb = new StringBuilder();
sb.append(htmlContent);
sb.append(scriptInjectJavascript(lstimageNameValue));
imageBufferedWriter.write(sb.toString());
imageBufferedWriter.close();
return mimageFile;
}
catch (IOException e) {
MAFLogger.e("", "", e);
}
finally{
if(imageFileWriter!=null)
try {
imageFileWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
MAFLogger.e("","",e);
}
if(imageBufferedWriter!=null)
try {
imageBufferedWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
MAFLogger.e("","",e);
}
}

How to push file from pc to android device programmatically using ftp server/client

I'm new to android programming and ftp. Currently on a project to create android application to transfer file from PC to android device and verse vice. I'm able to transfer file from android device to PC using ftp server/client method. However, I'm facing difficulties on how to push file from PC to android device. Is it possible to push file from ftp server(PC) to client using ftp server/client? Or any alternatives to do so? Is there any tutorial/example which I could go through?
Any help would be greatly appreciated. Thanks a lot!
Transfer from android device to pc (android client side):
FTPClient mFTP = new FTPClient();
try {
// Connect to FTP Server
mFTP.connect("192.168.0.103");
mFTP.login("user", "password");
mFTP.setFileType(FTP.BINARY_FILE_TYPE);
mFTP.enterLocalPassiveMode();
File file = new File(f);
BufferedInputStream buffIn=null;
buffIn=new BufferedInputStream(new FileInputStream(file));
mFTP.enterLocalPassiveMode();
mFTP.storeFile(filename, buffIn);
buffIn.close();
mFTP.logout();
mFTP.disconnect();
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

upload captured image to FTP using ftpClient in android

i want to upload captured image from an android phone to Ftp Server i have written following code i m facing error at this line ftp.store(remote,input) after this line
i m getting replycode=550 file unavialable i m using apache commons-net 3.3 library i have tried
same with WebClient in .net it works fine but in java its gives me dis error(550) i have searched many sites but no help i even called hosting service they said no problem from there side i stucked from last 1 day plz correct me what i am missing here......
my code :
private void UploadImage(){
FTPClient ftp = new FTPClient();
try {
ftp.connect("hostname",21);// Using port no=21
int reply = ftp.getReplyCode(); //here i m getting 220
ftp.login("abc", "abc");
reply = ftp.getReplyCode(); //here i m getting 230
ftp.setFileType(FTP.BINARY_FILE_TYPE);
reply = ftp.getReplyCode();
ftp.enterLocalPassiveMode();
reply = ftp.getReplyCode();
File sfile = new File(PhotoPath); //here i m getting /mnt/sdcard/DCIM/CameraSample/abc769708880.jpg
String filename = sfile.getName();
FileInputStream fis = new FileInputStream(sfile);
boolean aRtn=ftp.storeFile("ftpPath"+sfile.getName(), fis);// return true if successful
reply = ftp.getReplyCode(); // here im getting 550
if(aRtn == true)
{
Toast toast= Toast.makeText(getApplicationContext(),
"Successfull", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER, 0, 0);
toast.show();
}
fis.close();
ftp.disconnect();
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
That exception seems to be thrown when you try to perform network operations (such as FTP) from your main thread. This is not allowed for performance reasons (so that the application doesn't appear to lock up to the user, when performing an action which may take a while). Assuming you are using Honeycomb or higher, you would need to move the code that makes the connection into its own child thread.
http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html
http://developer.android.com/guide/practices/design/responsiveness.html

Ftp upload file in perticular path by android

f.connect("ftp.drivehq.com",21);
if(f.login("XXXX", "XXX"))
{
f.enterLocalPassiveMode();
f.setFileType(FTP.BINARY_FILE_TYPE);
FileInputStream in = new FileInputStream(URL);
result = f.storeFile(newfile, in);
in.close();
f.logout();
f.disconnect();
}
It upload fine but not in particular path i also try as ftp://ftp.drivehq.com/myfolder
but it gives error in android unknown host exception like I need to upload file in particular folder So thanks in advance help me
Hi Please use the following code. it worked for me i have checked it.
SimpleFTP ftp = new SimpleFTP();
try
{
// Connect to an FTP server on port 21.
ftp.connect("host", 21, "username", "password");
// Set binary mode.
ftp.bin();
// Change to a new working directory on the FTP server.
ftp.cwd("/httpdocs/yourdestinationfolderinftp");
// Upload some files.
ftp.stor(new File("/mnt/sdcard/ftp.jpg"));
// Quit from the FTP server.
ftp.disconnect();
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}

Using EWS to send mail from exchange server

I followed this link to generate the jar file. I added this to my project. Then have a simple code to send a mail:
public void mailSend() {
ExchangeService service = new ExchangeService();
ExchangeCredentials credentials = new WebCredentials(
"email", "$*pass!");
service.setCredentials(credentials);
String host = "host";
try {
service.setUrl(new java.net.URI("https://" + host
+ "/EWS/Exchange.asmx"));
service.setTraceEnabled(true);
EmailMessage msg = new EmailMessage(service);
msg.setSubject("Hello world!");
msg.setBody(MessageBody
.getMessageBodyFromText("Sent using the EWS Managed API."));
msg.getToRecipients().add("email");
msg.send();
Log.i("Msg","SEND ");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
The problem is that it shows the jar file has no source attachment. Hence I get the error no definition found for class ExchangeService. This should be part of the jar file generated. While exporting as jar file, I had made sure the src folder is clicked.

Categories

Resources