0

我是编程新手...我尝试从android调用webservice。但我在那里有致命错误。

1.这是安卓编码

public class UserFunction {
    private JSONParser jsonParser;

    private static String LoginURL="http://10.0.2.2/android/include/index.php";
    private static String RegisterURL="http://10.0.2.2/android/include/index.php";

    private static String login_tag="login";
    private static String register_tag="register";


    public UserFunction(){
        jsonParser=new JSONParser();
    }


    public JSONObject loginUser(String email, String password){
        //building parameter
        List<NameValuePair> params= new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", login_tag));
        params.add(new BasicNameValuePair("email", email));
        params.add(new BasicNameValuePair("password", password));
        JSONObject json=jsonParser.getJSONFromUrl(LoginURL, params);

        return json;
    }


    public JSONObject registerUser(String email, String name, String password){
        //building parameter
        List<NameValuePair> params= new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", login_tag));
        params.add(new BasicNameValuePair("name", name));
        params.add(new BasicNameValuePair("email", email));
        params.add(new BasicNameValuePair("password", password));
        //getting JSONObject
        JSONObject json=jsonParser.getJSONFromUrl(RegisterURL, params);

        return json;
    }


    public boolean isUserLoggedIn(Context context){
        DatabaseHandler db= new DatabaseHandler(context);
        int count=db.getRowCount();
        if(count>0){
            return true;
        }
        return false;
    }


    public boolean logoutUser(Context context){
        DatabaseHandler db=new DatabaseHandler(context);
        db.resetTable();
        return false;   
    }
}

2.这是index.php

if(isset($_POST['tag'])&& $_POST['tag'] !=''){
        //get tag
        $tag=$_POST['tag'];

        //include DB handler
        require_once ('DB_Functions.php');//ganti include
        $db=new DB_Functions();

        //response array
        $response=array ("tag"=> $tag, "Success"=>0, "error"=>0);

        //check 4 tag type
        if($tag='login'){
            //req type is check login
            $email=$_POST['email'];
            $password=$_POST['password'];

            //check 4 user
            $user=$db->GetUserByEmailAndPassword($email, $password);
            if($user !=false){
                /* user found
                * echo JSON with Success =1 */
                $response ["Success"]=1;
                $response["uid"]=$user["unique_id"];
                $response ["user"]["name"]=$user["name"];
                $response["user"]["email"]=$user["email"];
                $response ["user"]["created_at"]=$user["created_at"];
                $response["user"]["updated_at"]=$user["updated_at"];
                echo json_encode($response);
            }
            else{
                /* user not found
                * echo JSON with error =1 */
                $response ["Error"]=1;
                $response["error_msg"]="INCORRECT EMAIL OR PASSWORD!!!";
                echo json_encode($response);
            }
        }
        else if($tag=='register'){
            //req type is register new user
            $name=$_POST['name'];
            $email=$_POST['email'];
            $password=$_POST['password'];

            //check if user already existed
            if($db->IsUserExisted($email)){
                //user is already existed - error response
                $response["error"]=2;
                $response["error_msg"]="USER IS ALREADY EXISTS";
                echo json_encode($response);
            }
            else{
                //store user
                $user=$db->StoreUser($name, $email, $password);
                if($user){
                    //user stored successfull
                    $response ["Success"]=1;
                    $response["uid"]=$user["unique_id"];
                    $response ["user"]["name"]=$user["name"];
                    $response["user"]["email"]=$user["email"];
                    $response ["user"]["created_at"]=$user["created_at"];
                    $response["user"]["updated_at"]=$user["updated_at"];
                    echo json_encode($response);
                }
                else{
                    //user failed to store
                    $response ["Error"]=1;
                    $response["error_msg"]="ERROR OCCURED IN REGISTRATION";
                    echo json_encode($response);
                }
            }
        }
        else{
            echo "INVALID REQUEST";
        }
    }
    else{
        echo "ACCESS DENIED";
    }
?>

3. 这是 DB_Functions.php

<?php
    class DB_Functions{
        private $db;
        /*constructor*/
        function __construct($db){
            require_once ('DB_Connect.php');//ganti include
            /*connecting DB*/
            $this->db= new DB_Connect();
            $this->db->connect();
        }
        /*destructor*/
        function __destruct($db){
        }

        /*
        *storing new user
        *returning user detail
        */

        public function StoreUser($name, $email, $password){
            $uuid= uniqid('',true);
            $hash= $this->hashSSHA($password);
            $encrypted_password= $hash["encrypted"]; //encripted password
            $salt=$hash["salt"];//salt
            $result=mysql_query("INSERT INTO account_users(unique_id, name, email, encrypted_password, salt, created_at) VALUES ('$uuid','$name','$email','$encrypted_password','$salt',NOW())");
            /*check for succesfull store*/
            if($result){
                /*get user detail*/
                $uid= mysql_insert_id();
                $result= mysql_query("SELECT * FROM account_users WHERE uid='$uid'");
                /*return user detail*/
                return mysql_fetch_array($result);
            }
            else{
                return false;
            }
        }
        /*
        *get user by email & password
        */

        public function GetUserByEmailAndPassword($email, $password){
            $result=mysql_query("SELECT * FROM account_users WHERE email='$email'") or die (mysql_error());
            //check 4 result
            $no_of_rows=mysql_num_rows($result);
            if($no_of_rows>0){
                $result=mysql_fetch_array($result);
                $salt=$result['salt'];
                $encrypted_password=$result['encrypted_password'];
                $hash=$this->checkhashSSHA($salt, $password);
                //check pass equality
                if($encrypted_password==$hash){
                    //user auth are correct
                    return $result;
                }
            }
            else{
                //user not found
                return false;
            }
        }

        /*
        *check user existed or not
        */
        public function IsUserExisted($email){
            $result=mysql_query("SELECT email FROM account_users WHERE email='$email'");
            $no_of_rows=mysql_num_rows($result);
            if($no_of_rows>0){
                //user existed
                return true;
            }
            else{
                //user not existed
                return false;
            }
        }
        /*
        *encrypting pass
        *@param pass
        *return salt & encrpted pass
        */
        public function hashSHHA($password){
            $salt=sha1(rand());
            $salt=substr($salt, 0, 10);
            $encrypted= base64_encode(sha1($password.$salt, true).$salt);
            $hash=array("salt"=> $salt, "encrypted"=> $encrypted);
            return $hash();
        }
        /*
        *decrypting pass
        *@param salt, pass
        *return hash string
        */
        public function CheckHashSSHA($salt, $password){
            $hash=base64_encode(sha1($password.$salt, true).$salt);
            return $hash;
        }

    }
?>

4.最后这是我运行android代码时的错误

07-18 02:07:59.393: D/dalvikvm(541): GC_EXPLICIT freed 3420 objects / 314064 bytes in 340ms
07-18 02:08:08.802: E/JSON(541): <br />n<b>Fatal error</b>:  Destructor DB_Functions::__destruct() cannot take arguments in <b>C:\xampp\htdocs\android\include\DB_Functions.php</b> on line <b>13</b><br />n
07-18 02:08:08.802: E/JSON PARSER(541): Error parsing dataorg.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
07-18 02:08:08.802: W/System.err(541): java.lang.NullPointerException
07-18 02:08:08.812: W/System.err(541):  at com.androidTA.androidclient.RegisterActivity$1.onClick(RegisterActivity.java:58)
07-18 02:08:08.812: W/System.err(541):  at android.view.View.performClick(View.java:2408)
07-18 02:08:08.812: W/System.err(541):  at android.view.View$PerformClick.run(View.java:8816)
07-18 02:08:08.812: W/System.err(541):  at android.os.Handler.handleCallback(Handler.java:587)
07-18 02:08:08.812: W/System.err(541):  at android.os.Handler.dispatchMessage(Handler.java:92)
07-18 02:08:08.812: W/System.err(541):  at android.os.Looper.loop(Looper.java:123)
07-18 02:08:08.812: W/System.err(541):  at android.app.ActivityThread.main(ActivityThread.java:4627)
07-18 02:08:08.822: W/System.err(541):  at java.lang.reflect.Method.invokeNative(Native Method)
07-18 02:08:08.858: W/System.err(541):  at java.lang.reflect.Method.invoke(Method.java:521)
07-18 02:08:08.858: W/System.err(541):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-18 02:08:08.858: W/System.err(541):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-18 02:08:08.862: W/System.err(541):  at dalvik.system.NativeStart.main(Native Method)

我的破坏功能有问题...,请任何人帮我解决这个问题...,非常感谢您的解决方案,,,:)

4

2 回答 2

2

从构造函数/析构函数参数中删除所有 $db 参数。像这样:

    function __construct(){
        require_once ('DB_Connect.php');//ganti include
        /*connecting DB*/
        $this->db= new DB_Connect();
        $this->db->connect();
    }

您不需要参数列表中的 $db,因为您通过 $this->db 引用,所以它不是必需的。

    function __destruct(){
    }

您不必传递任何其他参数,因为该对象已被完全清除

于 2013-07-17T19:33:01.547 回答
1

只需从析构函数中删除$db参数。它应该看起来像这样。

/*destructor*/
function __destruct(){
}

如果您需要从析构函数访问数据库,您可以随时使用,$this->db因为您已经在构造函数中对其进行了初始化。

除此之外,您还应该真正$db从构造函数中删除参数。DB_functions它没有被使用,并且当您在 index.php 中构造对象时,您不会传递任何参数。

于 2013-07-17T19:32:47.917 回答