BREW RSS Reader - 4 / 9 -
HTTP Communication
SFXHTTPConnection Class
HTTP communication is implemented by using the SFXHTTPConnection class.
Declare SFXHTTPConnection Instance Variable
class SubscriptionWindow : public SFRTitleWindow {
private:
...
SFXHTTPConnection _http; // HTTP connection
SFXAnsiStringStreamReader _reader; // Stream for receiving data
SFXAnsiString _xml; // String for saving received data
...
// Callback for when HTTP connection begins
CALLBACK_DECLARE_SFXHTTPCONNECTION(ConnectCallback)
// Callback for when data is received over HTTP
CALLBACK_DECLARE_SFXBINARYSTREAMREADER(FetchCallback)
}
Connect HTTP
error = _http.Open(); // First call Open()
if (error == SFERR_NO_ERROR) {
error = _http.SetMethod("GET");
if (error == SFERR_NO_ERROR) {
// start to connect by calling Connect()
// 1st argument: URL string
// 2nd argument: Callback function on http connection
error = _http.Connect(url, CALLBACK_FUNCTION(ConnectCallback));
if (error == SFERR_NO_ERROR) {
// Set outer rectangle of dialog
rectangle.Set(SFXGraphics::GetDeviceRectangle());
rectangle.Deflate(10, 100);
// Display the "connecting..." dialog
connectingDialog = ::new SFRMessageDialog(rectangle,
"connecting...", "");
if (connectingDialog != null && static_try()) {
// The input key is not accepted
static_throw(
connectingDialog->RegisterHandler(SFEVT_KEY,
HANDLER_AFTER,
HANDLER_NULL));
}
}
}
}
ConnectCallback
ConnectCallback is a callback function that will be executed when connected.
When connected, it will get a stream for reading data (through the Fetch() function) and receiving data. When using Fetch()function, FetchCallback should be registered.
CALLBACK_IMPLEMENT_SFXHTTPCONNECTION(SubscriptionWindow, ConnectCallback, error)
{
if (error == SFERR_NO_ERROR) {
// Get stream for reading data
error = _http.GetStreamReader(1024, &_reader);
if (error == SFERR_NO_ERROR) {
// Receive data
error = _reader.Fetch(CALLBACK_FUNCTION(FetchCallback));
}
}
return;
}
FetchCallback
FetchCallback is a callback function that will be executed when data is received.
Received strings are read by ReadSFXAnsiString() through a stream. If there exists data yet to be received, FetchCallback should be registered by using Fetch().
CALLBACK_IMPLEMENT_SFXBINARYSTREAMREADER(SubscriptionWindow, FetchCallback, error)
{
SFRResponderPtr dialog;
SFXAnsiString string;
RSSFeedPtr feed = (*RssReader::GetRSSFeedList())[_displine + _cursor];
if (error == SFERR_NO_ERROR) {
// Read string
_reader.ReadSFXAnsiString(&string);
// Append string to the end of _xml
_xml += string;
// If receiving is over
if (_reader.Ends() && _reader.GetReadableSize() == 0) {
// close SFXHTTPConnection
_http.Close();
.
.
// Analyze _xml
.
.
// Get the front window
dialog = SFRApplication::GetInstance()->GetFront
(TYPE_WINDOW);
if (dialog != null) {
// Close window
dialog->Invoke(SFXEvent(SREVT_RESPONDER_TERMINATE,
SRP16_TERMINATE_INVOKE, true));
}
// Redraw
SFRApplication::GetInstance()->Invoke(
SFXEvent(SREVT_RESPONDER_RENDER,
SRP16_RENDER_INVOKE, true));
}
else { // If there still remains data to be received
_reader.Fetch(CALLBACK_FUNCTION(FetchCallback));
}
}
else {
DisplayErrorDialog();
}
return;
}







