gpt4 book ai didi

php - JsonException : No value for userid 异常

转载 作者:行者123 更新时间:2023-11-29 21:11:49 25 4
gpt4 key购买 nike

我遇到一个问题,我的 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);
}
}

有什么建议吗?

最佳答案

你有

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

但是你还有一个 JSONObject userlogs

JSONObject userlogsobject = c.getJSONObject("userlogs");
String userID = userlogsobject.getString(KEY_USERID);

关于php - JsonException : No value for userid 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22527774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com