How to connect android to server

余生颓废 提交于 2019-12-14 03:39:53

问题


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


回答1:


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();
    }



回答2:


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.



来源:https://stackoverflow.com/questions/2696190/how-to-connect-android-to-server

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!