1

我有一个问题,我的 json 对象数组没有传递值。我知道它们在那里,但我无法让它们传递到我的 android 应用程序中的变量中。

The Java function to handle the JSONObject:/**
     * Function to get all userlogs
     **/
    public JSONObject getUserLogs(String userid){
     // userlogs JSONArray
       List<NameValuePair> params = new ArrayList<NameValuePair>();
       params.add(new BasicNameValuePair("tag", getUserLogs_tag ));
       params.add(new BasicNameValuePair("userid", userid));
       JSONObject json = jsonParser.getJSONFromUrl(getAllUserLogs,params);

       return json;
   }

这是后台的 Java 操作:

protected String doInBackground(String... args) {
            //List<NameValuePair> params = new ArrayList<NameValuePair>();
            DatabaseHandler db = new DatabaseHandler(getApplicationContext());  

            HashMap<String,String> user = new HashMap<String, String>();
            user = db.getUserDetails();
            userid = user.get("uid");        

            UserFunctions userFunction = new UserFunctions();
            JSONObject json = userFunction.getUserLogs(userid);
            Log.d("All userlogs: ", json.toString());
           // return json;
            try {
                if (json.getString(KEY_SUCCESS) != null) {
                    userlogs = json.getJSONArray(KEY_USERLOGS);
                    // looping through All Products
                    for (int i = 0; i < userlogs.length(); i++) {
                        JSONObject c = userlogs.getJSONObject(i);

                        // Storing each json item in variable
                        String userID = c.getString(KEY_USERID);
                        String medname = c.getString(KEY_MEDNAME);
                        String medtaken = c.getString(KEY_MEDTAKEN);
                        String tabsleft = c.getString(KEY_TABSLEFT);

                        // creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        map.put(KEY_USERID, userid);
                        map.put(KEY_MEDNAME, medname);
                        map.put(KEY_MEDTAKEN, medtaken);
                        map.put(KEY_TABSLEFT, tabsleft);

                        // adding HashList to ArrayList
                        logList.add(map);
                    }
                 }

                }
                catch (JSONException e) {
                    e.printStackTrace();

                }
            return null; 
        }

结果如下:

03-20 04:47:01.212: E/JSON(7034): {"tag":"getUserLogs","success":1,"error":0,"userlogs":[{"userlogs":{"userid":"3705","medname":"atorvastatin 20mg","medtaken":"2014-03-20 03:48:49","tabsleft":"30"}},{"userlogs":{"userid":"3705","medname":"atorvastatin 20mg","medtaken":"2014-03-20 03:55:54","tabsleft":"30"}}]}
03-20 04:47:01.212: D/All userlogs:(7034): {"error":0,"success":1,"userlogs":[{"userlogs":{"userid":"3705","medtaken":"2014-03-20 03:48:49","tabsleft":"30","medname":"atorvastatin 20mg"}},{"userlogs":{"userid":"3705","medtaken":"2014-03-20 03:55:54","tabsleft":"30","medname":"atorvastatin 20mg"}}],"tag":"getUserLogs"}
03-20 04:47:01.212: W/System.err(7034): org.json.JSONException: No value for userid
03-20 04:47:01.212: W/System.err(7034):     at org.json.JSONObject.get(JSONObject.java:355)
03-20 04:47:01.212: W/System.err(7034):     at org.json.JSONObject.getString(JSONObject.java:515)
03-20 04:47:01.212: W/System.err(7034):     at com.jipe.medicationstation.UserlogListActivity$LoadAllLogs.doInBackground(UserlogListActivity.java:104)
03-20 04:47:01.212: W/System.err(7034):     at com.jipe.medicationstation.UserlogListActivity$LoadAllLogs.doInBackground(UserlogListActivity.java:1)
03-20 04:47:01.212: W/System.err(7034):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
03-20 04:47:01.212: W/System.err(7034):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
03-20 04:47:01.212: W/System.err(7034):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
03-20 04:47:01.212: W/System.err(7034):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
03-20 04:47:01.212: W/System.err(7034):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
03-20 04:47:01.212: W/System.err(7034):     at java.lang.Thread.run(Thread.java:841)

PHP:

else if ($tag == 'getUserLogs') {
       $userid = $_POST['userid'];
       //Special case (normally conntections handled in functions/config or connect)
       $link = new mysqli($host, getDBuser(), getDBpassword(), getDBname());
        /* check connection */
        if (mysqli_connect_errno()) {
            $response["error"] = 1;
            $response["error_msg"] = mysqli_connect_error();
            echo json_encode($response);
        }
        $query = "SELECT * FROM userlog WHERE userid = '$userid'";
        $result = mysqli_query($link, $query);
        $row_cnt = mysqli_num_rows($result);
        //If results, then
        if( $row_cnt > 0){
            $response["userlogs"] = array();
            while($row = mysqli_fetch_array($result, MYSQLI_BOTH)){
                $userlog = array();
                $userlog["userlogs"]["userid"] = $row["userid"];
                $userlog["userlogs"]["medname"] = $row["medname"];
                $userlog["userlogs"]["medtaken"] = $row["medtaken"];
                $userlog["userlogs"]["tabsleft"] = $row["tabsleft"];
                array_push($response["userlogs"], $userlog);
            }
            $response["success"] = 1;
            echo json_encode($response);
            mysqli_close($link);
        }
        else {
            // userlogs not found
            // echo json with error = 1
            $response["error"] = 1;
            $response["error_msg"] = "No Userlogs found";
            echo json_encode($response);
        }
    }
    else {
        $response["error"] = 3;
        $response["error_msg"] = "JSON ERROR";
        echo json_encode($response);
    }
} 

有什么建议么?

4

1 回答 1

2

你有

"userlogs": [ // jsonarray userlogs
    {         // json object node
        "userlogs": {   // json object userlogs

然后

userlogs = json.getJSONArray(KEY_USERLOGS); // fine 
for (int i = 0; i < userlogs.length(); i++) {
JSONObject c = userlogs.getJSONObject(i);

userlogs但是你也有一个 JSONObject

JSONObject userlogsobject = c.getJSONObject("userlogs");
String userID = userlogsobject.getString(KEY_USERID);
于 2014-03-20T08:55:37.457 回答