如何将android连接到服务器

Sij*_*ith 1 android

如何将android连接到服务器(PC)并将值传递给它

Pau*_*ire 6

这是一种向服务器发送"id"和"name"的方法:


    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();
    }
Run Code Online (Sandbox Code Playgroud)