作者:穿行印象
http://blog.sina.com.cn/s/blog_7a9486880101qa3s.html
获取到航班信息之后,如果有满意的航班就需要预定了。于是增加如下界面进行航班的预定:

点击“Book”按钮后会使用OData
SDK创建POST操作所需要的对象,然后向SMP暴露的OData服务发起POST操作:
public void Book(View v)
{
this.bookFlight();
}
private void bookFlight()
{
IODataEntry newBookingEntry = new ODataEntry();
newBookingEntry.putPropertyValue(ConnectivityConstants.AIRLINE_ID, mAirlineID);
newBookingEntry.putPropertyValue(ConnectivityConstants.CONNECTION_ID, mConnectionID);
newBookingEntry.putPropertyValue(ConnectivityConstants.FLIGHT_DATE, Utils.formatDate(mFlightDate, mApplication.useJSONFormat()));
newBookingEntry.putPropertyValue(ConnectivityConstants.CUSTOMER_ID, ConnectivityConstants.DEFAULT_CUSTOMER_ID);
newBookingEntry.putPropertyValue(ConnectivityConstants.AGENCY_ID, ConnectivityConstants.DEFAUL_AGENCY_ID);
newBookingEntry.putPropertyValue(ConnectivityConstants.BOOKING_DATE, Utils.formatDate(new Date(), mApplication.useJSONFormat()));
try
{
IRequest request = RequestBuilderHelper.getInstance().buildPOSTRequest(this, ConnectivityConstants.BOOKING_COLLECTION, newBookingEntry, BOOKING_REQUEST_TAG);
mApplication.getRequestManager().makeRequest(request);
}
catch(Exception e)
{
e.printStackTrace();
}
}
一旦调用成功,会触发onSuccess方法:
@Override
public void onSuccess(IRequest aRequest, IResponse aResponse) {
final IResponse response = aResponse;
mMessages = new ArrayList();
mCurrentRequestTag = aRequest.getRequestTAG();
if (aRequest.getRequestTAG().equalsIgnoreCase(BOOKING_REQUEST_TAG)) {
try {
HttpEntity responseEntity = response.getEntity();
String responseString = EntityUtils.toString(responseEntity);
Parser parser = mApplication.getParser();
mBookingEntries = parser.parseODataEntries(responseString, ConnectivityConstants.FLIGHT_COLLECTION, mApplication.getODataSchema());
showText();
} catch (Exception e) {
e.printStackTrace();
}
}
}
onSuccess该方法的最后会给出会调用showText()方法,给用户提示预定机票成功,如下图所示:

showText()方法代码:
private void showText()
{
Message msg = new Message();
mHandler.sendMessage(msg);
}
private Handler mHandler = new Handler()
{
public void handleMessage(Message msg)
{
if (mBookingEntries != null)
{
String bookingID = mBookingEntries.get(0).getPropertyValu(ConnectivityConstants.BOOKING_ID);
Toast.makeText(getApplicationContext(), "Book Flight Successfully, BookingID:" + bookingID, Toast.LENGTH_LONG).show();
}
};
};