@blog.justoneplanet.info

日々勉強

TizenアプリでHTTP通信する

サンプルアプリのHttpClientを見る。

■MainForm.cpp

MainFormはTizen::Net::Http::IHttpTransactionEventListenerを継承している。

result MainForm::RequestHttpGet(void)

エラー処理を省くと以下の通りとなる。

HttpTransaction* pHttpTransaction = null;
HttpRequest* pHttpRequest = null;

pHttpTransaction = __pHttpSession->OpenTransactionN();
r = pHttpTransaction->AddHttpTransactionListener(*this);

pHttpRequest = const_cast< HttpRequest* >(pHttpTransaction->GetRequest());
r = pHttpRequest->SetUri(HTTP_CLIENT_REQUEST_URI);
r = pHttpRequest->SetMethod(NET_HTTP_METHOD_GET);

r = pHttpTransaction->Submit();

AddHttpTransactionListenerの引き数はIHttpTransactionEventListenerを継承したクラスでサンプルアプリでは自身である。

void MainForm::OnTransactionReadyToRead(HttpSession& httpSession, HttpTransaction& httpTransaction, int availableBodyLen)

通信結果を受け取る。

HttpResponse* pHttpResponse = httpTransaction.GetResponse();
if (pHttpResponse->GetHttpStatusCode() == HTTP_STATUS_OK)
{
	HttpHeader* pHttpHeader = pHttpResponse->GetHeader();
	if (pHttpHeader != null)
	{
		String* tempHeaderString = pHttpHeader->GetRawHeaderN();
		ByteBuffer* pBuffer = pHttpResponse->ReadBodyN();

		String text(L"Read Body Length: ");
		text.Append(availableBodyLen);

		__pEditArea->SetText(text);
		Draw();

		delete tempHeaderString;
		delete pBuffer;
	}
}

IHttpTransactionEventListenerを継承した場合は以下のメソッドの実装が必要になるっぽい。

  • void MainForm::OnTransactionReadyToRead(HttpSession& httpSession, HttpTransaction& httpTransaction, int availableBodyLen)
  • void MainForm::OnTransactionAborted(HttpSession& httpSession, HttpTransaction& httpTransaction, result r)
  • void MainForm::OnTransactionReadyToWrite(HttpSession& httpSession, HttpTransaction& httpTransaction, int recommendedChunkSize)
  • void MainForm::OnTransactionHeaderCompleted(HttpSession& httpSession, HttpTransaction& httpTransaction, int headerLen, bool authRequired)
  • void MainForm::OnTransactionCompleted(HttpSession& httpSession, HttpTransaction& httpTransaction)
  • void MainForm::OnTransactionCertVerificationRequiredN(HttpSession& httpSession, HttpTransaction& httpTransaction, Tizen::Base::String* pCert)

コメントはまだありません»

No comments yet.

RSS feed for comments on this post.TrackBack URL

Leave a comment