获取到航班信息之后,如果有满意的航班就需要预定了。于是增加如下界面进行航班的预定:
<img src="/oldimg/50/https://www.ut163.com/wp-content/uploads/2019/08/20190827124503-12.jpg" alt="SMP3.0学习笔记之五使用ODataNativeSDK修改数据” />
点击“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()方法,给用户提示预定机票成功,如下图所示:
<img src="/oldimg/50/https://www.ut163.com/wp-content/uploads/2019/08/20190827124503-60.jpg" alt="SMP3.0学习笔记之五使用ODataNativeSDK修改数据” />
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();
}
};
};