如何将android连接到服务器

如何将android连接到服务器,android,Android,如何将android连接到服务器(PC)并将值传递给它如果要连接到HTTP服务器,请查看;如果要使用自己的协议,请使用原始套接字。如果要连接到HTTP服务器,请查看;如果要使用自己的协议,请使用原始套接字。以下是发送“id”的方法和服务器的“名称”: URL url = null; try { String registrationUrl = String.format("http://myserver/register?id=%s&name=%s", myId

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

如果要连接到HTTP服务器,请查看;如果要使用自己的协议,请使用原始套接字。

如果要连接到HTTP服务器,请查看;如果要使用自己的协议,请使用原始套接字。

以下是发送“id”的方法和服务器的“名称”:


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

以下是向服务器发送“id”和“名称”的方法:


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

这是一个非常广泛的问题。你试过什么。哪个代码已经存在?你的问题在哪里?Stackoverflow不是其他程序员解决您的问题并提供示例代码而您不做任何工作的地方。看看这个问题,可以得到一些提示:这是一个非常广泛的问题。你试过什么。哪个代码已经存在?你的问题在哪里?Stackoverflow不是其他程序员解决您的问题并提供示例代码而您不做任何工作的地方。看看这个问题,可以得到一些提示:我不知道为什么,但我无法使用这种方法连接,即使我尝试使用谷歌。有什么建议吗?自从这篇文章发布以来,安卓系统在过去4年里发生了变化,但我认为这个非常简单的方法仍然有效。如果无法连接,我建议在日志和异常之外添加调试。我不知道为什么,但我无法使用此方法连接,即使我尝试使用Google。有什么建议吗?自从这篇文章发布以来,安卓系统在过去4年里发生了变化,但我认为这个非常简单的方法仍然有效。如果无法连接,我建议在日志和异常之外添加调试。