dongzhi9192 2013-09-26 11:46
浏览 67

如何通过json将数据从php正确发送到android?

After houres of trying to find a ways I like to ask how I can send data from PHP correctly to an Android application via JSON?

In my application I have a custom dialog, where I enter a E-Mail adress. This would be sent via JSONParser.makeHttpRequest to the php file. -> This is working! When I try to send the response back to my android application it gives me following error:

Logcat:

09-26 13:12:02.777: E/JSON Parser(26892): Error parsing data org.json.JSONException: Value 2013-09-26 of type java.lang.String cannot be converted to JSONObject

When I try to run the following php "check_mail.php" over the browser on my server it works and gives me following code:

output at normal webbrowser

2013-09-26 11:29:43 CLIENT -> SERVER: EHLO localhost 2013-09-26 11:29:43    
CLIENT -> SERVER: AUTH LOGIN 2013-09-26 11:29:43    
CLIENT -> SERVER: MzUyMjFtYWlsMQ== 2013-09-26 11:29:43  
CLIENT -> SERVER: eWx2dDNoOQ== 2013-09-26 11:29:43  
CLIENT -> SERVER: MAIL FROM: 2013-09-26 11:29:44    
CLIENT -> SERVER: RCPT TO: 2013-09-26 11:29:45  
CLIENT -> SERVER: DATA 2013-09-26 11:29:46  
CLIENT -> SERVER: Date: Thu, 26 Sep 2013 13:29:37 +0200 2013-09-26 11:29:46 
CLIENT -> SERVER: Return-Path: 2013-09-26 11:29:46  
CLIENT -> SERVER: To: zensored 2013-09-26 11:29:46  
CLIENT -> SERVER: From: zensored 2013-09-26 11:29:46 
CLIENT -> SERVER: Subject: CheatApp your login Data 2013-09-26 11:29:46 
CLIENT -> SERVER: Message-ID: <919740395d59ef297e32e9e0aaaee688@localhost> 2013-09-26 11:29:46  
CLIENT -> SERVER: X-Priority: 3 2013-09-26 11:29:46 
CLIENT -> SERVER: X-Mailer: PHPMailer 5.2.7 (https://github.com/PHPMailer/PHPMailer/) 2013-09-26 11:29:46   
CLIENT -> SERVER: MIME-Version: 1.0 2013-09-26 11:29:46 CLIENT -> SERVER: Content-Type: text/html; charset=iso-8859-1 2013-09-26 11:29:46   
CLIENT -> SERVER: Content-Transfer-Encoding: 8bit 2013-09-26 11:29:46   
CLIENT -> SERVER: 2013-09-26 11:29:46   
CLIENT -> SERVER: Body with login data... 2013-09-26 11:29:46   
CLIENT -> SERVER: 2013-09-26 11:29:46 
CLIENT -> SERVER: . 2013-09-26 11:29:46 
CLIENT -> SERVER: QUIT {"success":1,"message":"Your data has been send to your email!"}

*FILES: *

reglogin.java - asynctask

String TAG_SUCCESS = "success";
String TAG_MESSAGE = "message";

/**
* Background Async Task to check the email
**/

    private class checkEMail extends AsyncTask<String, String, String> {        
        // show progressdialog, while email is be checked
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(loginreg.this);
            pDialog.setMessage("Check entered emailadress...");
            pDialog.setIndeterminate(false);            
            pDialog.show();            
        }

        /**
         * Check the mail
         * */
        @Override
        protected String doInBackground(String... args) {
            // new jsonParser
            jsonParser = new JSONParser();

            // configure the paremeters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("BENUTZER_EMAIL", email));

            // Log the entered emailadress
            Log.v("EMAIL: ", email);

            // configure json variable
            JSONObject json = jsonParser.makeHttpRequest(url_check_mail,
                    "POST", params);

            // give a log if json is null
            if(json == null){
                Log.v("JSON: ", "IS NULL");
            }

            // check for message & success tag
            try {
                Log.v("message", json.getString(TAG_MESSAGE));
                Log.v("success", ""+json.getInt(TAG_SUCCESS));
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

        /**
         * Dismiss the progressdialog an show a toastmessage
         * **/
        @Override
        protected void onPostExecute(String file_url) {         
            pDialog.dismiss();            
            Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
        }

    }

check_mail.php

<?php
    /*FILE DESCRIPTION
        Name: check_mail.php
        Developer: M. Seiz
        Last change: 26.09.2013
        ->  This php file is checking the email adress from reglogin.java and if the email exists it sends
            the login data to the entered email adress!
    */

    $response = array();

    //Check if required field isset
    if (isset($_POST['BENUTZER_EMAIL'])) {

        // write the email adress from android application to a local variable
        $useremail = $_POST['BENUTZER_EMAIL'];

        // include database connection class
        require_once 'db_connect.php';

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

        // check if the email exists
        $checkifuserexists = "SELECT COUNT(*) num FROM benutzer WHERE BENUTZER_EMAIL = '$useremail'";
        $result = mysql_query($checkifuserexists) or die('error');
        $row = mysql_fetch_assoc($result);

        // if there is an entry with this email
        if($row['num']) {
            // asking for userdata
            $checkifemailexists = "SELECT BENUTZER_NAME, BENUTZER_PASSWORT FROM benutzer WHERE BENUTZER_EMAIL = '$useremail'";
            $result = mysql_query($checkifemailexists) or die('error');
            $row = mysql_fetch_assoc($result);

            // write userdata to local variables
            $username = $row['BENUTZER_NAME'];
            $userpw = $row['BENUTZER_PASSWORT'];
            $useremail = $useremail;

            //include once the phpmail.php class
            require_once('phpmailer/class.phpmailer.php');

            // setting up the email data
            $mail = new PHPMailer();
            $mail->IsSMTP();
            $mail->SMTPDebug = 1;
            $mail->SMTPAuth = true;
            $mail->SMTPSecure = 'ssl';
            $mail->Host = "smtp.easyname.eu";
            $mail->Port = 465;
            $mail->IsHTML(true);
            $mail->Username = "zensored";
            $mail->Password = "zensored";
            $mail->SetFrom("zensored");
            $mail->Subject = 'CheatApp your login Data';
            $mail->Body = 'Body with login data...';
            $mail->AddAddress($useremail);

            // if the mail could not be send...
            if(!$mail->Send()){
                $response["success"] = 0;
                $response["message"] = 'E-Mail could not be send!';
                echo json_encode($response);
            } else {
                $response["success"] = 1;
                $response["message"] = 'Your data has been send to your email!';
                echo json_encode($response);
            }

        } else {
            $response["success"] = 0;
            $response["message"] = 'E-Mail does not exist!';
            echo json_encode($response);
        }

    } else {
        $response["success"] = 0;
        $response["message"] = "Required field(s) is missing";
        echo json_encode($response);
    }
?>
  • 写回答

1条回答 默认 最新

  • doqp87012 2013-09-27 10:47
    关注

    Okay finally I figured it out by myself. The problem is / was that "class.phpmailer.php" from "check_mail.php" has another class included, which is called "class.smtp.php" If you take a look to the first line at the browser output, there is a date with the time!

    LogCat says:

    09-26 13:12:02.777: E/JSON Parser(26892): Error parsing data org.json.JSONException: Value 2013-09-26 of type java.lang.String cannot be converted to JSONObject
    

    So I don't know why but this date is given too to the jsonparser response!

    This time is printed in "class.smtp.php" here:

    echo gmdate('Y-m-d')."\t".trim($str)."
    ";
    

    After the outcommenting of this line, it works very well!!

    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集