我的问题的答案可能在那里,但我不确定我是否正确理解了这个概念。
我有一个小型JSON文件,仅包含
{"meteo": "1"}
我可以成功阅读GSON并向我展示我的敬酒。
但是,如果A基于这样做的" if语句":
if(meteoStatus == "1"){ // I know for sure it's 1
// Do something
} else {
// Do something else
}
尽管我已经做了烤面包,但它总是转到if语句的第二部分,这向我展示了1
这是代码的完整部分:
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"));
}
任何帮助都将不胜感激; - (
字符串是jsonobject,因此首先将meto从这样的json对象中删除
String meteoStatus;
try {
JSONObject object = new JSONObject("{"meteo": "1"}");
meteoStatus = object.getString("meteo");
if(meteoStatus.equals("1")){
}else {
}
} catch (JSONException e) {
e.printStackTrace();
}
===用于布尔,浮点和整数检查。对于字符串比较,u始终使用等价运算符。
ex: if(meteostatus.equals(" 1"(({
}else {
}