Yariv Gavriel
Yariv Gavriel

Reputation: 53

how to get response from php file?

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?

Upvotes: 2

Views: 1297

Answers (2)

Ahmad Behzadi
Ahmad Behzadi

Reputation: 1026

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:

Upvotes: 0

Peter Chaula
Peter Chaula

Reputation: 3731

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

Upvotes: 1

Related Questions