2

I'm currently trying to connect my phpmyadmin server with android studio. I'm using the emulator if it matters. I made some php files to receive data and change data in my database.

checkuserexist.php

<?php
require('con1.php');

if (isset($_GET['username'])) {
    $username = mysqli_real_escape_string($link,$_GET['username']);
    if (!empty($username)) {
            $username_query = mysqli_query($link,"SELECT * FROM users WHERE username='".$username."'");
            $username_result = mysqli_num_rows($username_query);
            if($username_result == 0)
                print $existornot = "NotExist";
            else print $existornot = "Exist";
    }
}
?>

And in my Android Studio program:

  URL url = new URL("http://10.0.2.2:8080/fives/checkuserexist.php?username=yariv");
  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  InputStream stream = conn.getInputStream();
  InputStreamReader isReader = new InputStreamReader(stream );

//put output stream into a string
  BufferedReader br = new BufferedReader(isReader );
  String result = "";
  String line;
  while ((line = br.readLine()) != null) {
  System.out.println(line);
  result += line;
  }
  br.close();

InputStream stream = conn.getInputStream(); return an exeption: failed to connect to /10.0.2.2 (port 8080): connect failed: ETIMEDOUT (Connection timed out) How to solve this exeption?

Yariv Gavriel
  • 53
  • 1
  • 9
  • This is more of a Java question that a PHP question. Check out this post, the TLDR of it all is you need to use a BufferedReader on the `conn.getInputStream()` – MarshallOfSound Sep 05 '15 at 09:28
  • http://stackoverflow.com/questions/12805107/httpurlconnection-how-to-get-the-xml-response-into-a-string – MarshallOfSound Sep 05 '15 at 09:28
  • try to use local ip instead localhost http://stackoverflow.com/questions/3378501/how-to-browse-localhost-on-android-device – Vlad Sep 05 '15 at 09:30

2 Answers2

1

You could echo out some JSON in the PHP file and use the JSONObject/JSONArray classes in android

Peter Chaula
  • 3,456
  • 2
  • 28
  • 32
0

should check the emulator network setting to see what is your main machine IP address.

second way : open run menu, type cmd and again type ipconfig. you can also try other IP v4 addresses found in the here:

Ahmad Behzadi
  • 1,006
  • 15
  • 30
  • my IP v4 is 192.168.1.20 So i tried: url = new URL("http://192.168.1.20:8080/fives/checkuserexist.php"); and the exeption is : failed to connect to /192.168.1.20 (port 8080): connect failed: ETIMEDOUT (Connection timed out) – Yariv Gavriel Sep 05 '15 at 10:17
  • I think you should have other IP addresses too. check emulator network settings. – Ahmad Behzadi Sep 05 '15 at 10:29
  • And another thing you should do is to encode every url param in android – Peter Chaula Sep 05 '15 at 10:42