Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi I am working on a project. I have a Android Client App and C# Based WebServer App. I use HttpUrlConnection to request data from server and use HttpListener to response to Android. I tried to request with a java desktop app it works fine.But then i tried on android it is not work. My prefix is "http : //127 . 0 . 0 . 1:8080/". I changed prefix as "http://www.google.com.tr" and it worked. These two applications are sharing the same Internet Network over wifi and i get "Connection Timeout Error". What is the problem?

Notes : I run app on Administrator Mode and I added url reservation with "netsh http add urlacl" command
Posted
Comments
[no name] 15-May-14 12:43pm    
Think about that on your local host maybe no web server is running.....
Regaloq 15-May-14 12:46pm    
Web server app is running when i send to request via web browser it works fine, web server app response... but not working with other device over network as android
[no name] 15-May-14 12:54pm    
Make it more clear, what/where.... Your question was related "local host" vs. "www.google...."....

127.0.0.1 is a loopback address for the local machine, you should be using the server machines lan ip address instead on your android device.

run cmd.exe on your server and type ipconfig to see what your server's ip address is.
 
Share this answer
 
Comments
Regaloq 15-May-14 13:51pm    
I try my server machine lan ip address but not working
Mehdi Gholam 15-May-14 13:55pm    
Use your server ip address from another computer on the lan and see if you can connect to it.
phil.o 15-May-14 14:25pm    
Did you try with [server lan address]:[port] (port seems to be 8080)?
Regaloq 15-May-14 15:21pm    
I am sharing codes:
Server side:

string[] prefixes = new string[] { "http://*:2999/" };
webserver = new WebServer(prefixes);

public class WebServer
{
private readonly HttpListener listener = new HttpListener();
internal HttpListenerContext context;
public WebServer(string[] prefixes)
{
if (!HttpListener.IsSupported)
throw new NotSupportedException(
"Needs Windows XP SP2, Server 2003 or later.");

if (prefixes == null || prefixes.Length == 0)
throw new ArgumentException("prefixes");


foreach (string s in prefixes)
listener.Prefixes.Add(s);

}

public void Run()
{
listener.Start();
context = listener.GetContext();
}
public byte[] GetDatas()
{
HttpListenerRequest request = context.Request;
byte[] gelenveriler = new byte[5000];
request.InputStream.Read(gelenveriler, 0, gelenveriler.Length);
request.InputStream.Close();
return gelenveriler;
}
public void SendDatas(byte[] veriler)
{
HttpListenerResponse response = context.Response;
response.ContentLength64 = veriler.Length;
response.OutputStream.Write(veriler, 0, veriler.Length);
response.OutputStream.Close();
}
public void Stop()
{
listener.Stop();
listener.Close();
}
}
}

Client:

String targetURL = "http://192.168.43.2:2999/";
HttpURLConnection connection = null;
try {
URL url;
//Create connection
url = new URL(targetURL );
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
Toast.makeText(getApplicationContext(), "Connection nesnesi ayarlandı.", Toast.LENGTH_LONG).show();





//Send request
DataOutputStream wr = new DataOutputStream (
connection.getOutputStream ());
File dosya = new File(xmlpath);
FileInputStream fis = new FileInputStream(dosya);
byte [] veriler = new byte[(int) dosya.length()];
fis.read(veriler);
Toast.makeText(getApplicationContext(), "Veriler Gönderiliyor", Toast.LENGTH_LONG).show();
wr.write(veriler);
wr.flush ();
fis.close();
wr.close ();

//Get Response
InputStream is = connection.getInputStream();
byte [] gelenler = new byte[10000*5000];
int size = is.read(gelenler);
byte[] sonuc = new byte[size];
for (int sayac = 0; sayac < size ; sayac++) {
sonuc[sayac] = gelenler[sayac];
}
File gelendosya = new File(Environment.getExternalStorageDirectory().getPath() + "/otoq/result.html");
FileOutputStream fos = new FileOutputStream(gelendosya);
fos.write(sonuc);
fos.close();
is.close();

} catch (Exception e) {

Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();

} finally {

if(connection != null) {
connection.disconnect();
}
}
Mehdi Gholam 15-May-14 15:30pm    
... and what is wrong?
Hi i solved the problem.
Click here for source
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900