Access local Web server.

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

Access local Web server.

Postby venkat » Fri Jan 04, 2008 11:54 am

Hi to All,

I am behind the proxy and proxy will authentication. Because of that :( , I am facing lots of problem for Downloading files from Web Server as well Uploading Files to Web Server. So, I decided to Install Web server in my Desktop itself, So that it's easy to test my Application. After installing Web server, I can open my Text File Using Following URL http://127.0.0.1/testData.txt Through Web Browser.

When I tried this URL to Download Text file from Android,I am getting “Connection refused” :?::!: Exception.
Can you Help me to Solve this problem , :( Please.... .
I have attached my Coding here. (I have deleted proxy settings from emulator. )

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import java.io.BufferedInputStream;
  2.  
  3. import java.io.InputStream;
  4.  
  5. import java.net.URL;
  6.  
  7. import java.net.URLConnection;
  8.  
  9. import org.apache.http.util.ByteArrayBuffer;
  10.  
  11. import android.app.Activity;
  12.  
  13. import android.os.Bundle;
  14.  
  15. import android.widget.TextView;
  16.  
  17.  
  18.  
  19. public class DownloadText extends Activity {
  20.  
  21.      @Override
  22.  
  23.      public void onCreate(Bundle icicle) {
  24.  
  25.           super.onCreate(icicle);
  26.  
  27.  
  28.  
  29.           /* We will show the data we read in a TextView. */
  30.  
  31.           TextView tv = new TextView(this);
  32.  
  33.          
  34.  
  35.           /* Will be filled and displayed later. */
  36.  
  37.           String myString = null;
  38.  
  39.           try {
  40.  
  41.                /* Define the URL we want to load data from. */
  42.  
  43.                URL myURL = new URL("http://127.0.0.1/testData.txt");
  44.  
  45.                /* Open a connection to that URL. */
  46.  
  47.                URLConnection ucon = myURL.openConnection();
  48.  
  49.                /* Define InputStreams to read
  50.  
  51.                 * from the URLConnection. */
  52.  
  53.                InputStream is = ucon.getInputStream();
  54.  
  55.                BufferedInputStream bis = new BufferedInputStream(is);
  56.  
  57.                
  58.  
  59.                /* Read bytes to the Buffer until
  60.  
  61.                 * there is nothing more to read(-1). */
  62.  
  63.                ByteArrayBuffer baf = new ByteArrayBuffer(50);
  64.  
  65.                int current = 0;
  66.  
  67.                while((current = bis.read()) != -1){
  68.  
  69.                     baf.append((byte)current);
  70.  
  71.                }
  72.  
  73.  
  74.  
  75.                /* Convert the Bytes read to a String. */
  76.  
  77.                myString = new String(baf.toByteArray());
  78.  
  79.           } catch (Exception e) {
  80.  
  81.                /* On any Error we want to display it. */
  82.  
  83.                myString = e.getMessage();
  84.  
  85.           }
  86.  
  87.           /* Show the String on the GUI. */
  88.  
  89.           tv.setText(myString);
  90.  
  91.           this.setContentView(tv);
  92.  
  93.      }
  94.  
  95. }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4




Regards, :roll:
Venkat. :)
venkat
Senior Developer
Senior Developer
 
Posts: 152
Joined: Tue Nov 27, 2007 5:42 am
Location: India

Top

Postby plusminus » Fri Jan 04, 2008 12:43 pm

Hello venkat,

note that "127.0.0.1" is the emulator itself :!:
The IP to use is probably sth like: 192.168.X.X or 10.10.X.X.

Type ipconfig to the Windows Cmdline or ifconfig to your LinuxShell on your Desktop-PC and search for such a IP.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby venkat » Sat Jan 05, 2008 5:21 am

Dear PlusMinus... :) :)
it works... Thank you very much :D :) :D :) :D :) :D :) :D :) :D :) :) :) :)

Regards,
Venkat.
venkat
Senior Developer
Senior Developer
 
Posts: 152
Joined: Tue Nov 27, 2007 5:42 am
Location: India

Postby plusminus » Sat Jan 05, 2008 12:53 pm

Hey venkat

No Problem, nice combination of smilies :lol:

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby mrdanger » Tue Jun 24, 2008 11:16 am

i try to use yout example, but i cannot to my local webserver. Have you any idea what my problem is?

Failure Mesage:
10.0.2.218/10.0.2.218:80 - Operation Failed


P.S.
The Problem is: i am in a network with 10.0.2.x and the emulator also in the ip range. How can i change the ip range of the emulator?

Unfortunately i cannot change the network in my company. Are there no other possibilities?
mrdanger
Junior Developer
Junior Developer
 
Posts: 10
Joined: Tue Jun 17, 2008 3:22 pm
Location: Hamburg, Germany

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: Google Feedfetcher and 7 guests