Weather App temperatura getTemp() zwraca 0

0

Witam, pisze prosta aplikacje pogodowa i mam pewiem problem gdyz gdy chce odczytac temperature to ciagle zwracane jest 0 udalo mi sie odczytac juz description a temp nie moge :/ Podaje kod\

package com.example.milosz.weatherapp;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by milosz on 15.02.16.
 */
public class WeatherDataAPI {



    public static class Weather{
        public Integer id;
        public String main;
        public String description;

    }

    private List<Weather> weather;
    private float temp;

    public List<Weather> getWeather() {return weather;}
    public void setTemp(float temp){this.temp=temp;}
    public float getTemp(){return this.temp;}


    @Override
    public String toString()
    {
        return "weather" + weather + "temp" + temp;
    }
}
 
package com.example.milosz.weatherapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity  {

    ConnectionAPI connection = new ConnectionAPI();
    WeatherDataAPI weatherDataAPI = new WeatherDataAPI();
    TextView weatherRes;
    EditText cityName;
    String tempCityName;
    String responseString;
    WeatherDataAPI result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        cityName=(EditText)findViewById(R.id.cityNameRow);
        weatherRes = (TextView)findViewById(R.id.weatherResult);

        connection.queue = Volley.newRequestQueue(this);

    }

    public void getWeather(View view) {

        tempCityName = String.valueOf(cityName.getText());
        connection.url = "http://api.openweathermap.org/data/2.5/weather?q="+tempCityName+"&appid=44db6a862fba0b067b1930da0d769e98";
        connection.stringRequest = new StringRequest(connection.url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                 responseString=response;

                 Gson gson = new Gson();
                 result = gson.fromJson(responseString, WeatherDataAPI.class);
                 weatherRes.setText(result.getWeather().get(0).description +"\n"+ result.getTemp());


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getApplicationContext(),"Whoops Something go wrong",Toast.LENGTH_SHORT).show();
            }
        });
        connection.queue.add(connection.stringRequest);


    }
}





 
1

Nie używałem nigdy Gsona ale z tego co widzę, to jeżeli zwracany Json wygląda tak:

{
	"coord": {
		"lon": -0.13,
		"lat": 51.51
	},
	"weather": [{
		"id": 501,
		"main": "Rain",
		"description": "moderate rain",
		"icon": "10n"
	}, {
		"id": 300,
		"main": "Drizzle",
		"description": "light intensity drizzle",
		"icon": "09n"
	}],
	"base": "stations",
	"main": {
		"temp": 277.2,
		"pressure": 1009,
		"humidity": 100,
		"temp_min": 276.05,
		"temp_max": 278.35
	},
	"visibility": 10000,
	"wind": {
		"speed": 4.6,
		"deg": 180
	},
	"rain": {
		"1h": 1.05
	},
	"clouds": {
		"all": 75
	},
	"dt": 1455749689,
	"sys": {
		"type": 1,
		"id": 5093,
		"message": 0.0455,
		"country": "GB",
		"sunrise": 1455692992,
		"sunset": 1455729594
	},
	"id": 2643743,
	"name": "London",
	"cod": 200
}

To czy nie próbowałeś sobie zrobić klasy Mainz polem temp tak jak masz klasę Weather? Skoro description i temp jest na tym samym poziomie to może tak to zadziała - ale tak tylko zgaduję.

1

A gdzie masz model to Main? Przecież jak pasujesz dane tylko do "Weather" to jak chcesz wyciągnąć temperaturę skoro ona jest w zupełnie innym obiekcie?

1 użytkowników online, w tym zalogowanych: 0, gości: 1