francesco bocci francesco bocci - 2 months ago 10
Android Question

How to connect Android on external host (not localhost)

I decided to connect to an external host my app but don't work. How can i do?
this is an exemple:

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


with localhost work, but when i use my url don't work.. (if i use my url on google adress he work)

what is wrong?

Answer

Are you supposed to be using login_url rather than reg_url in URL url = new URL(reg_url)? I think reg_url was initialized with something else.

Comments