Problem using 10.0.2.2 for localhost

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

Problem using 10.0.2.2 for localhost

Postby Goomba » Fri Nov 14, 2008 5:00 pm

Hello guys,

I'm trying to connect my Android application with Tomcat web server running in the same computer.

This Activity is simple, only shows the HTML code from the index page of a server. It works with whatever URL on Internet, but I'm having problems getting the index page of my own Tomcat server (from my browser with "http://localhost:8080", there isn't any problem)

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class Localizador extends Activity {
  2.  
  3.     /** Called when the activity is first created. */
  4.  
  5.     @Override
  6.  
  7.     public void onCreate(Bundle savedInstanceState) {
  8.  
  9.                
  10.  
  11.         super.onCreate(savedInstanceState);
  12.  
  13.        
  14.  
  15.         EditText edit = new EditText(this);
  16.  
  17.  
  18.  
  19.         String server = "10.0.2.2";            
  20.  
  21.         int port = 8080;                  
  22.  
  23.  
  24.  
  25.         try {
  26.  
  27.                
  28.  
  29.           Socket socket = new Socket( server, port);
  30.  
  31.                
  32.  
  33.           BufferedReader in = new BufferedReader(
  34.  
  35.             new InputStreamReader( socket.getInputStream() ) );
  36.  
  37.          
  38.  
  39.           PrintWriter out = new PrintWriter(
  40.  
  41.             new OutputStreamWriter( socket.getOutputStream() ),true );
  42.  
  43.  
  44.  
  45.           out.println("GET /" );
  46.  
  47.          
  48.  
  49.           String line = null;
  50.  
  51.  
  52.  
  53.           while( (line = in.readLine()) != null ) {
  54.  
  55.                 edit.append(line);
  56.  
  57.  
  58.  
  59.           }
  60.  
  61.  
  62.  
  63.           socket.close();
  64.  
  65.  
  66.  
  67.           setContentView(edit);
  68.  
  69.          
  70.  
  71.         } catch( IOException e ) {
  72.  
  73.                 edit.setText("Error: " + e.getMessage());
  74.  
  75.             setContentView(edit);
  76.  
  77.         }    
  78.  
  79.  
  80.  
  81.     }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4



I have read here (http://code.google.com/android/kb/commo ... lhostalias) I need use the IP 10.0.2.2 referer to localhost, but it doesn't work !

I have no clue about the problem and I'm sure it's a nonsense. :cry:

Please, could somebody help me to connect my Android application with my Tomcat server, running on the same host?

Thanks a lot in advance
Goomba
Freshman
Freshman
 
Posts: 3
Joined: Fri Nov 14, 2008 4:32 pm
Location: Madrid, Spain

Top

Postby Goomba » Fri Nov 14, 2008 5:10 pm

Sorry, I forgot it ... when I run it, I only see an empty EditText box :?
Goomba
Freshman
Freshman
 
Posts: 3
Joined: Fri Nov 14, 2008 4:32 pm
Location: Madrid, Spain

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 10 guests