首页 > SAP > HANA > SMP3.0学习笔记之四 使用OData Native SDK获取数据
2019
08-27

SMP3.0学习笔记之四 使用OData Native SDK获取数据

登录(onboarding)成功后的界面中,我增加了三个按钮:

Unregister: 注销登录

GetSerDoc/Sechema: 在OData Native
SDK能够对OData服务返回的结果进行解析之前,我们首先要获取的是OData接口的Service
Document以及Metadata,点击次按钮我使用OData SDK获取这两个数据, 并保存在全局变量里。

GetAirLines:
获取航空公司信息,点击每个航空公司,可以获取每个航空公司的航班信息。

界面如下图所示:

<img src="/oldimg/50/https://www.ut163.com/wp-content/uploads/2019/08/20190827124558-8.jpg" alt="SMP3.0学习笔记之四使用ODataNativeSDK获取数据” />

当点击“GetSerDoc/Schema”按钮,会触发GetSerDocAndSchema函数:

public void GetSerDocAndSchema(View view)

{

getServiceDocument();

}

private void getServiceDocument() {

//Initializes the RequestBuilder

this.initializeRequestBuilder();

if (mServiceDocument == null) {

//opens a progress dialog window

mRequestTag = REQUEST_SERVICE_DOCUMENT;

//Creates the Request object

IRequest request =
RequestBuilderHelper.getInstance().buildServiceDocumentRequest(this,
REQUEST_SERVICE_DOCUMENT);

//Sends the request

((SDKApplication)getApplication()).getRequestManager().makeRequest(request);

} else {

this.getServiceSchema();

}

}

当获取service
document成功后,在onSuccess回调函数中会调用getServiceSchema()方法:

public void onSuccess(IRequest
_Request, IResponse _Response) {

// TODO Auto-generated method stub

try

{

HttpEntity responseEntity = _Response.getEntity();

String responseString =
EntityUtils.toString(responseEntity);

Parser parser = ((SDKApplication)getApplication()).getParser();

if
(_Request.getRequestTAG().contentEquals(REQUEST_SERVICE_DOCUMENT))
{

mServiceDocument =
parser.parseODataServiceDocument(responseString);

showText(ConnectivityConstants.GET_SERVICE_DOC);

getServiceSchema();

}

else if
(_Request.getRequestTAG().contentEquals(REQUEST_METADATA))

{

IODataSchema schema =
parser.parseODataSchema(responseString, mServiceDocument);

((SDKApplication)getApplication()).setODataSchema(schema);

RequestBuilderHelper.getInstance().setSchema(schema);

showText(ConnectivityConstants.GET_META_DOC);

}

}

catch(Exception e)

{

e.printStackTrace();

}

}

而getServiceSchema方法如下:

private void getServiceSchema() {

IODataSchema schema =
((SDKApplication)getApplication()).getODataSchema();

if (schema == null) {

mRequestTag = REQUEST_METADATA;

//Creates the request object

IRequest request =
RequestBuilderHelper.getInstance().buildMetaDataRequest(this,
REQUEST_METADATA);

//Sends the request

((SDKApplication)getApplication()).getRequestManager().makeRequest(request);

} else {

RequestBuilderHelper.getInstance().setSchema(schema);

}

}

点击GetSerDoc/Schema之后,如果成功会出现如下信息:

<img src="/oldimg/50/https://www.ut163.com/wp-content/uploads/2019/08/20190827124558-66.jpg" alt="SMP3.0学习笔记之四使用ODataNativeSDK获取数据” />

获取到ServiceDocument以及Schema信息以后,我们就可以获取航班信息了,当点击GetAirlines以后处罚getAirLines函数,进入AirLinesActivity:

public void getAirLines(View
view)

{

Intent intent = new Intent(this,
AirLinesActivity.class);

startActivity(intent);

}

AirLinesActivity的onCreate方法如下:

@Override

protected void onCreate(Bundle savedInstanceState)
{

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_air_lines);

initializeViews();

getAirlines();

}

private void initializeViews() {

mListView = (ListView) findViewById(R.id.listView1);

mListAdapter = new
ArrayAdapter(this,android.R.layout.simple_expandable_list_item_1);

mListView.setAdapter(mListAdapter);

mListView.setOnItemClickListener(this); //Handle on item
click

mListView.setEmptyView(findViewById(android.R.id.empty));

}

private void getAirlines() {

IRequest request = RequestBuilderHelper.getInstance().

buildGETRequest(this,
ConnectivityConstants.AIRLINE_COLLECTION);

((SDKApplication)getApplication()).getRequestManager().makeRequest(request);

}

当OData调用成功后,会回调onSuccess方法:

@Override

public void onSuccess(IRequest arg0, IResponse
_Response) {

// TODO Auto-generated method stub

try {

HttpEntity responseEntity = _Response.getEntity();

String responseString =
EntityUtils.toString(responseEntity);

Log.d(AirLinesActivity.class.getName(),
responseString);

Parser parser = ((SDKApplication)getApplication()).getParser();

IODataSchema schema = (IODataSchema)
((SDKApplication)getApplication()).getODataSchema();

mAirlineEntries = parser.parseODataEntries(responseString,
ConnectivityConstants.AIRLINE_COLLECTION, schema);

updateUI();

} catch (Exception e) {

e.printStackTrace();

}

}

AireLines界面如下:

<img src="/oldimg/50/https://www.ut163.com/wp-content/uploads/2019/08/20190827124558-89.jpg" alt="SMP3.0学习笔记之四使用ODataNativeSDK获取数据” />

当点击一个航空公司时,会进入FlightActivity界面,

显示每个航班的具体时间以及起止地点:

<img src="/oldimg/50/https://www.ut163.com/wp-content/uploads/2019/08/20190827124558-30.jpg" alt="SMP3.0学习笔记之四使用ODataNativeSDK获取数据” />

FlightActivity的onCreate相关代码以及调用成功的回调函数如下:

@Override

protected void onCreate(Bundle savedInstanceState)
{

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_flight);

mInflater = getLayoutInflater();

mApplication = (SDKApplication)getApplication();

mAirlineID =
getIntent().getExtras().getString(ConnectivityConstants.AIRLINE_ID);

initializeViews();

getFlights();

}

private void getFlights()

{

String filter = ConnectivityConstants.AIRLINE_ID + ” eq ” +
“‘” + mAirlineID + “‘”;

IRequest request =
RequestBuilderHelper.getInstance().buildGETRequest(this,
ConnectivityConstants.FLIGHT_COLLECTION, 0, filter);

mApplication.getRequestManager().makeRequest(request)

}

@Override

public void onSuccess(IRequest _Request, IResponse
_Response) {

// TODO Auto-generated method stub

try {

HttpEntity responseEntity = _Response.getEntity();

String responseString =
EntityUtils.toString(responseEntity);

Log.d(FlightActivity.class.getName(),
responseString);

Parser parser = mApplication.getParser();

IODataSchema schema = (IODataSchema)
mApplication.getODataSchema();

mFlightEntries = parser.parseODataEntries(responseString,
ConnectivityConstants.FLIGHT_COLLECTION, schema);

updateUI();

} catch (Exception e) {

e.printStackTrace();

}

}

最后,Unregister按钮用来注销用户,其代码如下:

public void unRegister(View view)

{

ClientConnection clientConnection =

new ClientConnection(getApplicationContext(),

ConnectivityConstants.mSmpAppId,

null,

null,

((SDKApplication)getApplication()).getRequestManager());

//Sets the Host and Port

clientConnection.setConnectionProfile(true,
ConnectivityConstants.mSmpHost, “8080”,null,
“0”);

String lSmpAppConnId =
PreferenceManager.getDefaultSharedPreferences(this).

getString(ConnectivityConstants.PREFS_KEY_APP_CID,
null);

//Set application connection ID to unregister the user

clientConnection.setApplicationConnectionID(lSmpAppConnId);

//Creates UserManager

try

{

UserManager userManager = new
UserManager(clientConnection);

userManager.deleteUser();

}

catch(Exception e)

{

e.printStackTrace();

}

//Clears old values

((SDKApplication)getApplication()).clearODataSchema();

clearPreferenceSettings();

}

最后编辑:
作者:yangsen
有需要可在文章留言或者邮件yan252@163.com给我。

留下一个回复

你的email不会被公开。

two × 2 =