10

Currently I support a client with the following architecture, used in an industrial manufacturing process:

  1. They have a Windows program running on a PC that controls the industrial machinery.

  2. They have a proprietary app (which I maintain for them) running on an Android device (basically a phone) which interfaces wirelessly with the PC software over a TCP socket, so it can remotely control those industrial processes.

What the client wants now is a web server running on the PC and a web browser built into the app to control some additional processes not controlled from his Windows program.

I've set up a WAMP server on the PC and a sample web page, which I can see on any browser on the PC as "localhost". I know how to add a web browser View to the Android app via a WebView class.

But I don't know how to make the browser on the phone see the WAMP server on the PC via the TCP connection. How do I hook those two things up?

user316117
  • 7,971
  • 20
  • 83
  • 158
  • 1
    the server should have an address, they should be on the same network, and the server should be accessible from that network – njzk2 Jun 02 '15 at 17:47

5 Answers5

6

Some basic information that you should be aware of

When that PC connects to your phone, an underlying network interface must be used, for instance, WiFi or Ethernet. Also note that localhost is lied on loopback interface. It should be noted that loopback interface is only accessible in a device itself (i.e. Other devices cannot communicate with loopback of another device).

In the other side, once an interface is connected, it would be assigned an IP address. I assume that your phone is connected to that PC via WiFi interface, So in this case two interfaces are in use.

  • wlan interface of that PC
  • wlan interface of your phone.

enter image description here

And both have their unique IP addresses. If you want to connect from your phone to that PC you should know IP address of wlan interface of that PC.

If your PC is Linux-based, you could write ifconfig and see that IP address in inet addr field (Under wlan0 section). For Windows machines read this page.


In Android WebView

This View provides a method called loadUrl that is used to fetch HTML content from remote machines. The string you should pass to this method is formated as follows:

http://IP_ADDRESS:PORT_NUMBER

Where

  • IP_ADDRESS : IP address of remote machine. (In your case that one you've found in the previous step)
  • PORT_NUMBER : Each machine can listen on different ports for different purposes (e.g. HTTP, FTP, SSH, ...). Default port for HTTP is 80.

Therefore if we assume that the IP address of that PC is 192.168.0.1, then you should have:

webView.loadUrl("http://192.168.0.1:80");

Or

webView.loadUrl("http://192.168.0.1");
// Because 80 is the default port number for HTTP
Community
  • 1
  • 1
frogatto
  • 28,539
  • 11
  • 83
  • 129
2

Well, how do you connect a webbrowser to a server? On a desktop webbrowser you type the host name or the ip address into the addresss bar of the webbrowser.

A similar processs works for an embedded webbrowser, you just have to call the method loadUrl of your WebView instance.

The more important question what network name your web-server has and prepend http://. If the server gets a static IP address you can use that, too. However you have to make sure the WAMP is not only listening on localhost, otherwise it can not be access from any device (but that is not a question for Stackoverflow).

Robert
  • 39,162
  • 17
  • 99
  • 152
2

What you want (not just for the WAMP server but for XAMPP, LAMP) is a static IP Address that you can connect to at any point of time.

Another thing you mentioned was sockets. Sockets provide very basic transaction of data and you need to take care of everything, HTTP is an application layer protocol that is an abstraction over Transport layer. Web browsers (popularly) use HTTP. Here is the difference.

For Socket programming here are two links : link1 and link2.

Using HTTP requires sending a GET or a POST response to the server, received probably by php or django that take it from there. As far as the code goes. Here you go.

For GET Request

public String sendGetRequest() {
    HttpClient client = new DefaultHttpClient();
    URI website;
    try {
        website = new URI(url);
        HttpGet request = new HttpGet();
        request.setURI(website);

        HttpResponse response = client.execute(request);
        return response;
    } catch (URISyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

For POST Request

public String sendPostRequest(ArrayList<NameValuePair> nameValuePairs) {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url);

    try {
        // Add your data
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
        if (response != null)
            return response;
        else
        {
            Log.e("Request", "response is null");
            return null;
        }

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
    return null;
}

Parse the response to check how the server responded. The function of the app should be to ease the user not provide him with a solution that already exists. I'd suggest you against using webview(just display the relevant information organized properly).

EDIT : Here is a good explanation for HTTP vs TCP.

Community
  • 1
  • 1
Mayank
  • 1,364
  • 1
  • 15
  • 29
1

Essentially, for the Windows machine, you need to know the IP address. This is the address on the network that other devices will use to contact it. If you have an open TCP/IP socket already, then you already know this address.

HTTP is a TCP-based protocol. It works just like your HTTP socket, listening by default for connections on port 80. From the documentation, it looks as though the default port for Wamp is port 80.

From your connection activity containing the webview (from here):

private WebView webview;

public void onCreate(Bundle savedInstanceState) {
        [initialize stuff as needed ...]

        this.webview = (WebView)findViewById(R.id.webview);
        WebSettings settings = this.webview.getSettings();
        settings.setJavaScriptEnabled(true);
        this.webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);

        webview.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Log.i(TAG, "Processing webview url click...");
                view.loadUrl(url);
                return true;
            }

            public void onPageFinished(WebView view, String url) {
                Log.i(TAG, "Finished loading URL: " +url);
                if (progressBar.isShowing()) {
                    progressBar.dismiss();
                }
            }

            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Log.e(TAG, "Error: " + description);
                Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
                alertDialog.setTitle("Error");
                alertDialog.setMessage(description);
                alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        return;
                    }
                });
                alertDialog.show();
            }
        });
}

Then, whenever you want to load or reload the URL into the web view:

this.webview.loadUrl("http://ipaddress/");

Where ipaddress is the IP you use to connect using your TCP socket. If for some reason your Windows machine does not run the HTTP server on port 80 (configurable in the httpd.conf that comes with the Apache inside Wamp), you can specify the port in the URL as well (port 8080 in this example):

this.webview.loadUrl("http://ipaddress:8080/");
Community
  • 1
  • 1
David S.
  • 6,567
  • 1
  • 25
  • 45
0

I had the same problem when I built my WAMP server and show database from my Android Phone.

the issue is that the WampServer is a localHost and the unique way to connect with WAMP is through Wifi, because use the same infrastructure.

If you want to connect out of your infrastructure you should build a public Hosting and try again.

Please show us the logcat and review if you have permission on your manifest file.

<uses-permission android:name="android.permission.INTERNET" />
Slinker
  • 379
  • 1
  • 4
  • 14