2017-04-19 76 views
-1

我的问题的答案可能在那里,但我不知道我正确理解这个概念。Gson异步结果,如果语句

我有一个小的JSON文件,其中只包含

{"meteo": "1"} 

我成功可以GSON阅读并显示它我敬酒。

但如果做基于它这样的一个“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")); 
     } 

任何帮助将高度赞赏;-)

+0

尝试'meteoStatus.equals( “1”)' –

+0

的作品!谢谢(即使我不明白为什么...) –

+3

可能的重复[如何比较Java中的字符串?](http://stackoverflow.com/questions/513832/how-do-i-compare-字符串在Java) – Selvin

回答

0

是否有什么这个字符串的JSONObject,所以首先得梅托了JSON对象的这样

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

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

     }else { 

     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
+0

这段代码需要清理。另外,OP要求Gson,而不是org.json。 –

0

==用于布尔型,浮点和整数检查。对于字符串比较,你应该总是使用equals运算符。

例: 如果(meteoStatus.equals( “1”)){

}else { 

    }