I am trying to create a Warehouse management app for Android using Java and the Google Sheets API. I create a query to get the data stored in a sheet. I have a class which handles all the requests to the API. I create an instance of this object from an activity class and I want to load another Activity after the method readCurrentWarehouse() returns. The problem is that the main thread execution continues before the query responds and the result is obtained some seconds later. I have tried using both the Thread() and the AsyncTask, but the results were not the desired. My code for the function that handles the query is the following:
public class GoogleHandler {
private Sheets service;
private Exception mLastError = null;
public GoogleHandler(GoogleAccountCredential credential){
HttpTransport transport = AndroidHttp.newCompatibleTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
this.service = new Sheets.Builder(transport,jsonFactory,credential)
.setApplicationName("EvoScanner")
.build();
}
public void readCurrentWarehouse(){
Thread thread = new Thread(){
public void run(){
String spreadsheetId = //spreadsheetID;
try{
Spreadsheet spreadsheet = service.spreadsheets().get(spreadsheetId).execute();
Sheet lastSheet = spreadsheet.getSheets().get(spreadsheet.getSheets().size()-1);
String range = lastSheet.getProperties().getTitle();
List<String> results = new ArrayList<String>();
ValueRange response = service.spreadsheets().values().get(spreadsheetId,range).execute();
List<List<Object>> values = response.getValues();
if (values!=null){
for (List row : values){
results.add(row.get(0).toString());
}
}
System.out.println(results.size());
}catch (IOException e){
System.out.println("-------------------------------");
e.printStackTrace();
}
}
};
thread.start();
}
}
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private void handleSignInResult(Task<GoogleSignInAccount> task) {
try{
GoogleSignInAccount account = task.getResult(ApiException.class);
GoogleSignIn.requestPermissions(this,2,account, new Scope(SheetsScopes.SPREADSHEETS));
if (GoogleSignIn.hasPermissions(
GoogleSignIn.getLastSignedInAccount(this),new Scope(SheetsScopes.SPREADSHEETS)
)){
String accountName = account.getEmail();
GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(this, Arrays.asList(SheetsScopes.SPREADSHEETS)).setBackOff(new ExponentialBackOff());
credential.setSelectedAccountName(accountName);
GoogleHandler googleHandler = new GoogleHandler(credential);
googleHandler.readCurrentWarehouse();
Intent mainMenuIntent = new Intent(getBaseContext(),MainMenuActivity.class);
mainMenuIntent.putParcelableArrayListExtra("warehouse",warehouse.getWarehouse());
startActivity(mainMenuIntent);
}
}catch(ApiException e){
Log.w("tag","signInResult:failed code=" + e.getStatusCode());
Log.w("tag2","signInResult:failed description = "+e.getCause());
//updateUI(null)
}
}
Is there any way for the main thread to wait for the query to return the response before proceeding with loading the next Activity?