I have Android Device Samsung GT OS 2.2.1
and i have succeeded Send HTTP Request to my Localhost (My Windows 8 PC) over WIFI
but, considering of speed, i also want to learn "How to send HTTP Request to localhost my windows 8 PC over USB"
this is my code to send over WIFI
URL url = null;
try {
/*Wireless LAN adapter Local Area Connection*/
url = new URL("http://192.168.xxx.xxx/MySkripsi/testWriteFile.php");
String body = "";
body += "text=" + messageTujuan;
byte[] bytes = body.getBytes();
HttpURLConnection conn = null;
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
OutputStream out = conn.getOutputStream();
out.write(bytes);
out.close();
int status = conn.getResponseCode();
if (status != 200) {
Toast.makeText(con, "status = " + status , 0).show();
}
else
{
InputStream is = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append((line + "\n"));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Toast.makeText(con, "Return Nya = " +sb.toString(), 0).show();
Log.v("TEST" , "Return Nya = " + sb.toString());
tv.setText(sb.toString());
is.close();
}
} catch (Exception e) {
Toast.makeText(this, e.toString() + "#" + e.getMessage(), 0).show();
}
this code, give me result
but whenever i turn off the WIFI it shows
"java.net.SocketException: Network Unreachable"
so i research it through some website, and i found this
http://www.codeproject.com/Articles/191930/Android-Usb-Port-Forwarding
here is the print screen
and USB Tunnel in my android device also say that "Service is running, Connected !"
but , with the same code as above, except :
url = new URL("h**p://127.0.0.1:80/MySkripsi/testWriteFile.php");
the result is "java.netConnectException :/127.0.0.1:80 - Connection refused"
i think there is something wrong with my firewall , but i dont know how to fix it
so my question is
is there any other method that can be used to communicate between android to server?
i really need my android device to Send Data to my localhost (Windows 8 pc) as my server and receive server's response over USB
thanks..
127.0.0.1
is always "this device", whichever device it's on. If you're trying to open it from your Android device, it's going to try to contact the Android device itself, not your Windows machine. You have to use your server's IP address. – chrylis -cautiouslyoptimistic-Network unreachable
means that your IP addresses aren't on the same subnet and you don't have a router available. Ifping
doesn't work, then your HTTP connections won't work either. – chrylis -cautiouslyoptimistic-