Reputation: 3932
How to connect android to server(PC) and pass values to it
Upvotes: 1
Views: 23822
Reputation: 9793
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();
}
Upvotes: 6
Reputation: 52002
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.
Upvotes: 1