I am trying to build Android Login app with PHP,mySQL and localhost. However it displays connection problems. I tried both emulator and Android device. I also tried IP address of my WLAN by finding its IP from ifconfig under Wireless LAN Adapter Wifi 2. My Android device uses the same WIFIconnection(although I have a repeater too, if that info is of any use). It used to display 'ehostunreach' in my emulator. And 'ehostunreach' & 'etimedout' in my Android device. I hope this much detail is enough. Any help would be much appreciated.
protected String doInBackground(String... params) {
String type = params[0];
String login_url = "http://10.0.2.2/login.php";
if(type.equals("login")) {
try {
String user_name = params[1];
String password = params[2];
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("user_name","UTF-8")+"="+URLEncoder.encode(user_name,"UTF-8")+"&"
+URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
bufferedWriter.write(post_data);
........
答案 0 :(得分:-1)
无法访问该服务器。尝试从PC上ping到10.0.2.2,可能会超时。首先尝试成功ping。也许服务器没有该IP地址,或者您的路由器正在过滤掉消息。