-1

The answer of my question is probably out there but I not sure I understand the concept correctly.

I've a small Json file, which contains only

{"meteo": "1"}

I can successfully read it with Gson and show it my a Toast.

But If a do an "if statement" based on it like this:

 if(meteoStatus == "1"){ // I know for sure it's 1
// Do something
} else {
// Do something else
}

It always goes to the second part of the if statement despite I have done a toast just before which show me a 1

Here is the complete section of code:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        String url = "http://www.boisdelacambre.be/ios/json/weather.php?key=53666d6c7b206a532d52403e414d2579";
        String result = getUrlContents(url);

        gsonInstance = new Gson();
        meteo = new Meteo();
        meteo = gsonInstance.fromJson(result, meteo.getClass());

        String meteoStatus = meteo.getMeteo();


        View rootView = inflater.inflate(R.layout.fragment_meteo, container, false);
        ImageWeather = (ImageView) rootView.findViewById(R.id.imageWeather);
        DonnesOuRemis = (TextView) rootView.findViewById(R.id.donnesOuRemis);

        Toast.makeText(getActivity(), meteoStatus, Toast.LENGTH_LONG).show();

        if(meteoStatus == "1"){
            // il fait beau
            Toast.makeText(getActivity(), "Pas remis !!", Toast.LENGTH_LONG).show();
            ImageWeather.setImageResource(R.drawable.soleil);
            DonnesOuRemis.setText("Donnés");
            DonnesOuRemis.setTextColor(Color.parseColor("#06f828"));

        } else {
            Toast.makeText(getActivity(), "Pourquoi remis ??", Toast.LENGTH_LONG).show();
            ImageWeather.setImageResource(R.drawable.pluie);
            DonnesOuRemis.setText("Remis");
            DonnesOuRemis.setTextColor(Color.parseColor("#f80b27"));
        }

Any help will be highly appreciated ;-)

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

0

The string what you have is JSONObject, so first get meto out of the json object like this

  String meteoStatus;
    try {
        JSONObject object = new JSONObject("{\"meteo\": \"1\"}");
        meteoStatus = object.getString("meteo");

        if(meteoStatus.equals("1")){

        }else {

        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
Sathya Baman
  • 3,424
  • 7
  • 44
  • 77
0

== is used for boolean, float and integer checks. for string comparison u should always use equals operator.

Ex: if(meteoStatus.equals("1")){

    }else {

    }
Harshitha
  • 104
  • 3