簡體   English   中英

JSON請求始終返回null

[英]JSON Request always returns null

我正在嘗試使我的Android項目可與LAMP服務器一起使用,但JSON請求始終返回null。 LAMP服務器工作正常,我的要求是到達其上的php文件。

這是我的代碼:

class GetEntryData extends AsyncTask<String, String, String> {

private static JSONParser jsonParser;
private List<NameValuePair> entry_data;

private static String server_address;
private static String server_db;
private static String db_table;
private static int id;
public static String TAG = "LogDebug";

private static final String TAG_SUCCESS = "success";

public  GetEntryData(String server_address, String server_db, String db_table, int id) {
    this.server_address = server_address;
    this.server_db = server_db;
    this.db_table = db_table;
    this.id = id;
}

@Override
protected String doInBackground(String[] params) {

    Log.d(TAG, "Sending JSON request");
    jsonParser = new JSONParser();
    int success;
        entry_data = new ArrayList<NameValuePair>();
        entry_data.add(new BasicNameValuePair("id", String.valueOf(id)));
        Log.d(TAG, "server address: " + server_address + "   enrty data: " + entry_data.toString());
        JSONObject json = jsonParser.makeHttpRequest(server_address, "GET", entry_data);
        try {
        Log.d(TAG, json.toString());

        success = json.getInt(TAG_SUCCESS);
        if (success == 1) {
            JSONArray productObj = json.getJSONArray(db_table);
            JSONObject entry = productObj.getJSONObject(0);
            return entry.toString();
        } else {
            return null;
        }
    } catch (NullPointerException e) {
        return e.getMessage();
    }catch (JSONException e){
            return e.getMessage();
        }
}
@Override
protected void onPostExecute(String result) {
    Log.d(TAG, "Entry data was written with the result: " + result);
}

}

我的JSON類:

public class JSONParser {

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
public JSONParser() {

}
public JSONObject makeHttpRequest(String url, String method, List<NameValuePair> params) {
    try {
        if(method == "POST"){
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        }else if(method == "GET"){
            DefaultHttpClient httpClient = new DefaultHttpClient();
            String paramString = URLEncodedUtils.format(params, "utf-8");
            url += "?" + paramString;
            HttpGet httpGet = new HttpGet(url);

            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
        }

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }
    return jObj;

}}

和我的PHP代碼:

<?php

$response = array();

require 'db_connect.php';

$db = new DB_CONNECT();

if (isset($_GET["id"])) {
$pid = $_GET['id'];

$result = mysqli_query("SELECT *FROM marker_data WHERE id = $id");

if (!empty($result)) {
    if (mysqli_num_rows($result) > 0) {

        $result = mysqli_fetch_array($result);

        $entry = array();
        $entry["id"] = $result["id"];
        $entry["title"] = $result["title"];
        $entry["address"] = $result["address"];
        $entry["image"] = $result["image"];
        $entry["working_hours"] = $result["working_hours"];
        $entry["product_range"] = $result["product_range"];
        $entry["comments"] = $result["comments"];
        $entry["confirmation_status"] = $result["confirmation_status"];
        $entry["latitude"] = $result["product_range"];
        $entry["longitude"] = $result["longitude"];


        $response["entry"] = array();

        array_push($response["entry"], $entry);

        echo json_encode($response);
    } else {
        $response["success"] = 0;
        $response["message"] = "No entry found";

        echo json_encode($response);
    }
} else {
    $response["success"] = 0;
    $response["message"] = "No entry found";

    echo json_encode($response);
}
} else {
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";

echo json_encode($response);
}
?>

db_connect.php:

<?php

class DB_CONNECT {

function __construct() {
    $this->connect();
}

function __destruct() {
    $this->close();
}

function connect() {
    require 'db_config.php';

    $con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysqli_error());

    $db = mysqli_select_db(DB_DATABASE) or die(mysqli_error()) or die(mysqli_error());

    return $con;
}

function close() {
    mysqli_close();
}

}

?>

我在Logcat中得到的是:解析數據org.json.JSONException時出錯:輸入數據輸入字符0的結尾,結果是:嘗試調用虛擬方法'java.lang.String org.json.JSONObject.toString( )'在空對象引用上

有一個id = 1的條目(我正在嘗試獲取),並且所有變量名(例如服務器,數據庫,php文件等)均已正確設置。

我的建議是也使用PDO

所以db_connect,也許是這樣的:

<?php

try {
    $conn = new PDO("mysql:host=localhost;dbname=test;", 'root', 'test');
} catch (PDOException $e) {
    die("Error occurred: " . $e->getMessage());
}

$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);

header('Content-Type: text/html; charset=utf-8');

?>

getMarkerData.php

<?php

require 'DBConnect.php'; //PHP file for connect to database
header('Content-Type: application/json'); //You want answers in json 
$header = apache_request_headers(); //set headers

if (isset($header['authorization'])) { //if ID is posted than do stuffs

    $id = $header['authorization']; //save ID in some variable

    //You can put user id in marker_data table and then get values from there, but for easier testing you can firstly check if user ID is found in users table and if is not than just answer in json user not found
    $stmt = $conn->prepare("SELECT id FROM users WHERE id = :id");
    $stmt->bindParam(":id", $id);
    $stmt->execute();
    $row = $stmt->fetch(PDO::FETCH_ASSOC);

    if ($row < 1) {
        $error['success'] = 0; //Success is for easier handling on Android to know what to do next
        $error['message'] = 'Access denied!';

        die(json_encode($error));
    } else {
       //so user is found and we now want data for that user
        $sql = $conn->prepare("SELECT * FROM marker_data WHERE userid = :uid);
        $sql->bindParam(":uid", $id);
        $sql->execute();
        $data = $sql->fetch(PDO::FETCH_ASSOC);

        if ($data > 0) {
            //So here you choose which data you want to fetch
            $error['success'] = 1;
            $error['product_range'] = $data['product_range'];
            $error['working_hours'] = $data['working_hours'];

            echo json_encode($error);
        }

    }

} else {
    $error["success"] = 0;
    $error["message"] = "Access denied!";

    die(json_encode($error));
}

?>

對於android和JSON示例

public class CheckingDatabase extends AsyncTask<String, Integer, Integer> {
        //Add your ID for identification
        public CheckingDatabase(String postID) {
            postID = id;
        }

        @Override
        protected Integer doInBackground(String... params) {

            try {
                //Objects of HttpClient and HttpPost
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost("http://localhost/PHPScripts/getMarkerData.php");
                HttpParams httpParams = new BasicHttpParams();
                int timeoutConnection = 5000;
                HttpConnectionParams.setConnectionTimeout(httpParams, timeoutConnection);
                int timeoutSocket = 7000;
                HttpConnectionParams.setSoTimeout(httpParams, timeoutSocket);
                httpPost.setHeader("authorization", id); //ID

                //Post request
                try {
                    HttpResponse httpResponse = httpClient.execute(httpPost);
                    HttpEntity httpEntity = httpResponse.getEntity();
                    InputStream inputStream = null;
                    inputStream = httpEntity.getContent();
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
                    StringBuilder stringBuilder = new StringBuilder();

                    String line = null;
                    while ((line = bufferedReader.readLine()) != null) {
                        stringBuilder.append(line + "\n");
                    }
                    String result = null;
                    result = stringBuilder.toString();
                    json = result;
                } catch (ClientProtocolException cpe) {
                    cpe.printStackTrace();
                    Log.d("URIException", cpe.getMessage());
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                    Log.d("IOException", ioe.getMessage());
                }

                Thread.sleep(1000);
            } catch (InterruptedException ie) {
                ie.printStackTrace();
                Log.d("InterruptedException", ie.getMessage());
            }

            //JSON response
            try {
                JSONObject jsonObject = new JSONObject(json);
                success = jsonObject.getInt("success");

                if (success == 1) { //If success is 1 get data
                    product_range = jsonObject.getString("product_range");
                    working_hours = jsonObject.getString("working_hours");
                }
            } catch (JSONException je) { //Otherveis exception
                je.printStackTrace();
                Log.d("JSONException", je.getMessage());
            }

            return success;
        }

        @Override
        protected void onPostExecute(Integer integer) {
            integer = success;

            if (integer == 1) {
                checkForChanges(); //Now you can call some another method to do some other things with data or something else, or even you don't need this :D
            }
        }
    }

也許只是建議將另一個隨機生成的ID用於用戶標識。 此外,如果要測試php腳本是否真的可以,可以使用ARC(Chrome擴展名,可以進行測試)。

您需要對剩下的有關sql注入的注釋進行操作。 使用PDO。

沒有足夠的信息可以完全為您提供幫助。 錯誤的行號會有所幫助,甚至會產生完整的logcat輸出。

您可以嘗試從PHP返回一些有效的json。 這樣可以避免錯誤,並且您可以確定PHP代碼將返回您期望的結果。 一旦感到滿意,您就可以將json放回所需的位置-也許自己格式化它,而不要依賴於關聯數組的json_encode()。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM