📄 httpmanage.cpp
字号:
#include "HTTPManage.h"
#include <aknnotewrappers.h>
#include <aknview.h>
#include "AssistantAppUi.h"
#define KHEADLEN 5
#define KENDLEN 6
_LIT8(KPostUri, "http://www.ldci.mobi/snsProject/update");
_LIT(KHEAD,"<Rss>");
_LIT(KEND,"</Rss>");
_LIT(KRECORD,"<record>");
_LIT(KRECORDEND,"</record>");
_LIT(KLNAME, "<lname>");
_LIT(KLNAMEEND, "</lname>");
_LIT(KFNAME, "<fname>");
_LIT(KFNAMEEND, "</fname>");
_LIT(KMOBILE, "<mob>");
_LIT(KMOBILEEND, "</mob>");
_LIT(KEMAIL, "<mail>");
_LIT(KEMAILEND, "</mail>");
_LIT(KADDRESS, "<addr>");
_LIT(KADDRESSEND, "</addr>");
_LIT(KCOMPANY, "<company>");
_LIT(KCOMPANYEND, "</company>");
// _LIT(CONTENT,"<rss><type>update</type><mobile>13800138001</mobile><record><lname>qin</lname><fname>jie</fname><mob>13800138000</mob><mail>tommy@126.com</mail><addr>beijing</addr><company>yuecheng</company></record></rss>");
// _LIT(KDefaultUrlText, "http://www.ldci.mobi/snsProject/register?mobile=13800138001&pass=123456");
// _LIT(KStatusFormat, "%d: %S"); // Format string for status returned from web server, e.g. "200: OK" or "404: Not Found"
// const TInt KStatusCodeLength = 10; // Enough room in a descriptor for code + ": "
CHTTPManage* CHTTPManage::NewL()
{
CHTTPManage* self = CHTTPManage::NewLC();
CleanupStack::Pop();
return self;
}
CHTTPManage* CHTTPManage::NewLC()
{
CHTTPManage* self = new(ELeave) CHTTPManage;
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
CHTTPManage::CHTTPManage()
{
m_pBookEngine = NULL;
m_pHTTPEngine = NULL;
m_pName = NULL;
m_pPassword = NULL;
m_pContent = NULL;
m_pRegister = NULL;
m_pLogin = NULL;
m_pAddress = NULL;
m_pStatus = NULL;
}
void CHTTPManage::ConstructL()
{
m_pStatus = HBufC::NewL(10);
m_pBookEngine = CBookEngine::NewL();
m_pHTTPEngine = CHTTPEngine::NewL( *this );
}
CHTTPManage::~CHTTPManage()
{
MEMORY_FREE(m_pBookEngine);
MEMORY_FREE(m_pHTTPEngine);
MEMORY_FREE(m_pContent);
MEMORY_FREE(m_pStatus);
MEMORY_FREE(m_pName);
MEMORY_FREE(m_pPassword);
MEMORY_FREE(m_pAddress);
}
void CHTTPManage::Update(RPointerArray<CBookInfo>& aArray)
{
m_pBookEngine->UpdatePhoneBook(1,aArray);
}
void CHTTPManage::Delete(TInt aItemId)
{
m_pBookEngine->DeleteUserFromBook(aItemId);
}
void CHTTPManage::GetBookInfo(RPointerArray<CBookInfo>& aArray)
{
m_pBookEngine->UpdatePhoneBook(0,aArray);
}
void CHTTPManage::Upload()
{
m_pStatus->Des().Copy(_L("upload"));
SpelLogAndReg();
m_pHTTPEngine->PostRequestL(m_pAddress->Des(),KPostUri);
}
void CHTTPManage::DownLoad()
{
m_pStatus->Des().Copy(_L("download"));
SpelLogAndReg();
m_pHTTPEngine->GetRequestL(m_pAddress->Des());
}
void CHTTPManage::Register(const TDesC& aName,const TDesC& aPsw)
{
m_pStatus->Des().Copy(_L("register"));
MEMORY_FREE(m_pName);
m_pName = HBufC::NewL(11);
m_pName->Des().Copy(aName);
MEMORY_FREE(m_pPassword);
m_pPassword = HBufC::NewL(11);
m_pPassword->Des().Copy(aPsw);
SpelLogAndReg();
m_pHTTPEngine->GetRequestL(m_pAddress->Des());
}
void CHTTPManage::Login(const TDesC& aName,const TDesC& aPsw)
{
m_pStatus->Des().Copy(_L("login"));
MEMORY_FREE(m_pName);
m_pName = HBufC::NewL(11);
m_pName->Des().Copy(aName);
MEMORY_FREE(m_pPassword);
m_pPassword = HBufC::NewL(11);
m_pPassword->Des().Copy(aPsw);
SpelLogAndReg();
m_pHTTPEngine->GetRequestL(m_pAddress->Des());
}
void CHTTPManage::ResponseStatusL(TInt aStatusCode, const TDesC& aStatusText)
{
}
void CHTTPManage::ResponseReceivedL(const TDesC& aResponseBuffer)
{
MEMORY_FREE(m_pContent);
// int nStart = 0;
// int nEnd = 0;
//
// nStart = aResponseBuffer.Find(KHEAD) + KHEADLEN;
// nEnd = aResponseBuffer.Find(KEND);
// m_pContent = HBufC::NewL(nEnd - nStart);
m_pContent = HBufC::NewL(aResponseBuffer.Length());
m_pContent->Des().Copy(aResponseBuffer);
if(!m_pStatus->Compare(_L("register")))
{
m_pRegister->Action(LogAndRegParse());
}
else if(!m_pStatus->Compare(_L("login")))
{
m_pLogin->Action(LogAndRegParse());
}
else if(!m_pStatus->Compare(_L("upload")))
{
m_pBook->Action(LogAndRegParse());
}
else
{
DownLoadParse();
m_pBook->Action(ETrue);
}
}
void CHTTPManage::SpelLogAndReg()
{
if(m_pStatus->Des().Compare(_L("upload")))
{
MEMORY_FREE(m_pAddress);
m_pAddress = HBufC::NewLC(100);
m_pAddress->Des().Copy(_L("http://www.ldci.mobi/snsProject/"));
m_pAddress->Des().Append(m_pStatus->Des());
m_pAddress->Des().Append(_L("?mobile="));
m_pAddress->Des().Append(m_pName->Des());
m_pAddress->Des().Append(_L("&pass="));
m_pAddress->Des().Append(m_pPassword->Des());
CleanupStack::Pop(m_pAddress);
}
else
{
RPointerArray<CBookInfo> bookArray;
CDesC16ArrayFlat* labelArray = new(ELeave) CDesC16ArrayFlat(10);
CleanupStack::PushL(labelArray);
labelArray->AppendL(_L("<rss><type>update</type><mobile>"));//0
labelArray->AppendL(_L("</mobile>"));//1
labelArray->AppendL(_L("<record><lname>"));//2
labelArray->AppendL(_L("</lname><fname>"));//3
labelArray->AppendL(_L("</fname><mob>"));//4
labelArray->AppendL(_L("</mob><mail>"));//5
labelArray->AppendL(_L("</mail><addr>"));//6
labelArray->AppendL(_L("</addr><company>"));//7
labelArray->AppendL(_L("</company></record>"));//8
labelArray->AppendL(_L("</rss>"));
MEMORY_FREE(m_pAddress);
m_pAddress = HBufC::NewL(512);
m_pAddress->Des().Copy((*labelArray)[0]);
m_pAddress->Des().Append(m_pName->Des());
m_pAddress->Des().Append((*labelArray)[1]);
m_pBookEngine->UpdatePhoneBook(0, bookArray);
for (TInt i=0; i<bookArray.Count(); i++)
{
if(bookArray[i]->GetFirstName()->Length() == 0 && bookArray[i]->GetLastName()->Length() == 0 && bookArray[i]->GetTelephone()->Length() == 0)
{
m_pAddress->Des().Append((*labelArray)[2]);
m_pAddress->Des().Append(bookArray[i]->GetLastName()->Des());
m_pAddress->Des().Append((*labelArray)[3]);
m_pAddress->Des().Append(bookArray[i]->GetFirstName()->Des());
m_pAddress->Des().Append((*labelArray)[4]);
m_pAddress->Des().Append(bookArray[i]->GetTelephone()->Des());
m_pAddress->Des().Append((*labelArray)[5]);
m_pAddress->Des().Append(bookArray[i]->GetEmail()->Des());
m_pAddress->Des().Append((*labelArray)[6]);
m_pAddress->Des().Append(bookArray[i]->GetAddress()->Des());
m_pAddress->Des().Append((*labelArray)[7]);
m_pAddress->Des().Append(bookArray[i]->GetCompany()->Des());
m_pAddress->Des().Append((*labelArray)[8]);
}
}
m_pAddress->Des().Append((*labelArray)[9]);
bookArray.ResetAndDestroy();
labelArray->Reset();
CleanupStack::PopAndDestroy(labelArray);
}
}
TBool CHTTPManage::LogAndRegParse()
{
HBufC* head = HBufC::NewLC(8);
HBufC* ender = HBufC::NewLC(9);
HBufC* news = HBufC::NewLC(1);
head->Des().Copy(_L("<result>"));
ender->Des().Copy(_L("</result>"));
TInt length = m_pContent->Des().Find(head->Des())+head->Des().Length();
TInt length1 = m_pContent->Des().Find(ender->Des());
if (m_pContent->Des().Find(head->Des()) != KErrNotFound)
{
news->Des().Copy(m_pContent->Des().Mid(length, length1-length));
}
if (!news->Des().Compare(_L("1")))
{
CleanupStack::PopAndDestroy(3);
return EFalse;
}
else
{
CleanupStack::PopAndDestroy(3);
return ETrue;
}
}
void CHTTPManage::DownLoadParse()
{
RPointerArray<CBookInfo> bookArray;
HBufC* result = NULL;
CBookInfo* bookInfo = NULL;
TInt nStart = 0;
TInt nEnd = 0;
while(m_pContent->Find(KRECORD) != KErrNotFound)
{
bookInfo = CBookInfo::NewL();
nStart = m_pContent->Find(KLNAME) + 7;
nEnd = m_pContent->Find(KLNAMEEND);
result = HBufC::NewLC(nEnd - nStart);
result->Des().Copy(m_pContent->Mid(nStart,nEnd-nStart));
bookInfo->SetLastName(result->Des());
CleanupStack::PopAndDestroy(result);
result = NULL;
nStart = m_pContent->Find(KFNAME) + 7;
nEnd = m_pContent->Find(KFNAMEEND);
result = HBufC::NewLC(nEnd - nStart);
result->Des().Copy(m_pContent->Mid(nStart,nEnd-nStart));
bookInfo->SetFirstName(result->Des());
CleanupStack::PopAndDestroy(result);
result = NULL;
nStart = m_pContent->Find(KMOBILE) + 5;
nEnd = m_pContent->Find(KMOBILEEND);
result = HBufC::NewLC(nEnd - nStart);
result->Des().Copy(m_pContent->Mid(nStart,nEnd-nStart));
bookInfo->SetTelephone(result->Des());
CleanupStack::PopAndDestroy(result);
result = NULL;
nStart = m_pContent->Find(KEMAIL) + 6;
nEnd = m_pContent->Find(KEMAILEND);
result = HBufC::NewLC(nEnd - nStart);
result->Des().Copy(m_pContent->Mid(nStart,nEnd-nStart));
bookInfo->SetEmail(result->Des());
CleanupStack::PopAndDestroy(result);
result = NULL;
nStart = m_pContent->Find(KADDRESS) + 6;
nEnd = m_pContent->Find(KADDRESSEND);
result = HBufC::NewLC(nEnd - nStart);
result->Des().Copy(m_pContent->Mid(nStart,nEnd-nStart));
bookInfo->SetAddress(result->Des());
CleanupStack::PopAndDestroy(result);
result = NULL;
nStart = m_pContent->Find(KCOMPANY) + 9;
nEnd = m_pContent->Find(KCOMPANYEND);
result = HBufC::NewLC(nEnd - nStart);
result->Des().Copy(m_pContent->Mid(nStart,nEnd-nStart));
bookInfo->SetCompany(result->Des());
CleanupStack::PopAndDestroy(result);
result = NULL;
bookInfo->SetItemId(-1);
bookArray.Append(bookInfo);
nStart = m_pContent->Find(KRECORDEND) + 9;
m_pContent->Des().Copy(m_pContent->Right(m_pContent->Length()-nStart));
}
m_pBookEngine->UpdatePhoneBook(1,bookArray);
}
void CHTTPManage::SetRegistePtr(MActive* aPtr)
{
m_pRegister = aPtr;
}
void CHTTPManage::SetLoginPtr(MActive* aPtr)
{
m_pLogin = aPtr;
}
void CHTTPManage::SetBookPtr(MActive* aPtr)
{
m_pBook = aPtr;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -