[英]Android App can't find PHP function
I have an Android app with Eclipse. 我有一个带有Eclipse的Android应用程序。 I want to select values from mySql with php files.
我想从MySql中选择带有php文件的值。 My app was working exactly before I installed slimfiremawork.
在安装slimfiremawork之前,我的应用程序就可以正常工作。 After I installed slimfremawork, my app has error;
安装slimfremawork之后,我的应用出现错误;
org.json.JSONException: Value 404 of type java.lang.String cannot be converted to JSONObject
org.json.JSONException:类型为java.lang.String的值404无法转换为JSONObject
My jsonparser class is; 我的jsonparser类是;
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. 网址实际上在浏览器上有效,但JsonParser类无法读取该网址。 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是;
<?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? 这个PHP在浏览器中工作,但我不明白为什么它不能在Android中工作?
I understand my problem. 我明白我的问题。 The problem is about my PHP connecting to database.
问题出在我的PHP连接数据库上。 Now I have 3 PHP files for connecting database.
现在,我有3个PHP文件用于连接数据库。
First db_config.php 首先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 第二个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
的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);
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.