Skip to content Skip to sidebar Skip to footer

How To Stop Exception From Searching Unavailable/no Cities?

I have a Weather app that searches for any city typed on the EditText and it works very well. But the problem is that the app crashes and reports '(my app) has stopped' on my phone

Solution 1:

As OP said I just need the best method to handle the exceptions

Let me give two examples for your case to avoid the crash and show toast for both Activity and Fragment.

  • try/catch

Update your Activity onResponse like below.

publicvoidonResponse(Call<Example> call, Response<Example> response) {

                    try {
                        time_field.setText("Last Updated:" + " " + response.body().getDt());
                    } catch (Exception e) {
                        time_field.setText("Last Updated: Unknown");
                        Log.e("TAG", "No City found");
                        Toast.makeText(HomeActivity.this, "No City found", Toast.LENGTH_SHORT).show();
                    }
                }

Update your Fragment `onResponse like below

publicvoidonResponse(Call<Example> call, Response<Example> response) {

            try {
                current_temp.setText(response.body().getMain().getTemp() + " ℃");
                current_output.setText(response.body().getWeather().get(0).getDescription());
                rise_time.setText(response.body().getSys().getSunrise() + " ");
                set_time.setText(response.body().getSys().getSunset() + " ");
                temp_out.setText(response.body().getMain().getTemp() + " ℃");
                Press_out.setText(response.body().getMain().getPressure() + " hpa");
                Humid_out.setText(response.body().getMain().getHumidity() + " %");
                Ws_out.setText(response.body().getWind().getSpeed() + " Km/h");
                Visi_out.setText(response.body().getVisibility() + " m");
                Cloud_out.setText(response.body().getClouds().getAll() + " %");
            } catch (Exception e) {
                Log.e("TAG", "No City found");
                Toast.makeText(getActivity(), "No City found", Toast.LENGTH_SHORT).show();
            }

        }
  • null check

Update your Activity onResponse like below.

publicvoidonResponse(Call<Example> call, Response<Example> response) {

                    if (response.isSuccessful() && response.body() != null) {
                        time_field.setText("Last Updated:" + " " + response.body().getDt());
                    } else {
                        time_field.setText("Last Updated: Unknown");
                        Log.e("TAG", "No City found");
                        Toast.makeText(HomeActivity.this, "No City found", Toast.LENGTH_SHORT).show();
                    }

                }

Update your Fragment onResponse like below

publicvoidonResponse(Call<Example> call, Response<Example> response) {

            if (response.isSuccessful() && response.body() != null) {
                current_temp.setText(response.body().getMain().getTemp() + " ℃");
                current_output.setText(response.body().getWeather().get(0).getDescription());
                rise_time.setText(response.body().getSys().getSunrise() + " ");
                set_time.setText(response.body().getSys().getSunset() + " ");
                temp_out.setText(response.body().getMain().getTemp() + " ℃");
                Press_out.setText(response.body().getMain().getPressure() + " hpa");
                Humid_out.setText(response.body().getMain().getHumidity() + " %");
                Ws_out.setText(response.body().getWind().getSpeed() + " Km/h");
                Visi_out.setText(response.body().getVisibility() + " m");
                Cloud_out.setText(response.body().getClouds().getAll() + " %");
            } else {
                Log.e("TAG", "No City found");
                Toast.makeText(getActivity(), "No City found", Toast.LENGTH_SHORT).show();
            }


        }

Note: There is a bit of scope to improve your code and design a better, like to avoid an extra call to API on search click, one from Activity and another in the fragment. But all those suggestion will be out of the scope of this question, So I will stick to OP request which is I just need the best method to handle the exceptions.

Solution 2:

publicvoidonResponse(@NotNull Call<Example> call, @NotNull Response<Example> response) {
    
           assert response.body() !=null;
    
    if(response.body().getMain()==null && response.body().getWeather()!=null && all other field!=null){
    yourErrorTextview.setText("No City found");
    allOtherField.setText();
    }
    elseif(response.body().getMain()!=null && response.body().getWeather()!=null && all other field!=null){
             current_temp.setText(response.body().getMain().getTemp() + " ℃");
             allOtherField.setText();
    }}

Solution 3:

According to the log you get a success response with a body but getMain returns null.

You can prevent this by adding some null checks around getMain. Make sure to check the possibility of null in the api documentation.

Solution 4:

You can use try catch block in catch you are getting exception you can toast message city not found so your app will not crash.

Post a Comment for "How To Stop Exception From Searching Unavailable/no Cities?"