简体   繁体   中英

Android App can't find PHP function

I have an Android app with Eclipse. I want to select values from mySql with php files. My app was working exactly before I installed slimfiremawork. After I installed slimfremawork, my app has error;

org.json.JSONException: Value 404 of type java.lang.String cannot be converted to JSONObject

My jsonparser class is;

try {
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost("http://172.20.10.32/android_connect/index.php/ogr_giris_kontrol?tc=12&sifre='hh'");
                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                gelenVeriler = httpEntity.getContent();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                BufferedReader oku = new BufferedReader(new InputStreamReader(
                        gelenVeriler, "iso-8859-1"), 8);
                sb = new StringBuilder();
                String satir = null;
                while ((satir = oku.readLine()) != null) {
                    sb.append(satir + "\n");
                }
                gelenVeriler.close();
                okunanDeger = sb.toString();
            } catch (Exception e) {
                Log.e("jparse", "Buffer Hatası :  " + e.toString());
            }
            try {
                jObje = new JSONObject(okunanDeger);
            } catch (JSONException e) {
                Log.e("jparse", "Obje Parse işlemi hatası " + e.toString());
            }

Actually url is working on browser but JsonParser class can't read the url. When I write sb, I get this strings;

> <html><head><title>404 Page Not
> Found</title><style>body{margin:0;padding:30px;font:12px/1.5
> Helvetica,Arial,Verdana,sans-serif;}h1{margin:0;font-size:48px;font-weight:normal;line-height:48px;}strong{display:inline-block;width:65px;}</style></head><body><h1>404
> Page Not Found</h1><p>The page you are looking for could not be found.
> Check the address bar to ensure your URL is spelled correctly. If all
> else fails, you can visit our home page at the link below.</p><a
> href="/android_connect/index.php/">Visit the Home
> Page</a></body></html>

My php is;

<?php
//require '/Applications/XAMPP/xamppfiles/lib/php/PHPUnit/Framework/MockObject/Autoload.php';
require __DIR__.'/vendor/autoload.php';

$app = new \Slim\Slim();

define('DB_USER', "root"); 
define('DB_PASSWORD', ""); 
define('DB_DATABASE', "BritishTower");
define('DB_SERVER', "localhost"); 


        // Connecting to mysql database
        $con = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysql_error());

        // Selecing database
        $db = mysql_select_db(DB_DATABASE) or die(mysql_error()) or die(mysql_error());
$app->get('/ogr_giris_kontrol', function () {
    $response = array();
    if (empty($_GET["tc"]) || empty($_GET["sifre"])) {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);

} else {
    $tc = $_GET['tc'];
    $sifre = $_GET['sifre'];

    // get a product from products table
    $result = mysql_query("SELECT *FROM ogrenciler WHERE tc = $tc and sifre=$sifre");

    if (!empty($result)) {
        // check for empty result
        if (mysql_num_rows($result) > 0) {


            $response["success"] = 1;

            // user node
            $response["message"] = "Mevcuttur";

            // echoing JSON response
            echo json_encode($response);
        } else {
            // no product found
            $response["success"] = 0;
            $response["message"] = "Öğrenci bulunamadı";
            // echo no users JSON
            echo json_encode($response);
        }
    } else {
        // no product found
        $response["success"] = 0;
        $response["message"] = "Öğrenci bulunamadı";

        // echo no users JSON
        echo json_encode($response);
    }
}
});

This php is working in browser but I don't understand why it is'n working in android?

I understand my problem. The problem is about my PHP connecting to database. Now I have 3 PHP files for connecting database.

First db_config.php

<?php
define('DB_USER', "root"); // db user
define('DB_PASSWORD', ""); // db password (mention your db password here)
define('DB_DATABASE', "BritishTower"); // database name
define('DB_SERVER', "localhost"); // db server
?>

Second db_connect.php

<?php

class DB_CONNECT {

    // constructor
    function __construct() {
        // connecting to database
        $this->connect();
    }

    // destructor
    function __destruct() {
        // closing db connection
        $this->close();
    }

    /**
     * Function to connect with database
     */
    function connect() {
        // import database connection variables
        require_once __DIR__ . '/db_config.php';

        // Connecting to mysql database
        $con = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysql_error());

        // Selecing database
        $db = mysql_select_db(DB_DATABASE) or die(mysql_error()) or die(mysql_error());

        // returing connection cursor
        return $con;
    }

    /**
     * Function to close db connection
     */
    function close() {
        // closing db connection
        mysql_close();
    }

}

?>

Finally my select query; index.php

<?php
//require '/Applications/XAMPP/xamppfiles/lib/php/PHPUnit/Framework/MockObject/Autoload.php';
require __DIR__.'/vendor/autoload.php';

$app = new \Slim\Slim();

require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();

$app->get('/ogr_giris_kontrol', function () {
    $response = array();
    if (empty($_GET["tc"]) || empty($_GET["sifre"])) {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);

} else {
    $tc = $_GET['tc'];
    $sifre = $_GET['sifre'];

    // get a product from products table
    $result = mysql_query("SELECT *FROM ogrenciler WHERE tc = $tc and sifre=$sifre");

    if (!empty($result)) {
        // check for empty result
        if (mysql_num_rows($result) > 0) {


            $response["success"] = 1;

            // user node
            $response["message"] = "Mevcuttur";

            // echoing JSON response
            echo json_encode($response);
        } else {
            // no product found
            $response["success"] = 0;
            $response["message"] = "Öğrenci bulunamadı";
            // echo no users JSON
            echo json_encode($response);
        }
    } else {
        // no product found
        $response["success"] = 0;
        $response["message"] = "Öğrenci bulunamadı";

        // echo no users JSON
        echo json_encode($response);
    }
}
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM