我有一個網站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;
}
}
}
}
?>
您的堆棧跟蹤根本不包含錯誤! – moritzg
而不是使用樣板代碼的網絡操作,我建議你去健壯的庫,如[OkHttp](https://github.com/square/okhttp/wiki/Recipes)或[排球](https://開發人員.android.com /培訓/抽射/ index.html的)。 –
使用php原生輸入讀取器讀取您的輸入,如'$ postdata = file_get_contents('php:// input');' – Sundar