tags:

views:

125

answers:

2

How to connect android to server(PC) and pass values to it

+1  A: 

Take a look at HttpClient if you want to connect to an HTTP server, or use raw sockets if you want to roll your own protocol.

Erich Douglass
A: 

Here is a means for sending an "id" and "name" to a server:


    URL url = null;
    try {
        String registrationUrl = String.format("http://myserver/register?id=%s&name=%s", myId, URLEncoder.encode(myName,"UTF-8"));
        url = new URL(registrationUrl);
        URLConnection connection = url.openConnection();
        HttpURLConnection httpConnection = (HttpURLConnection) connection;
        int responseCode = httpConnection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            Log.d("MyApp", "Registration success");
        } else {
            Log.w("MyApp", "Registration failed for: " + registrationUrl);              
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
Mondain