// 该文件返回双方的聊天记录
require 'Class_DBOperation.php';
require 'globle.php';
//联系人对象,属性与客户端一致
class Contact
{
public $sender;
public $receiver;
public $senddate;
public $sendcontent;
function Contact($sender,$receiver,$senddate,$sendcontent)
{
$this->sender = $sender;
$this->receiver = $receiver;
$this->senddate = $senddate;
$this->sendcontent =$sendcontent;
}
}
接受客户端的字段值,来查询表
$senderVar = $_POST['SENDER'];
$receiverVar = $_POST['RECEIVER'];
$contentVar = $_POST['SENDCONTENT'];;
$dbOperation = new class_DBOperation(DBHOST,DBUSER,DBPWD,DBNAME,DBCHARSET);
//
$sql = "select * from chat where (SENDER ='$senderVar' and RECEIVER='$receiverVar') or (SENDER='$receiverVar' and RECEIVER='$senderVar') order by SENDDATE";
$result = $dbOperation->query($sql);
// $a = json_encode(array("aa"=>"22222"));
// echo $a;
$contacts = array();
while($contact = mysql_fetch_assoc($result))
{
//$contact = new Contact($row->SENDER,$row-RECEIVER,$row->SENDDATE,$row->SENDCONTENT);
$contacts[] = array("contact"=>$contact);
}
echo json_encode($contacts);
$dbOperation->closeConnect();
?>
客户端接收:
NSLog(@"xxx:%@",responseString);
NSData *jsonData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:nil];
NSLog(@"count:%d",[dictionary count]);
//
NSArray *rounds = [responseString JSONValue];
NSLog(@"pppp:%d",[rounds count]);
//
for(NSDictionary *item in rounds)
{
NSDictionary * info = [item objectForKey:@"contact"];
//获取字段的值
NSString *sender = [info objectForKey:@"SENDER"];
NSString *sendedate = [info objectForKey:@"SENDDATE"];
NSString *sendcontent = [info objectForKey:@"SENDCONTENT"];
//获取消息内容
ChatInfo *chatInfo = [[ChatInfo alloc] init];
chatInfo.sender = sender;
chatInfo.senddate = sendedate;
chatInfo.sendcontent = sendcontent;
[self.chatInfos addObject:chatInfo];
[chatInfo release];
}
另一个例子:
服务器端:
<?php
require 'Class_DBOperation.php';
require 'globle.php';
// 检查某一用户是否为有我用户
$dbOperation = new class_DBOperation(DBHOST,DBUSER,DBPWD,DBNAME,DBCHARSET);
//Ω” ’øÕªß∂À≤Œ ˝
$user_pwd = $_POST['pwd'];//$_POST['pwd'];
$sqlCheckName = "select password from user ";
$checkResult = $dbOperation->query($sqlCheckName);
$yoWoContacts = array();
while($contact = mysql_fetch_assoc($checkResult))
{
//$contact = new Contact($row->SENDER,$row-RECEIVER,$row->SENDDATE,$row->SENDCONTENT);
$yoWoContacts[] = $contact;
}
echo json_encode($yoWoContacts);
?>
客户端:
-(void)requestFinished:(ASIHTTPRequest *)request
{
NSString *responseString = [request responseString];
NSArray *contacts = [responseString JSONValue];
//在这里检测谁是有我联系人
for(NSDictionary *youWoContact in contacts )
{
NSString *password = [youWoContact objectForKey:@"password"];
for(Contact *contact in [AppConfigure sharedConfigure].allContactsOfUser)
{
if([contact.contactTeleNum isEqualToString:password])
{
contact.contactType = kYOU_WO_CONTACT;
[[AppConfigure sharedConfigure].youWoContactsOfUser addObject:contact];
}
}
}
}