Yogie Soesanto
Yogie Soesanto

Reputation: 172

Communicate Android Device to Localhost (My Windows 8 PC) over / via USB Cable

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

https://i.sstatic.net/hY8SI.jpg

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..

Upvotes: 0

Views: 2101

Answers (2)

Yogie Soesanto
Yogie Soesanto

Reputation: 172

thanks @Ram and @chyrlis

i want to share it, maybe it will help the others

so i just googled everywhere , and they all tell me about usb tether

here is what i did to solve my own problem

  1. in my pc , i type cmd > ipconfig

and there isn't any ethernet for my android device

  1. i TURN ON the "USB TETHERING" (I'm Using Android Froyo GT-S5830) (Note that , when i googled, they all told me that not every device has "USB TETHERING")

  2. then i type cmd > ipconfig again

and THERE IS one ethernet for my android devie , with ip = 192.168.42.201 (Note that this IP is dynamic [DHCP], so maybe you can change it to static first) here is the tutorial http://www.youtube.com/watch?v=SIYyRYdV7B8

so i changed my IP to 192.168.42.202

  1. using my own code

i changed the url to

"url = new URL("http://192.168.42.202/MySkripsi/testWriteFile.php");" 

NOTE THAT 192.168.42.201 is the IP i got from ipconfig and i changed my IP to 192.168.42.202 so it will be static IP

  1. and BAM there it is , i can use my http request from my device and get the response from my server

hope it helps

Upvotes: 1

Ram
Ram

Reputation: 2530

Well, I developed one application which connect my jsp file.here the sample code.

              try
                 {

                    URL url = new URL("http:/xx.xxx.xxx.x:80/sample.jsp");
                    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                     InputStream in = new BufferedInputStream(urlConnection.getInputStream());
                     BufferedReader r = new BufferedReader(new InputStreamReader(in));
                     String x = "";
                     String total = "";
                     int i=0;
                     ArrayList<String> content = new ArrayList();
                     while((x = r.readLine()) != null)
                     {
                                 content.add(x);

                     }
                     in.close();
                     r.close();
                 }
                 catch(Exception e)
                 {
                     e.printStackTrace();
                     Toast.makeText(Customer.this, e.toString(), Toast.LENGTH_SHORT).show();
                 }

So,you can make change(in url) this code as your wish.If you want to run in usb means, you have to download samsung usb driver to your pc and then you run the application.Before that you enable developer option in your device.

Upvotes: 0

Related Questions