gpt4 book ai didi

PHP JSON 和 Android

转载 作者:行者123 更新时间:2023-11-30 04:12:23 25 4
gpt4 key购买 nike

现在在我的类里面,我们正在为我的学校开发一个 Android 应用程序。这个类没有实际的开会时间,所以我类的一些人我还没有面对面交谈过。其中一个人一直在为不同的提要编写 json 和 php 程序。问题是他给我发了一个 php 文件,我不完全确定如何解析其中的信息以便能够在应用程序上显示提要。

我相当精通 Java 和 XML,不太精通 PHP(我现在正在学习)或 JSON(大约一年前使用过一次)

所以我有3个问题

1.我的应用发送页面需要什么才能访问不同的提要?

2.数据库应该放在哪里?

3.我应该如何解析从该页面发送给我的信息。

仅供引用,该应用程序目前只是一个基本设计,因此没有类(class)或任何需要担心的事情。

这是PHP文件

<?php

$server = "localhost";
$user_name = "web";
$password = "internet";
$db_name = "cptc";

@$db = mysqli_connect($server, $user_name, $password, $db_name);

if (mysqli_connect_errno()) {
echo 'Error: could not connect to database. Please try again later.';
exit ;
}

mysqli_select_db($db, $db_name) or die("could not connect");

$commandType = 'unknown';
$feedURL="";
if (isset($_GET['cmd'])) {
$commandType = trim($_GET['cmd']);
$commandType = strtolower($commandType);
}
if ($commandType == 'staff') {

if (isset($_GET['fname']) && isset($_GET['lname'])) {

$fname = trim($_GET['fname']);
$fname = strtolower($fname);
$lname = trim($_GET['lname']);
$lname = strtolower($lname);

$query = "SELECT * FROM staff WHERE fname like '" . $fname . "%' AND '" . $lname . "%'";

} else if (isset($_GET['lname'])) {
$commandType = trim($_GET['cmd']);
$commandType = strtolower($commandType);
$lname = trim($_GET['lname']);
$lname = strtolower($lname);
$query = "SELECT * FROM staff WHERE lname like '" . $lname . "%'";

} else if (isset($_GET['fname'])) {
$commandType = trim($_GET['cmd']);
$commandType = strtolower($commandType);
$lname = trim($_GET['fname']);
$lname = strtolower($fname);
$query = "SELECT * FROM staff WHERE lname like '" . $fname . "%'";
} else {
$commandType = trim($_GET['cmd']);
$commandType = strtolower($commandType);
$query = "SELECT * FROM " . $commandType;
}

$result = mysqli_query($db, $query);

if (mysqli_num_rows($result)) {

echo '{"Data":[';

$first = true;
$row = mysqli_fetch_assoc($result);

while ($row = mysqli_fetch_row($result)) {

if ($first) {
$first = false;
} else {
echo ',';
}
echo json_encode($row);
}
echo ']}';
} else {
echo '[]';
}
} else if ($commandType == 'event') {
if (isset($_GET['current']) && isset($_GET['future'])) {
$current = $_GET['current'];
$current = mktime(0, 0, 0, date("m"), date("d"), date("Y"));
$currentDate = date("Y-m-d", $current);

$future = $_GET['future'];
$future = mktime(0, 0, 0, date("m"), date("d") + 7, date("Y"));
$futureDate = date("Y-m-d", $future);
$userid = 'michael.herrera1977%40gmail.com';
$magicWord = '200f9cbc38a87ae4f65051f26d19e5a4';
$feedURL = "http://www.google.com/calendar/feeds/$userid/private-$magicWord/basic?start-min=$currentDate&start-max=$futureDate";

} else if (isset($_GET['current'])) {
$current = $_GET['current'];
$current = mktime(0, 0, 0, date("m"), date("d"), date("Y"));
$currentDate = date("Y-m-d", $current);
$userid = 'michael.herrera1977%40gmail.com';
$magicWord = '200f9cbc38a87ae4f65051f26d19e5a4';
$feedURL = "http://www.google.com/calendar/feeds/$userid/private-$magicWord/basic?start-min=$currentDate";

} else if (isset($_GET['future'])) {
$future = $_GET['future'];
$future = mktime(0, 0, 0, date("m"), date("d") + 7, date("Y"));
$futureDate = date("Y-m-d", $future);
$userid = 'michael.herrera1977%40gmail.com';
$magicWord = '200f9cbc38a87ae4f65051f26d19e5a4';
$feedURL = "http://www.google.com/calendar/feeds/$userid/private-$magicWord/basic?start-max=$futureDate";
}

// read feed into SimpleXML object
$xmlSimple = simplexml_load_file($feedURL);

//gets info based on CMD this will be pulled up if CMD = events
//if(isset($_GET['events']))

//put each title and content from each date into the array
foreach ($xmlSimple->entry as $entry) {
$title = stripslashes($entry -> title);
$content = stripslashes($entry -> content);
$eventsArray[] = array('Title' => $title, 'content' => $content);

}
//clean up json string for easy cheesy use and to get the when, where and event seperated
$json = json_encode($eventsArray);
$json = str_replace('"content":', '', $json);
$json = str_replace('\n', '', $json);
$json = str_replace('"When:', '"When":"', $json);
$json = str_replace('Where:', '","Where":"', $json);
$json = str_replace('Event Status: confirmed', '', $json);
$json = str_replace('Event Description:', '","Event Description":"', $json);
$json = preg_replace('/\s+/', '%20', $json);

echo $json;
}
?>

其中还包含一个 html 文件,

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>dbphp</title>
<meta name="author" content="Michael" />
<!-- Date: 2012-05-03 -->
</head>
<body>
<form action="dbphp.php" method="get" >
<!--<input type="hidden" name="cmd" value="staff">
<input type="hidden" name="fname" value="m">
<input type="hidden" name="lname" value="j"/><br />-->
<input type="hidden" name="cmd" value="event">
<input type="hidden" name="current" value="5/10/2012">
<input type="hidden" name="future" value="5/15/2012"/>
<input type="submit" name="submit" value="enter staff member" />

</form>

</body>
</html>

我真的希望我能和作者见面,但我似乎不想锻炼。

如能提供任何帮助,我们将不胜感激。谢谢

最佳答案

从表面上看,这是一个仅限 PHP 的问题。 API 似乎使用 ? PHP 文件末尾的参数,所以我们有:

  1. dbphp.php?cmd=staff&lname=<last_name>&fname=<first_name>
    • staff 中查找用户表。
  2. dbphp.php?cmd=<table_name>
    • table_name 中取出所有东西 table 。哎呀!
  3. dbphp.php?cmd=event?current=<date>&future=<date>
    • 查找 michael.herrera1977 的 Google 日历。 json 返回值在返回之前被严重破坏

数据库与 PHP 位于同一台服务器上,localhost .要解析输出,有标准的 JSONArrayJSONObject类(class)来帮助你。我还会删除所有 str_replacepreg_replace代码,因为它们可能会使 Google 的 JSON 输出无效。

关于PHP JSON 和 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10647537/

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