我正在尝试从我的mysql数据库中获取一些信息,即用户unique_id。我一直在线跟踪教程并更改部件只是创建返回json对象的简单函数。但是,当我尝试启动该方法时,我遇到了错误:
No database selected
value no of java.lang.String cannot be converted to JSONObject
第二个问题是因为我猜想从数据库连接或检索数据不成功,但我无法找到问题所在。 这是我的代码 的config.php
<?php
define("DB_HOST", "localhost");
define("DB_USER", "root");
define("DB_PASSWORD", "");
define("DB_DATABASE", "test");
?>
DB_Connect.php
<?php
class DB_Connect {
function __construct() { }
function __destruct() { }
// Connecting to database
public function connect() {
require_once 'config.php';
// connecting to mysql
$con = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
// selecting database
mysql_select_db(DB_DATABASE);
// return database handler
return $con;
// Closing database connection
public function close() {
mysql_close();
}
}
?>
DB_Functions.php
<?php
class DB_Functions {
private $db;
//put your code here
// constructor
function __construct() {
require_once 'DB_Connect.php';
// connecting to database
$this->db = new DB_Connect();
$this->db->connect();
if(!$this->db){
echo "Please try later.";
}
}
// destructor
function __destruct() {
}
// Check if user exists.
public function isUserExisted($unique_id) {
$result = mysql_query("SELECT unique_id from users WHERE unique_id = '$unique_id'");
if($result === FALSE) {
die(mysql_error());
}
$no_of_rows = mysql_num_rows($result);
if ($no_of_rows > 0) {
// user existed
return true;
} else {
// user not existed
return false;
}
}
}
?>
的index.php
<?php
if (isset($_POST['tag']) && $_POST['tag'] != '') {
// get tag
$tag = $_POST['tag'];
// include db handler
require_once 'include/DB_Functions.php';
$db = new DB_Functions();
$response = array("tag" => $tag, "success" => 0, "error" => 0);
if ($tag == 'check') {
// Request unique_id
$unique_id = $_POST['unique_id'];
// check if user is already existed
if ($db->isUserExisted($unique_id)) {
// user is already existed - error response
$response["error"] = 2;
$response["error_msg"] = "User already existed";
echo json_encode($response);
} else {
$response["error"] = 1;
$response["error_msg"] = "USER UNKNOWN!";
echo json_encode($response);
}
}
}
?>
在我的android JSONParses里面看起来像这样:
public class JSONParser {
private static InputStream inputStream;
private static JSONObject jSONObject;
private static String json;
JSONParser(){
inputStream = null;
jSONObject = null;
json = " ";
}
public JSONObject getJSONFromUrl(String url, List<NameValuePair> parameters) throws ClientProtocolException{
try {
DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(parameters));
HttpResponse httpResponse = defaultHttpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
inputStream = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"), 8);
StringBuilder stringBuilder = new StringBuilder();
String line = null;
while((line = bufferedReader.readLine()) != null){
stringBuilder.append(line + "n");
}
inputStream.close();
json = stringBuilder.toString();
Log.e("JSON", json);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
jSONObject = new JSONObject(json);
} catch (JSONException e) {
Log.e("ERROR PARSING DATA: ",e.toString());
}
return jSONObject;
}
}
最后一段代码,使用JSONParser的方法(它在异步调用中)
private JSONObject checkIfUserExists(String unique_id){
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("tag", CHECK_TAG));
parameters.add(new BasicNameValuePair("unique_id", unique_id));
JSONObject json=null;
try {
json = jsonParser.getJSONFromUrl(loginURL, parameters);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return json;
}
我收到的错误是我的JSONParser中没有选择数据库。 我一直在查看代码,它似乎对我有好处,我知道这个问题很小,这就是为什么我找不到它。 任何帮助将非常感激。提前谢谢。
@Edit。我刚检查了一下
if($result === FALSE) {
echo "DEAD";
die(mysql_error());
}
这打印死了,所以看来我的查询是错误的,或者有点没有实现,但我不知道它可能有什么问题......