How to connect android to server(PC) and pass values to it
Asked
Active
Viewed 2.4k times
1
-
8This is a very broad question. What have you tried. Which code is already there? Where are your problems? Stackoverflow is not the place where other programmers solve your problems and provide sample code without you doing any of the work. Look at this question for some hints: http://stackoverflow.com/questions/1047698/regarding-connecting-to-a-webserver-from-android – Janusz Apr 23 '10 at 07:39
2 Answers
6
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();
}

Paul Gregoire
- 9,715
- 11
- 67
- 131
-
I dont know why but I cannot connect using this method, even if I try it to Google. Any suggestion? – Anggie Aziz Dec 17 '14 at 09:09
-
Things have changed in Android over the last 4 years since this was posted, but I imagine this very simple method still works. I would suggest adding debugging beyond the log and exception if you are not able to connect. – Paul Gregoire Dec 17 '14 at 12:50
1
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
- 51,744
- 11
- 75
- 60