I have been banging my head against the very same problem until I
remembered the permission need to be set in the manifest:

<uses-permission android:name="android.permission.INTERNET"/>

That should get you past the error you are having.


On Jun 15, 4:43 pm, Andy Wolpert <[email protected]> wrote:
> Hello All -
>
> I am trying to upload a file from the Android (Emulator or I/O phone)
> and get a cannot connect to server error. I saw a variety of posts,
> but never a solution. The same accounts, URLs and passwords work from
> the shell.
>
> The following code is "Hello Android" extended to call my FTP code.
> (accounts, passwords changed).
> Does FTP upload work?
> Is there a sample to work from?
> Any other ideas?
> (P.S. I would gladly use HTTP instead but found that more confusing
> and got a different error. So a sample there would be great as well).
> - Andy
>
> ---------- the code follows ---------
>
> package com.example.helloandroid;
>
> import java.io.FileInputStream;
> import java.io.OutputStream;
> import java.net.URL;
> import java.net.URLConnection;
> import android.app.Activity;
> import android.os.Bundle;
> import android.util.Log;
>
> public class HelloAndroid extends Activity {
>     /** Called when the activity is first created. */
>     @Override
>     public void onCreate(Bundle savedInstanceState) {
>         super.onCreate(savedInstanceState);
>         setContentView(R.layout.main);
>
>         try {
>         testFTP();
>         }
>
>         catch (Exception e) {
>                         Log.println(1, "FTP", e.getMessage());
>                         e.printStackTrace();
>                 }
>     }
>
>     protected void testFTP() throws Exception {
>                 final String localfile = 
> "/data/data/com.technomusicology.upload/
> test.mp3";
>                 final String targetfile = "test.mp3";
>                 final String host = "ftp.myhost_here.com";
>                 final String user = "user_here";
>                 final String password = "password_here";
>                 URL url = new URL("ftp://"+ user+ ":"+ password+ "@"+ host + 
> "/"+
> targetfile+ ";type=i");
>                 URLConnection urlc = url.openConnection();
>                 urlc.setDoOutput(true);
>                 // the following line throws "unable to connect to host {0}"
>                 OutputStream os = urlc.getOutputStream();
>                 FileInputStream is=  new FileInputStream(localfile);
>
>            byte[] buf= new byte[16384];
>            int c;
>            while (true) {
>                    c= is.read(buf);
>                    if (c<= 0)  break;
>                    os.write(buf, 0, c);
>            }
>            os.close();
>            is.close();
>            urlc = null; // close
>     }
>
> }
>
> // here is more information on the exception
> this    HelloAndroid  (id=830058643256)
> savedInstanceState      null
> e       IOException  (id=830058667280)
>         cause   IOException  (id=830058667280)
>         detailMessage   "Unable to connect to server: {0}" (id=830058746592)
>         stackState      int[]  (id=830058792560)
>         stackTrace      null

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to