如何在外部主机(不是本地主机)上连接 Android
How to connect Android on external host (not localhost)
我决定连接到我的应用程序的外部主机,但不起作用。我能怎么做?
这是一个例子:
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();
}
在本地主机上工作,但是当我使用我的 url 时不起作用..(如果我在 google 地址上使用我的 url 他工作)
怎么了?
您是否应该在 URL url = new URL(reg_url)
中使用 login_url
而不是 reg_url
?我认为 reg_url
是用其他东西初始化的。
我决定连接到我的应用程序的外部主机,但不起作用。我能怎么做? 这是一个例子:
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();
}
在本地主机上工作,但是当我使用我的 url 时不起作用..(如果我在 google 地址上使用我的 url 他工作)
怎么了?
您是否应该在 URL url = new URL(reg_url)
中使用 login_url
而不是 reg_url
?我认为 reg_url
是用其他东西初始化的。