2017-06-23 121 views
0

我有一个网站register.php(在本地主机上运行)。 网站中的代码(AJAX jQuery)返回一个JSON数组,现在都很好。 当我需要数组时,Android中的代码返回null。Android JSON请求,输入流返回null

代码:

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(); 

栈跟踪:

5月6日至23日: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 部分代码缓存集合,代码= 28KB,数据= 30KB 06- 23 05:46:20.048 22500-22506/com.newworldgrip.lostandfound.lostandfound I/zygote:经过 代码缓存集合,代码= 28KB,data = 30KB 06-23 05:46:20.048 22500-22506/com.newworldgrip.lostandfound.lostandfound I/zygote: 将代码缓存容量增加到128KB 06-23 05:46:20.715 22500-22506/com.newworldgrip.lostandfound.lostandfound I/zygote:做 部分代码缓存集合,代码= 61KB,数据= 53KB 06- 23 05:46:20.716 22500-22506/com.newworldgrip.lostandfound.lostandfound I/zygote:经过 代码缓存收集,代码= 61KB,数据= 53KB 06-23 05:46:20.716 22500-22506/com。 newworldgrip.lostandfound.lostandfound I/zygote: 将代码缓存容量增加到256KB 06-23 05:46:22.452 22500-22506/com.newworldgrip.lostandfound.lostandfound I/zygote:执行 完整代码缓存集合,代码= 123KB,data = 100KB 06-23 05:46:22.452 22500-22506/com.newworldgrip.lostandfound.lostandfound I/zygote:经过 代码缓存集合,代码= 116KB,数据= 73KB 06-23 05:46:23.648 22500-22506/com.newworldgrip.lostandfound.lostandfound I/zygote:做 部分代码缓存集合,代码= 122KB,data = 84KB 06-23 05:46:23.648 22500-22506/com.newworldgrip.lostandfound.lostandfound I/zygote:代码缓存收集后,代码= 122KB,数据= 84KB 06-23 05:46: 23.648 22500-22506/com.newworldgrip.lostandfound.lostandfound I/zygote:将代码缓存容量增加到512KB 06-23 05:46:29.721 22500-22537/com.newworldgrip.lostandfound.lostandfound D/NetworkSecurityConfig:未指定网络安全配置,使用 平台默认值06-23 05:46:29.737 22500-22506/com.newworldgrip.lostandfound.lostandfound I/zygote:代码= 208KB,data = 162KB 06-23 05:46:29.737 22500-22506/com.newworldgrip.lostandfound.lostandfound I/zygote: 代码缓存集合,代码= 205KB,data = 134KB 5月6日至23日:46:29.742 22500-22537/com.newworldgrip.lostandfound.lostandfound W /的OnStart: doInBackground:200 5月6日至23日:46:29.763 22500-22536/com.newworldgrip.lostandfound.lostandfound D/EGL_emulation:eglMakeCurrent:0xa2d79580:ver 2 0(tinfo 0xaed0b350)5月6日至23日:46:29.784 22500-22536/com.newworldgrip.lostandfound.lostandfound d/EGL_emulation:eglMakeCurrent:0xa2d79580:第二版0(TINFO 0xaed0b350)5月6日至23日: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。I/zygote:代码缓存收集之后,代码缓存集合,代码= 245KB,数据= 173KB 06-23 05:47:03.396 22500-22506/com.newworldgrip.lostandfound.lostandfound I/zygote: = 245KB,数据= 173KB 06-23 05:47:03.396 22500-22506/com.newworldgrip.lostandfound.lostandfound I /合子:增加代码高速缓存容量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; 
     } 
     } 
    } 
} 
?> 
+0

您的堆栈跟踪根本不包含错误! – moritzg

+0

而不是使用样板代码的网络操作,我建议你去健壮的库,如[OkHttp](https://github.com/square/okhttp/wiki/Recipes)或[排球](https://开发人员.android.com /培训/抽射/ index.html的)。 –

+0

使用php原生输入读取器读取您的输入,如'$ postdata = file_get_contents('php:// input');' – Sundar

回答

0

我决定搬到firebase! 它为我做这种东西,我百分之百满意它。