• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps how to download and upload xml files from ftp server using android?

Hi Everybody,
I'm rajkumar this is my new thread in this forum, i'm new to android i'm facing problem connecting to ftp server for downloading and uploading xml files i used following code(see attachment) it will work fine in java but in android it is giving exception(505 error) unable to connected to ftp server i included all permissions in manifest files please help me.

Thank You,

**Java class**

//on button click

File objfile=new File(getFilesDir().getAbsolutePath()+ File.separator + "/index.xml");
download("ftp.qualityinaction.net/QIA/Questions/Airlines/","qualityinaction.net", "password","index.xml",objfile);
public void download( String strftpServer, String struser, String strpassword,
String strfileName, File strdestination ) throws MalformedURLException,
IOException
{
if (strftpServer != null && strfileName != null && strdestination != null)
{
StringBuffer strb = new StringBuffer( "ftp://" );
// check for authentication else assume its anonymous access.
if (struser != null && strpassword != null)
{
strb.append( struser );
strb.append( ':' );
strb.append( strpassword );
strb.append( '@' );
}
strb.append( strftpServer );
strb.append( '/' );
strb.append( strfileName );
/*
* type ==> a=ASCII mode, i=image (binary) mode, d= file directory
* listing
*/
strb.append( ";type=i" );
//test(strb.toString());
//OpenHttpConnection(strb.toString());

BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try
{
URL url = new URL(strb.toString());
URLConnection urlc =(URLConnection) url.openConnection();
//urlc.setDoOutput(true);
//urlc.connect();

//urlc.connect();

bis = new BufferedInputStream( urlc.getInputStream() );
bos = new BufferedOutputStream( new FileOutputStream(
strdestination.getName() ) );

int i;
while ((i = bis.read()) != -1)
{
bos.write( i );
}
}
finally
{
if (bis != null)
try
{
bis.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
if (bos != null)
try
{
bos.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
else
{
System.out.println( "Input not available" );
}
}

//File uploading to FTP
public void upload( String ftpServer, String user, String password,
String fileName, File source ) throws MalformedURLException,
IOException
{
if (ftpServer != null && fileName != null && source != null)
{
StringBuffer sb = new StringBuffer( "http://" );
// check for authentication else assume its anonymous access.
if (user != null && password != null)
{
sb.append( user );
sb.append( ':' );
sb.append( password );
sb.append( '@' );
}
sb.append( ftpServer );
sb.append( '/' );
sb.append( fileName );
/*
* type ==> a=ASCII mode, i=image (binary) mode, d= file directory
* listing
*/
sb.append( ";type=i" );

BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try
{
URL url = new URL( sb.toString() );
URLConnection urlc = url.openConnection();

bos = new BufferedOutputStream( urlc.getOutputStream() );
bis = new BufferedInputStream( new FileInputStream( source ) );

int i;
// read byte by byte until end of stream
while ((i = bis.read()) != -1)
{
bos.write( i );
}
}
finally
{
if (bis != null)
try
{
bis.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
if (bos != null)
try
{
bos.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
else
{
System.out.println( "Input not available." );
}
}
​
 

Attachments

Welcome to AF! you may have more luck with your question in our Application Development forum.

I dont know much about FTP, but 505 means the server doesnt support the version of protocol you are using I think.

[FONT=Arial,Helvetica,Geneva,Swiss,SunSans-Regular]505 HTTP Version Not Supported

The server does not support, or refuses to support, the HTTP protocol version that was used in the request message. The server is indicating that it is unable or unwilling to complete the request using the same major version as the client other than with this error message. The response SHOULD contain an entity describing why that version is not supported and what other protocols are supported by that server. [/FONT]
from: Server Error Codes

It seems like the server doesnt support FTP over an HTTP string as per this site:

nsftools - Java FTP Tips
This method can work if:

  • the site supports it
  • you know exactly what file you need and where it is
  • you don't have to do anything other than just download a file
505 makes me think that the server doesnt support the method you are using.
 
Welcome to AF! you may have more luck with your question in our Application Development forum.

I dont know much about FTP, but 505 means the server doesnt support the version of protocol you are using I think.

from: Server Error Codes

It seems like the server doesnt support FTP over an HTTP string as per this site:

nsftools - Java FTP Tips
505 makes me think that the server doesnt support the method you are using.


Hi, i started using following code using this i able to connect ftp while downloading a file it is giving you unable to access server path i included org.apache.commons.net.ftp.FTPClient jar files for that please help me.


** java code **

FTPClient client = new FTPClient();
FileOutputStream fos = null;

try {
//client.connect("ftp://ftp.qualityinaction.net/QIA/Questions/Airlines/");
client.connect("ftp.qualityinaction.net");
client.login("qualityinaction.net","password");

//
// The remote filename to be downloaded.
//
// String filename = "/QIA/Questions/Airlines/index.xml";
String filename = getFilesDir().getAbsolutePath()+ File.separator + "/index.xml";
// String filename = "/QIA/Questions/Airlines/index.xml";
File file = new File(filename);
fos = new FileOutputStream(file);

//
// Download file from FTP server
//
//client.retrieveFile("/" + filename, fos);
client.retrieveFile("/QIA/Questions/Airlines/index.xml", fos);
// client.retrieveFile( getFilesDir().getAbsolutePath()+ File.separator + "/index.xml", fos);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fos != null) {
fos.close();
}
client.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
 

Attachments

Have you tried asking in the development forum that alostpacket linked before (Application Development - Android Forums)? I think that what you're asking is a little beyond what most normal users are capable of...


Yes i tried all four methods given in development forum, using those methods first of all i'm not able to connect ftp so i looked further to find a solution finally i got above code it will work for connecting ftp but i unable to access particular file so i posted above question can you please check
 
try running from a phone

and post any error messages from LogCat <- important!!!!

Also make sure you dont try/catch all the errors without printing them to LogCat
 
Back
Top Bottom