duanbi2003 2017-06-23 06:08
浏览 134

Android JSON请求,Inputstream返回null

I have a website with register.php (running on localhost). the code in the website (AJAX jQuery) returns a JSON Array, all good by now. the code in Android returns null while I need the array.

Code:

HttpURLConnection urlConnection;
StringBuilder result = new StringBuilder();
DataOutputStream dataOutputStream;
try {
    URL url = parmeters[0].url;
    String username = parmeters[0].username;
    String email = parmeters[0].email;
    String password = parmeters[0].password;
    urlConnection = (HttpURLConnection) url.openConnection();
    urlConnection.setDoInput(true);
    urlConnection.setDoOutput(true);
    urlConnection.setConnectTimeout(5000);
    urlConnection.setRequestMethod("POST");
    urlConnection.setRequestProperty("Accept", "application/json");
    urlConnection.setRequestProperty("Content-Type", "application/json");
    urlConnection.connect();
    JSONObject jsonObj = new JSONObject();
    jsonObj.put("username", username);
    jsonObj.put("email", email);
    jsonObj.put("password", password);
    dataOutputStream = new DataOutputStream(urlConnection.getOutputStream());
    dataOutputStream.writeBytes(URLEncoder.encode(jsonObj.toString(), "UTF-8"));
    dataOutputStream.flush();
    dataOutputStream.close();
    int responsecode = urlConnection.getResponseCode();
    Log.w("OnStart", "doInBackground: " + responsecode);
    if (responsecode == 200 || responsecode == 201) {
        InputStream input = urlConnection.getInputStream();
        Scanner s = new Scanner(input).useDelimiter("\\A");
        result.append(s.hasNext() ? s.next() : "");
        }
    urlConnection.disconnect();
} catch (IOException | JSONException e) {
    Log.e("OnStart", e.toString());
}
Log.w("OnStart", result.toString());
return result.toString();

Stack-trace:

06-23 05:46:16.381 22500-22536/com.newworldgrip.lostandfound.lostandfound D/EGL_emulation: eglMakeCurrent: 0xa2d79580: ver 2 0 (tinfo 0xaed0b350) 06-23 05:46:20.046 22500-22506/com.newworldgrip.lostandfound.lostandfound I/zygote: Do partial code cache collection, code=28KB, data=30KB 06-23 05:46:20.048 22500-22506/com.newworldgrip.lostandfound.lostandfound I/zygote: After code cache collection, code=28KB, data=30KB 06-23 05:46:20.048 22500-22506/com.newworldgrip.lostandfound.lostandfound I/zygote: Increasing code cache capacity to 128KB 06-23 05:46:20.715 22500-22506/com.newworldgrip.lostandfound.lostandfound I/zygote: Do partial code cache collection, code=61KB, data=53KB 06-23 05:46:20.716 22500-22506/com.newworldgrip.lostandfound.lostandfound I/zygote: After code cache collection, code=61KB, data=53KB 06-23 05:46:20.716 22500-22506/com.newworldgrip.lostandfound.lostandfound I/zygote: Increasing code cache capacity to 256KB 06-23 05:46:22.452 22500-22506/com.newworldgrip.lostandfound.lostandfound I/zygote: Do full code cache collection, code=123KB, data=100KB 06-23 05:46:22.452 22500-22506/com.newworldgrip.lostandfound.lostandfound I/zygote: After code cache collection, code=116KB, data=73KB 06-23 05:46:23.648 22500-22506/com.newworldgrip.lostandfound.lostandfound I/zygote: Do partial code cache collection, code=122KB, data=84KB 06-23 05:46:23.648 22500-22506/com.newworldgrip.lostandfound.lostandfound I/zygote: After code cache collection, code=122KB, data=84KB 06-23 05:46:23.648 22500-22506/com.newworldgrip.lostandfound.lostandfound I/zygote: Increasing code cache capacity to 512KB 06-23 05:46:29.721 22500-22537/com.newworldgrip.lostandfound.lostandfound D/NetworkSecurityConfig: No Network Security Config specified, using platform default 06-23 05:46:29.737 22500-22506/com.newworldgrip.lostandfound.lostandfound I/zygote: Do full code cache collection, code=208KB, data=162KB 06-23 05:46:29.737 22500-22506/com.newworldgrip.lostandfound.lostandfound I/zygote: After code cache collection, code=205KB, data=134KB 06-23 05:46:29.742 22500-22537/com.newworldgrip.lostandfound.lostandfound W/OnStart: doInBackground: 200 06-23 05:46:29.763 22500-22536/com.newworldgrip.lostandfound.lostandfound D/EGL_emulation: eglMakeCurrent: 0xa2d79580: ver 2 0 (tinfo 0xaed0b350) 06-23 05:46:29.784 22500-22536/com.newworldgrip.lostandfound.lostandfound D/EGL_emulation: eglMakeCurrent: 0xa2d79580: ver 2 0 (tinfo 0xaed0b350) 06-23 05:46:29.828 22500-22536/com.newworldgrip.lostandfound.lostandfound D/EGL_emulation: eglMakeCurrent: 0xa2d79580: ver 2 0 (tinfo 0xaed0b350) 06-23 05:47:03.396 22500-22506/com.newworldgrip.lostandfound.lostandfound I/zygote: Do partial code cache collection, code=245KB, data=173KB 06-23 05:47:03.396 22500-22506/com.newworldgrip.lostandfound.lostandfound I/zygote: After code cache collection, code=245KB, data=173KB 06-23 05:47:03.396 22500-22506/com.newworldgrip.lostandfound.lostandfound I/zygote: Increasing code cache capacity to 1024KB

register.php:

<?php
/**
 * Created by PhpStorm.
 * User: Oded Shapira
 * Date: 6/2/2017
 * Time: 1:02 PM
 */
require_once "handler.php";
$db = new DataBaseHandler();
$returned = array("error" => FALSE);

if (isset($_POST['username']) && isset($_POST['password']) && isset($_POST['email'])) {
    $username = $_POST['username'];
    $password = $_POST['password'];
    $email = $_POST['email'];


    $result = $db->storeInDb($username, $email, $password);
    if ($result["error"]) {
        $returned["error"] = TRUE;
        $returned["error_msg"] = $result["error_msg"];
        echo json_encode($returned);
    } else {
        $returned["error"] = FALSE;
        $returned["user"] = $result;
        echo json_encode($returned);
    }

} else {
    $returned["error"] = TRUE;
    $returned["error_msg"] = "missing parameters";
    json_encode($returned);
}
?>

handler.php:

<?php
/**
 * Created by PhpStorm.
 * User: Oded Shapira
 * Date: 6/14/2017
 * Time: 3:41 PM
 */
class DB_Connect {
    private $conn;

    public function connect(){
        require_once 'include/Config.php';

        $this->conn = new mysqli(HOST,USER, PASS, DATABASE);
        if (!mysqli_connect_errno()){
            return $this->conn;
        } else {
            die("Sql error: " . $this->conn->error);
        }
    }
}

class DataBaseHandler {
    private $conn;

    function __construct()
    {
        $db = new DB_connect();
        $this->conn = $db->connect();
    }

    function __destruct()
    {
        if ($this->conn != null) {
            $this->conn->close();
        }
    }

    public function storeInDb($username, $email, $password) {
        $encrypted = md5($password);
        if ($this->checkExistence($email)) {
            return array("error"=>true, "error_msg"=>"Account with the same email exists.");
        } else {
            $result = $this->conn->prepare("INSERT INTO accounts (username, password, email) VALUES 
                                      (?, ?, ?)");
            if ($result === false){
                die("mysqli error: " . $this->conn->error);
            } else {
                $result->bind_param("sss", $username, $encrypted, $email);
                $result->execute();
                $result->close();
                $user["error"] = false;
                return $user;
            }
        }
    }

    public function getUser($email,$password) {
        $encrypted = md5($password);
        if ($this->checkExistence($email)) {
            $stmt = $this->conn->prepare("SELECT * FROM accounts WHERE email = ?");
            if ($stmt === false) {
                return array("error"=>true, "error_msg"=>"unknown error while fetching from database");
            } else {
                $stmt->bind_param("s", $email);
                $stmt->execute();
                $user = array();
                $stmt->bind_result($user["id"], $user["username"], $user["password"], $user["email"], $user["created_at"], $user["updated_at"]);
                if ($stmt->fetch()) {
                    $pass = $user["password"];
                    $user["error"] = false;
                    if ($pass == $encrypted) {
                        $stmt->close();
                        return $user;
                    } else {
                        $stmt->close();
                        return array("error" => true, "error_msg" => "password does not match the one on the database , $encrypted" . $user["username"]);
                    }
                } else {
                    return array("error"=>true, "error_msg"=>"unknown error while fetching");
                }
            }
        } else {
            return array("error"=>true, "error_msg"=>"user does not exists");
        }
    }

    public function checkExistence($email) {
        $stmt = $this->conn->prepare("SELECT * FROM accounts WHERE email = ?");
        if ($stmt === false) {
            die("Mysqli error ". $this->conn->error);
        } else {
            $stmt->bind_param("s", $email);
            $stmt->execute();
            $stmt->store_result();
            if($stmt->num_rows > 0) {
                $stmt->close();
                return true;
            } else {
                $stmt->close();
                return false;
        }
        }
    }
}
?>
  • 写回答

1条回答 默认 最新

  • douqin1932 2017-06-30 19:57
    关注

    I decided to move to firebase! It does this kinds of stuff for me and I am one hundred percent satisfied with it.

    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序