不在线我的Android应用程序

时间:2014-12-31 08:32:11

标签: java android

您好我正在为该注册设计Android聊天应用程序并登录以检查身份验证。 但在下面的代码当我运行localhost工作完美时,我将所有文件移动到服务器不工作。 任何人都可以解决这个问题。

signup.java

 signUpButton.setOnClickListener(new OnClickListener(){
                public void onClick(View arg0) 
                {                       
                    if (usernameText.length() > 0 &&        
                        passwordText.length() > 0 && 
                        passwordAgainText.length() > 0 &&
                        eMailText.length() > 0
                        )
                    {


                        if (passwordText.getText().toString().equals(passwordAgainText.getText().toString())){

                            if (usernameText.length() >= 5 && passwordText.length() >= 5) {

                                    Thread thread = new Thread(){
                                        String result = new String();
                                        @Override
                                        public void run() {
                                            result = imService.signUpUser(usernameText.getText().toString(), 
                                                    passwordText.getText().toString(), 
                                                    eMailText.getText().toString());

                                            handler.post(new Runnable(){

                                                public void run() {
                                                    if (result.equals(SERVER_RES_RES_SIGN_UP_SUCCESFULL)) {
                                                        Toast.makeText(getApplicationContext(),R.string.signup_successfull, Toast.LENGTH_LONG).show();

                                                    }
                                                    else if (result.equals(SERVER_RES_SIGN_UP_USERNAME_CRASHED)){
                                                        Toast.makeText(getApplicationContext(),R.string.signup_username_crashed, Toast.LENGTH_LONG).show();

                                                    }
                                                    else  
                                                    {
                                                        Toast.makeText(getApplicationContext(),R.string.signup_failed, Toast.LENGTH_LONG).show();

                                                    }           
                                                }

                                            });
                                        }

                                    };
                                    thread.start();
                            }
                            else{
                                Toast.makeText(getApplicationContext(),R.string.username_and_password_length_short, Toast.LENGTH_LONG).show();

                            }                           
                        }
                        else {
                            Toast.makeText(getApplicationContext(),R.string.signup_type_same_password_in_password_fields, Toast.LENGTH_LONG).show();

                        }

                    }
                    else {
                        Toast.makeText(getApplicationContext(),R.string.signup_fill_all_fields, Toast.LENGTH_LONG).show();


                    }               
                }           
            });

IMService.java

public String signUpUser(String usernameText, String passwordText,
            String emailText) 
    {
        String params = "username=" + usernameText +
                        "&password=" + passwordText +
                        "&action=" + "signUpUser"+
                        "&email=" + emailText+
                        "&";

        String result = socketOperator.sendHttpRequest(params);     

        return result;
    }

SocketOperator.java

public String sendHttpRequest(String params)
    {       
        URL url;
        String result = new String();
        try 
        {
            url = new URL(AUTHENTICATION_SERVER_ADDRESS);
            HttpURLConnection connection;
            connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);

            PrintWriter out = new PrintWriter(connection.getOutputStream());

            out.println(params);
            out.close();

            BufferedReader in = new BufferedReader(
                    new InputStreamReader(
                            connection.getInputStream()));
            String inputLine;

            while ((inputLine = in.readLine()) != null) {
                result = result.concat(inputLine);              
            }
            in.close();         
        } 
        catch (MalformedURLException e) {
            e.printStackTrace();
        } 
        catch (IOException e) {
            e.printStackTrace();
        }           

        if (result.length() == 0) {
            result = HTTP_REQUEST_FAILED;
        }

        return result;


    }

logcat的

12-31 03:51:31.859: E/AndroidRuntime(3269): FATAL EXCEPTION: main
12-31 03:51:31.859: E/AndroidRuntime(3269): Process: at.vcity.androidim, PID: 3269
12-31 03:51:31.859: E/AndroidRuntime(3269): java.lang.NullPointerException
12-31 03:51:31.859: E/AndroidRuntime(3269):     at at.vcity.androidim.SignUp$2$1$1.run(SignUp.java:106)
12-31 03:51:31.859: E/AndroidRuntime(3269):     at android.os.Handler.handleCallback(Handler.java:733)
12-31 03:51:31.859: E/AndroidRuntime(3269):     at android.os.Handler.dispatchMessage(Handler.java:95)
12-31 03:51:31.859: E/AndroidRuntime(3269):     at android.os.Looper.loop(Looper.java:136)
12-31 03:51:31.859: E/AndroidRuntime(3269):     at android.app.ActivityThread.main(ActivityThread.java:5017)
12-31 03:51:31.859: E/AndroidRuntime(3269):     at java.lang.reflect.Method.invokeNative(Native Method)
12-31 03:51:31.859: E/AndroidRuntime(3269):     at java.lang.reflect.Method.invoke(Method.java:515)
12-31 03:51:31.859: E/AndroidRuntime(3269):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-31 03:51:31.859: E/AndroidRuntime(3269):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-31 03:51:31.859: E/AndroidRuntime(3269):     at dalvik.system.NativeStart.main(Native Method)

的index.php

<?php


    require_once("mysql.class.php");


    $dbHost = "xxx";
    $dbUsername = "xxx";
    $dbPassword = "xxxx";
    $dbName = "xxx";


    $db = new MySQL($dbHost,$dbUsername,$dbPassword,$dbName);


    define("FAILED", 0);

    define("SUCCESSFUL", 1);

    define("SIGN_UP_USERNAME_CRASHED", 2);  

    define("ADD_NEW_USERNAME_NOT_FOUND", 2);


    define("TIME_INTERVAL_FOR_USER_STATUS", 60);

    define("USER_APPROVED", 1);
    define("USER_UNAPPROVED", 0);


    $username = (isset($_REQUEST['username']) && count($_REQUEST['username']) > 0) 
                                ? $_REQUEST['username'] 
                                : NULL;
    $password = isset($_REQUEST['password']) ? md5($_REQUEST['password']) : NULL;
    $port = isset($_REQUEST['port']) ? $_REQUEST['port'] : NULL;

    $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : NULL;
    if ($action == "testWebAPI")
    {
        if ($db->testconnection()){
        echo SUCCESSFUL;
        exit;
        }else{
        echo FAILED;
        exit;
        }
    }

    if ($username == NULL || $password == NULL)  
    {
        echo FAILED;
        exit;
    }

    $out = NULL;

    error_log($action."\r\n", 3, "error.log");
    switch($action) 
    {

        case "authenticateUser":


            if ($userId = authenticateUser($db, $username, $password)) 
            {                   



                $sql = "select u.Id, u.username, (NOW()-u.authenticationTime) as authenticateTimeDifference, u.IP, 
                                            f.providerId, f.requestId, f.status, u.port 
                                from friends f
                                left join users u on 
                                            u.Id = if ( f.providerId = ".$userId.", f.requestId, f.providerId ) 
                                where (f.providerId = ".$userId." and f.status=".USER_APPROVED.")  or 
                                             f.requestId = ".$userId." ";



                $sqlmessage = "SELECT m.id, m.fromuid, m.touid, m.sentdt, m.read, m.readdt, m.messagetext, u.username from messages m \n"
        . "left join users u on u.Id = m.fromuid WHERE `touid` = ".$userId." AND `read` = 0 LIMIT 0, 30 ";


                if ($result = $db->query($sql))         
                {
                        $out .= "<data>"; 
                        $out .= "<user userKey='".$userId."' />";
                        while ($row = $db->fetchObject($result))
                        {
                            $status = "offline";
                            if (((int)$row->status) == USER_UNAPPROVED)
                            {
                                $status = "unApproved";
                            }
                            else if (((int)$row->authenticateTimeDifference) < TIME_INTERVAL_FOR_USER_STATUS)
                            {
                                $status = "online";

                            }
                            $out .= "<friend  username = '".$row->username."'  status='".$status."' IP='".$row->IP."' userKey = '".$row->Id."'  port='".$row->port."'/>";



                        }
                            if ($resultmessage = $db->query($sqlmessage))           
                                {
                                while ($rowmessage = $db->fetchObject($resultmessage))
                                    {
                                    $out .= "<message  from='".$rowmessage->username."'  sendt='".$rowmessage->sentdt."' text='".$rowmessage->messagetext."' />";
                                    $sqlendmsg = "UPDATE `messages` SET `read` = 1, `readdt` = '".DATE("Y-m-d H:i")."' WHERE `messages`.`id` = ".$rowmessage->id.";";
                                    $db->query($sqlendmsg);
                                    }
                                }
                        $out .= "</data>";
                }
                else
                {
                    $out = FAILED;
                }           
            }
            else
            {

                    $out = FAILED;
            }



        break;

        case "signUpUser":
            if (isset($_REQUEST['email']))
            {
                 $email = $_REQUEST['email'];       

                 $sql = "select Id from  users 
                                where username = '".$username."' limit 1";



                 if ($result = $db->query($sql))
                 {
                        if ($db->numRows($result) == 0) 
                        {
                                $sql = "insert into users(username, password, email)
                                    values ('".$username."', '".$password."', '".$email."') ";                          

                                    error_log("$sql", 3 , "error_log");
                                if ($db->query($sql))   
                                {
                                        $out = SUCCESSFUL;
                                }               
                                else {
                                        $out = FAILED;
                                }                           
                        }
                        else
                        {
                            $out = SIGN_UP_USERNAME_CRASHED;
                        }
                 }                      
            }
            else
            {
                $out = FAILED;
            }   
        break;

        case "sendMessage":
        if ($userId = authenticateUser($db, $username, $password)) 
            {   
            if (isset($_REQUEST['to']))
            {
                 $tousername = $_REQUEST['to']; 
                 $message = $_REQUEST['message'];   

                 $sqlto = "select Id from  users where username = '".$tousername."' limit 1";



                        if ($resultto = $db->query($sqlto))         
                        {
                            while ($rowto = $db->fetchObject($resultto))
                            {
                                $uto = $rowto->Id;
                            }
                            $sql22 = "INSERT INTO `messages` (`fromuid`, `touid`, `sentdt`, `messagetext`) VALUES ('".$userId."', '".$uto."', '".DATE("Y-m-d H:i")."', '".$message."');";                       

                                    error_log("$sql22", 3 , "error_log");
                                if ($db->query($sql22)) 
                                {
                                        $out = SUCCESSFUL;
                                }               
                                else {
                                        $out = FAILED;
                                }                       
                            $resultto = NULL;
                        }   

            $sqlto = NULL;
            }
            }
            else
            {
                $out = FAILED;
            }   
        break;

        case "addNewFriend":
            $userId = authenticateUser($db, $username, $password);
            if ($userId != NULL)
            {

                if (isset($_REQUEST['friendUserName']))         
                {               
                     $friendUserName = $_REQUEST['friendUserName'];

                     $sql = "select Id from users 
                                     where username='".$friendUserName."' 
                                     limit 1";
                     if ($result = $db->query($sql))
                     {
                            if ($row = $db->fetchObject($result))
                            {
                                 $requestId = $row->Id;

                                 if ($row->Id != $userId)
                                 {
                                         $sql = "insert into friends(providerId, requestId, status)
                                             values(".$userId.", ".$requestId.", ".USER_UNAPPROVED.")";

                                         if ($db->query($sql))
                                         {
                                                $out = SUCCESSFUL;
                                         }
                                         else
                                         {
                                                $out = FAILED;
                                         }
                                }
                                else
                                {
                                    $out = FAILED;  
                                }                                                
                            }
                            else
                            {
                                $out = FAILED;                      
                            }
                     }                               
                     else
                     {
                            $out = FAILED;
                     }              
                }
                else
                {
                        $out = FAILED;
                }           
            }
            else
            {
                $out = FAILED;
            }   
        break;

        case "responseOfFriendReqs":
            $userId = authenticateUser($db, $username, $password);
            if ($userId != NULL)
            {
                $sqlApprove = NULL;
                $sqlDiscard = NULL;
                if (isset($_REQUEST['approvedFriends']))
                {
                      $friendNames = split(",", $_REQUEST['approvedFriends']);
                      $friendCount = count($friendNames);
                      $friendNamesQueryPart = NULL;
                      for ($i = 0; $i < $friendCount; $i++)
                      {
                        if (strlen($friendNames[$i]) > 0)
                        {
                            if ($i > 0 )
                            {
                                $friendNamesQueryPart .= ",";
                            }

                            $friendNamesQueryPart .= "'".$friendNames[$i]."'";

                        }               

                      }
                      if ($friendNamesQueryPart != NULL)
                      {
                        $sqlApprove = "update friends set status = ".USER_APPROVED."
                                        where requestId = ".$userId." and 
                                                    providerId in (select Id from users where username in (".$friendNamesQueryPart."));
                                    ";      
                      }

                }
                if (isset($_REQUEST['discardedFriends']))
                {
                        $friendNames = split(",", $_REQUEST['discardedFriends']);
                      $friendCount = count($friendNames);
                      $friendNamesQueryPart = NULL;
                      for ($i = 0; $i < $friendCount; $i++)
                      {
                        if (strlen($friendNames[$i]) > 0)
                        {
                            if ($i > 0 )
                            {
                                $friendNamesQueryPart .= ",";
                            }

                            $friendNamesQueryPart .= "'".$friendNames[$i]."'";

                        }                   
                      }
                      if ($friendNamesQueryPart != NULL)
                      {
                        $sqlDiscard = "delete from friends 
                                            where requestId = ".$userId." and 
                                                        providerId in (select Id from users where username in (".$friendNamesQueryPart."));
                                                ";
                      }                     
                }
                if (  ($sqlApprove != NULL ? $db->query($sqlApprove) : true) &&
                            ($sqlDiscard != NULL ? $db->query($sqlDiscard) : true) 
                   )
                {
                    $out = SUCCESSFUL;
                }
                else
                {
                    $out = FAILED;
                }       
            }
            else
            {
                $out = FAILED;
            }
        break;

        default:
            $out = FAILED;      
            break;  
    }

    echo $out;



    ///////////////////////////////////////////////////////////////
    function authenticateUser($db, $username, $password)
    {

        $sql22 = "select * from users 
                        where username = '".$username."' and password = '".$password."' 
                        limit 1";

        $out = NULL;
        if ($result22 = $db->query($sql22))
        {
            if ($row22 = $db->fetchObject($result22))
            {
                    $out = $row22->Id;

                    $sql22 = "update users set authenticationTime = NOW(), 
                                                                     IP = '".$_SERVER["REMOTE_ADDR"]."' ,
                                                                     port = 15145 
                                    where Id = ".$row22->Id."
                                    limit 1";

                    $db->query($sql22);             


            }       
        }

        return $out;
    }

    ?>

0 个答案:

没有答案