Authenticator and alternate ports

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

Authenticator and alternate ports

Postby timhoeck » Sat Jan 17, 2009 10:08 am

Hello,

I am new to Android programming, and fairly new to java (I wrote a single Tivo application). I am essentially porting some code from that app over to Android.

All I am trying to do is connect to a basic authentication server app on an alternate port which returns a file. The code I am using to connect to the url works fine as a standalone java app.

When I run this code on Android, it seems to hang connecting to my authenticated url. When debugging, it just appears to be looping through the Authenticator over and over again. It does not timeout or throw an exception.

If I change the url to a non-authenticated site, or even other basic authentication sites, it connects fine. I have even connected to a separate server app that uses an alternate port, and the page is returned. The only difference in the authenticated url that works and the one that doesn't is an alternate port with authentication, so I don't know if that is what is causing the issue.

As mentioned, this code works in a standalone java app, and all the server app does is return a file once authenticated. I have even taken the content returned and put it on another site, with basic authentication (on port 80), and it works fine on Android.

Is there something I need to do differently when using authentication on an alternate port with Android?


Here's the code:

Code: Select all
package com.android.test;
import java.io.*;
import java.net.*;


public class Connect extends Authenticator {
   public final String settingHostName = "myhostname";
   public final int settingPort = 81;
   public final String settingUserName = "admin";
   public final String settingPassword = "password;
   
   public PasswordAuthentication getPasswordAuthentication () {
       return new PasswordAuthentication (this.settingUserName, this.settingPassword.toCharArray());
    }
   
   public Connect() {
      Authenticator.setDefault(this);
   }
   
    public String Download() {
         String site = "http://" + this.settingHostName + ":" + this.settingPort + "/gui/?list=1";
         URL url;
         String data = null;
          
         // Load URL Data into File
          try {
            url = new URL(site);
            
              System.out.println("Connecting to: " + site + "n");
              
              HttpURLConnection con = (HttpURLConnection) url.openConnection();
               con.setInstanceFollowRedirects(true);
               con.setConnectTimeout(5000);
              
               InputStream in = null;
               try {
                   in = con.getInputStream();
               System.out.println("Success.");
               } catch(IOException e) {
                  System.out.println("Error: " + e.toString() + "n");
               }
              
           } catch (MalformedURLException e1) {
              System.out.println("Error in URLn");
         } catch (IOException e) {
              System.out.println("Problem reading urln");
         }
           return data;
          
       }
}
timhoeck
Junior Developer
Junior Developer
 
Posts: 22
Joined: Fri Jan 16, 2009 4:41 pm

Top

Postby timhoeck » Tue Jan 27, 2009 7:31 am

This is still driving me nuts. Any ideas what could be going on? My code hangs on in = con.getInputStream();

This code works as a standalone app, and works with other urls on android. My particular url is using basic authentication with an alternate port, that is the only difference I can see.

Is there some code someone can point me to for processing this same (authenticated, alt port) request in a more detailed method, to figure out what is going on?
timhoeck
Junior Developer
Junior Developer
 
Posts: 22
Joined: Fri Jan 16, 2009 4:41 pm

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 4 guests