A request can e executed synchronously and asynchronously:
Synchronous requests. They return values after method execution. For example, when requesting a remote database the method is executed too long and blocks the calling stream. Unless the application addresses the database and downloads the table, other buttons are not available.
Asynchronous requests. They return no value and the application moves to the next method. The response is generated as soon as available.
To execute synchronous or asynchronous authentication at mobile platform server, create the AsynchronousRequestActivity application, which uses the auth method implemented in the AuthAPI unit.
The AsynchronousRequestActivity application includes one screen and buttons:
SYNCHRONOUS. Execute synchronous authentication.
ASYNCHRONOUS. Execute asynchronous authentication.
To view application work results, use the interactive example:
Click any button
public class AsynchronousRequestActivity extends AppCompatActivity {
private static final String MY_URL = "https://testmasterfmp.fsight.cloud/";
private static final VersionAPI MY_VERSION_API = VersionAPI.V_1;
private static final String MY_ENVIRONMENT = "Leonid_environment";
private static final String MY_PROJECT = "Leonid_project";
private static final String MY_VERSION = "v1";
private static final String MY_LOGIN = "Leonid";
private static final String MY_PASSWORD = "123123";
private static final String TAG = "AsynchronousRequest";
private HyperHive hyperHive;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_asynchronous_request);
Handler uiHandler = new Handler();
hyperHive = new HyperHiveState(getApplicationContext())
.setHostWithSchema(MY_URL)
.setApiVersion(MY_VERSION_API)
.setEnvironmentSlug(MY_ENVIRONMENT)
.setProjectSlug(MY_PROJECT)
.setVersionProject(MY_VERSION)
.setHandler(uiHandler)
.buildHyperHive();
}
// Execute authentication method synchronously
public void authenticationSynchronous(View view) {
boolean status = hyperHive.authAPI.auth(MY_LOGIN, MY_PASSWORD, true).execute().isOk();
showStatus(status);
}
// Execute authentication method asynchronously
public void authenticationAsynchronous(View view) {
// Implement the Callback anonymous interface
hyperHive.authAPI.auth(MY_LOGIN, MY_PASSWORD, true).enqueue(new Callback<basestatus>() {
@Override
// Get execution status
public void onResponse(Call<basestatus> call, BaseStatus baseStatus) {
showStatus(baseStatus.isOk());
}
@Override
public void onFailure(Call<basestatus> call, Throwable throwable) {
Log.d(TAG, "onFailure: ");
}
});
}
public void showStatus(boolean status) {
Log.d(TAG, "auth: " + status);
Toast.makeText(this, "auth: " + status, Toast.LENGTH_LONG).show();
}
}
See also: