从阶级到活动获得价值?

时间:2014-03-06 14:36:07

标签: java android android-activity android-volley

任何人都可以提供帮助。当我想在另一个活动中获得温度时,我得到的值为0。 在onResponse中,我尝试了设置温度并设置了它。我不知道这个问题。

    private double temperature;

public double getTemperature() {
    return temperature;
}

public JSONparser(Context context, String city) {

    RequestQueue queue = Volley.newRequestQueue(context);
    String url = "http://api.openweathermap.org/data/2.5/weather?q=" + city;

    JsonObjectRequest jsObjRequest = new JsonObjectRequest(
            Request.Method.GET, url, null,
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    // TODO Auto-generated method stub
                    try {

                        temperature = (response.getJSONObject("main")
                                .getDouble("temp"));

                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    // TODO Auto-generated method stub

                }
            });

    queue.add(jsObjRequest);

}

1 个答案:

答案 0 :(得分:1)

对你的类对象使用单例,并且也可以使用setFunction而不直接初始化值,因为如果不使用同一个对象,该值将为0

public void setTemperature(double mTemp) {
    temperature =  mTemp;
}
相关问题