如何在外部主机(而非localhost)上连接Android

时间:2016-09-19 13:42:14

标签: android login host

我决定连接到我的应用程序的外部主机但不起作用。我能怎么做? 这是一个例子:

  String login_url = "http://***.**.**.**/fileman/**/register.php";
   // String login_url = "http://10.0.2.2/code/register.php";

    String method = params[0];

    if (method.equals("register")) {


        String name = params[1];
        String user_name = params[2];
        String user_pass = params[3];

        try {
            URL url = new URL(reg_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            //httpURLConnection.setDoInput(true);
            OutputStream OS = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
            String data = URLEncoder.encode("user", "UTF-8") + "=" + URLEncoder.encode(name, "UTF-8") + "&" +
                    URLEncoder.encode("user_name", "UTF-8") + "=" + URLEncoder.encode(user_name, "UTF-8") + "&" +
                    URLEncoder.encode("user_pass", "UTF-8") + "=" + URLEncoder.encode(user_pass, "UTF-8");
            bufferedWriter.write(data);
            bufferedWriter.flush();
            bufferedWriter.close();
            OS.close();
            InputStream IS = httpURLConnection.getInputStream();
            IS.close();
            //httpURLConnection.connect();
            httpURLConnection.disconnect();


           ritorno = "Registration Success...";
            return ritorno;

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

使用localhost工作,但是当我使用我的网址时不起作用..(如果我在google地址上使用我的网址,他的工作)

出了什么问题?

1 个答案:

答案 0 :(得分:1)

您应该在login_url中使用reg_url而不是URL url = new URL(reg_url)吗?我认为reg_url已用其他内容初始化。