I'm currently experiencing the exact same problem using basically the
exact same code. The code works in a project outside of Android but
not inside.
Does anyone know of any resolution? Does anyone have any sample code
of a successful FTP upload from inside Android that they wouldn't mind
sharing?
On Feb 22, 10:38 am, GeorgeS <[EMAIL PROTECTED]> wrote:
> Megha,
>
> Thanks for looking at this! Here's the full code area involved. An
> earlier call contacts the server and gets some XML back in response.
> The XML is written to the 'files' area and eventually processed. Theftparea
> is in this block for testing only. The code is by no means
> cleaned up...
>
> // code starts here
>
> /*
> * Handle the XML that we got back from the call to the server. If we
> get
> * this call then we should have XML otherwise we'll go into one of
> the
> * other routines to handle a failure to reach the server.
> */
> private void handleResults(String sXML) {
> try {
>
> // First we have to write the XML out...
> final String sFile = new String("results.xml");
>
> FileOutputStream fOut = openFileOutput(sFile,
> MODE_PRIVATE);
> OutputStreamWriter osWriter = new
> OutputStreamWriter(fOut);
> osWriter.write(sXML);
> osWriter.flush();
> osWriter.close();
>
> // Test test test test test - 2/14/2008 - GLS.
> URL ftpURL = new URL("ftp://userid:[EMAIL
> PROTECTED]/AndUpload/
> results.xml;type=i");
>
> URLConnection urlConn = ftpURL.openConnection();
>
> // These lines were added as suggested by
> different parties.
> urlConn.setDoOutput(true);
> urlConn.connect(); // <----erroroccurs here if
> this line is
> active.
>
> OutputStream osSend = urlConn.getOutputStream(); //
> <----error
> occurs here otherwise.
> FileInputStream fUpload = openFileInput(sFile);
>
> byte[] bUpBuff = new byte[16384];
> int c;
> while (true) {
> c = fUpload.read(bUpBuff);
> if (c <= 0)
> break;
> osSend.write(bUpBuff, 0, c);
> }
>
> osSend.close();
> fUpload.close();
> // End of test - 2/14/2008 - GLS.
>
> // Get an input stream for the XML file we just
> created.
> FileInputStream fIn = openFileInput(sFile);
>
> // Now read the XML file that we just wrote.
> DocumentBuilderFactory domFactory =
> DocumentBuilderFactory
> .newInstance();
> DocumentBuilder domParser =
> domFactory.newDocumentBuilder();
> Document domResults = domParser.parse(fIn);
> Element elResults = domResults.getDocumentElement();
> NodeList ndeResults =
> elResults.getElementsByTagName("Record");
>
> if (ndeResults != null) {
> NodeList ndeChild =
> ndeResults.item(0).getChildNodes();
> String sValue =
> ndeChild.item(0).getNodeValue();
>
> Log.d("XML Response", sValue);
>
> } else {
> ndeResults =
> elResults.getElementsByTagName("Error");
> if (ndeResults != null) {
> NodeList ndeChild =
> ndeResults.item(0).getChildNodes();
> String sValue =
> ndeChild.item(0).getNodeValue();
>
> Log.d("XMLError", sValue);
>
> }
> }
>
> // Close the file since we're done with it.
> fIn.close();
>
> } catch (Exception eException) {
> eException.printStackTrace();
> }
>
> }
>
> // end of code
>
> When I hit either of the lines indicated I get the exception 'Unable
> to connect to server {0}' though I can see on the server that theftp
> server thinks there is a connection. This same code works fine in a
> standalone (non-Android) program.
>
> Thanks!
> GeorgeS
>
> On Feb 21, 6:08 pm, "Megha Joshi" <[EMAIL PROTECTED]> wrote:
>
> > Hi,
>
> > Could you paste your code here to help us further in debugging.
>
> > Thanks,
> > Megha
>
> > On Thu, Feb 21, 2008 at 3:07 PM, GeorgeS <[EMAIL PROTECTED]> wrote:
>
> > > Thanks for the suggestion Megha.
>
> > > I added that statement in (I assumed that you meant
> > > urlConn.setDoOutput(true);) ahead of the OutputStream line and it made
> > > no difference. I can still see theftpserver say that someone is
> > > connected but as soon as I hit either a urlConn.connect(); or, in the
> > > older version, urlConn.getOutputStream(); I get the sameerroras
> > > before. My non-Android sample talking to the same server still works
> > > fine so I'm stumped.
>
> > > Thanks again,
> > > George
>
> > > On Feb 20, 2:50 pm, "Megha Joshi" <[EMAIL PROTECTED]> wrote:
> > > > Hi,
>
> > > > The URLConnection instance can be used either for sending a file or
> > > > receiving one.
> > > > So you need to call setDoOutput(true) or setDoInput(true) method on
> > > the
> > > > connection
> > > > instance before you start transferring files.
>
> > > > Try inserting
>
> > > > ftpURL.setDoOutput(true);
>
> > > > before OutputStream osSend = urlConn.getOutputStream();
>
> > > > and see if it works for you.
>
> > > > Thanks,
> > > > Megha
>
> > > > On Mon, Feb 18, 2008 at 1:44 PM, GeorgeS <[EMAIL PROTECTED]> wrote:
>
> > > > > I'm trying to do something pretty simple from my App... I just want to
> > > > >FTPa small file from the phone to a server. The code below works fine
> > > > > in a standalone Java project but fails running under Android (using
> > > > > the new M5 release).
>
> > > > > URL ftpURL = new URL("ftp://user:[EMAIL PROTECTED]/AndUpload/
> > > > > results.xml;type=i<
> > >ftp://user:[EMAIL PROTECTED]/AndUpload/results.xml;type=i>
> > > > > ");
> > > > > URLConnection urlConn = ftpURL.openConnection();
>
> > > > > OutputStream osSend = urlConn.getOutputStream();
>
> > > > > I can see the connection established on the server when I hit
> > > > > openConnection but when I get to getOutputStream I get an exception
> > > > > message 'Unable to connect to server {0}'. I'm likely missing
> > > > > something easy but just can't see it.
>
> > > > > Someone suggested elsewhere and I tried adding a call to .connect()
> > > > > after the .openConnection but all that does is to move theerrorup to
> > > > > the .connect() making me think that call is redundant to
> > > > > the .getOutputStream.
>
> > > > > Thanks!
> > > > > G.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
Announcing the new M5 SDK!
http://android-developers.blogspot.com/2008/02/android-sdk-m5-rc14-now-available.html
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---