-1

I have a Java Android app that uses an API to recognise images using AI. I require internet connection for my app to connect using the API. While the app retrieves data from the API, if the internet connection is lost, then the Activity closes with a RuntimeException.

Code that gets executed on clicking button in Activity:

new RecogniseImage().execute(new File(photoPath));

Here, photoPath contains the path to the image.

RecogniseImage.java file:

//Connect to the API and retrieve data
V2Grpc.V2BlockingStub stub = V2Grpc.newBlockingStub(ClarifaiChannel.INSTANCE.getGrpcChannel())
                    .withCallCredentials(new ClarifaiCallCredentials(PAT));

            //Identify image
            MultiOutputResponse postModelOutputsResponseFood = stub.postModelOutputs(
               PostModelOutputsRequest.newBuilder().setUserAppId(UserAppIDSet.newBuilder().setUserId(USER_ID)
                    .setAppId(APP_ID))
                    .setModelId(MODEL_ID_FOOD)
                    .addInputs(
                        Input.newBuilder().setData(
                            Data.newBuilder().setImage(
                                Image.newBuilder()
                                    .setBase64(ByteString.copyFrom(Files.readAllBytes(
                                            new File(IMAGE_FILE_LOCATION).toPath())))))).build());

            if (postModelOutputsResponseFood.getStatus().getCode() != StatusCode.SUCCESS) {
                throw new RuntimeException("Post model outputs failed, status: " + postModelOutputsResponseFood.getStatus());
            }

            Output outputFood = postModelOutputsResponseFood.getOutputs(0);

I tried wrapping the code that calls the API in try and catch statements, but the Activity still crashes. Is there any way I can prevent the Activity from closing if there is a RuntimeException?

I tried wrapping in try & catch statements like below:

try {
    new RecogniseImage().execute(new File(photoPath));
} 
catch (RuntimeException e){
   Log.d("Error","An error occurred");
}

or like below:

try {
     //Connect to the API and retrieve data
     V2Grpc.V2BlockingStub stub = V2Grpc.newBlockingStub(ClarifaiChannel.INSTANCE.getGrpcChannel())
                    .withCallCredentials(new ClarifaiCallCredentials(PAT));

    //Identify image
    MultiOutputResponse postModelOutputsResponseFood = stub.postModelOutputs(            PostModelOutputsRequest.newBuilder().setUserAppId(UserAppIDSet.newBuilder().setUserId(USER_ID)
                    .setAppId(APP_ID))
                    .setModelId(MODEL_ID_FOOD)
                    .addInputs(
                        Input.newBuilder().setData(
                            Data.newBuilder().setImage(
                                Image.newBuilder()
                                    .setBase64(ByteString.copyFrom(Files.readAllBytes(
                                            new File(IMAGE_FILE_LOCATION).toPath())))))).build());

            if (postModelOutputsResponseFood.getStatus().getCode() != StatusCode.SUCCESS) {
                throw new RuntimeException("Post model outputs failed, status: " + postModelOutputsResponseFood.getStatus());
            }

            Output outputFood = postModelOutputsResponseFood.getOutputs(0);
}
catch (RuntimeException e){
    Log.d("Error","An error occurred");
}

I'd like to stop the Activity from closing and alert the user if there is any exception.

Any help or suggestion is welcome.

Dinux
  • 644
  • 1
  • 6
  • 19

2 Answers2

0

You can take reference from below URL they are using separate class for handling the error and extending into the class where you want to handle the exception. Please have a look here

Hope this helps. Thank you!

0

REMOVE THIS

throw new RuntimeException("Post model outputs failed, status: " + postModelOutputsResponseFood.getStatus());

Rohaitas Tanoli
  • 624
  • 4
  • 16