Search code examples
javaandroidretrofit

I want to convert temperature value from unit to imperial in Retrofit in Android Studio


I want This Temperature Value that it give me

I have an issue in Retrofit. I want to convert Temperature value in Units To Imperial.

Mean I get data from API keys for Weather Application and when I run APP in Android it give me temperature values like this(285,200,etc). This is wrong because this temperature is not correctly. The now our Islamabad (Pakistan) city temperature is 15, and it give me 285, 200, etc, but JSON convertor give me a exact temperature of my city but Android Studio does not generate it.

I want the exact temperature of the city which API give me but android studio generate his own values which is not correct.

Main Activity Class

    package com.deitel.apiretrofitweatherapp;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.ImageFormat;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {
    public static String BaseUrl = "http://api.openweathermap.org/";
    public static String AppId = "90ebdc57172a838d0fce0abbc044df8e";
    public static String lat = "33.69";
    public static String lon = "73.06";


    private TextView textView_country,textView_city,textView_temp,/*textView_temp_min,textView_tem_max,*/textView_pressure,textView_humidity,textview_date;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView_country=findViewById(R.id.textView_country);
        textView_city=findViewById(R.id.textView_city);
        textView_temp=findViewById(R.id.text_temp);
        textView_pressure=findViewById(R.id.textView_pressure);
       /* textView_tem_max=findViewById(R.id.textView_temp_max);
        textView_temp_min=findViewById(R.id.textView_temp_min);*/
        textView_humidity=findViewById(R.id.textView_humidity);
        textview_date=findViewById(R.id.textView_date);
        getCurrentData();

    }
    void getCurrentData()
    {
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(BaseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        WeatherService weatherService=retrofit.create(WeatherService.class);
        Call<WeatherResponse> call=weatherService.getCurrentWeatherData(lat,lon,AppId);
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                if (response.code()==200)
                {
                    WeatherResponse weatherResponse= (WeatherResponse) response.body();
                    assert weatherResponse != null;
                    Calendar calendar = Calendar.getInstance();
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEEE-dd-MM");
                    String formatedate = simpleDateFormat.format(calendar.getTime());
                   /* String stringbuilder= "Country : " +
                            weatherResponse.sys.country +
                            "\n" +
                            "City : " +weatherResponse.name +
                            "\n" +
                            "Tempreture : " + weatherResponse.main.temp +
                            "\n" +
                            "Tempreture(Min) : " +
                            weatherResponse.main.temp_min +
                            "\n" +
                            "Tempreture(Max) : " +
                            weatherResponse.main.temp_max +
                            "\n" +
                            "Humidity : " +
                            weatherResponse.main.humidity +
                            "\n" +
                            "Pressure : " +
                            weatherResponse.main.pressure;*/

                             String Country=weatherResponse.sys.country;
                             String City=weatherResponse.name;
                             String Temp= String.valueOf(weatherResponse.main.temp);
                            double temp_int = Double.parseDouble(Temp);
                            double centi_int = (temp_int -32 )/1.8000;
                            centi_int = Math.round(centi_int);
                            int i = (int) centi_int;

                             /*String Temp_max= String.valueOf(weatherResponse.main.temp_max);
                             String Temp_min= String.valueOf(weatherResponse.main.temp_min);*/
                             String Pressure= String.valueOf(weatherResponse.main.pressure);
                             String Humidity=String.valueOf(weatherResponse.main.humidity);
                             textView_country.setText(Country);
                             textView_city.setText(City);
                             textView_temp.setText(String.valueOf(i));
                             /*textView_tem_max.setText(Temp_max);
                             textView_temp_min.setText(Temp_min);*/
                             textView_pressure.setText(Pressure);
                             textView_humidity.setText(Humidity);
                             textview_date.setText(formatedate);


                }
            }

            @Override
            public void onFailure(Call call, Throwable t) {
                textView_country.setText(t.getMessage());
                textView_city.setText(t.getMessage());
                textView_temp.setText(t.getMessage());
                /*textView_tem_max.setText(t.getMessage());
                textView_temp_min.setText(t.getMessage());*/
                textView_pressure.setText(t.getMessage());
                textView_humidity.setText(t.getMessage());
                textview_date.setText(t.getMessage());

            }
        });
    }

}

WebResponse Class

    package com.deitel.apiretrofitweatherapp;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.util.List;
public class WeatherResponse {

    @SerializedName("coord")
    @Expose
    public Coord coord;
    @SerializedName("weather")
    @Expose
    public List<Weather> weather = null;
    @SerializedName("base")
    @Expose
    public String base;
    @SerializedName("main")
    @Expose
    public Main main;
    @SerializedName("visibility")
    @Expose
    public Integer visibility;
    @SerializedName("wind")
    @Expose
    public Wind wind;
    @SerializedName("clouds")
    @Expose
    public Clouds clouds;
    @SerializedName("dt")
    @Expose
    public Integer dt;
    @SerializedName("sys")
    @Expose
    public Sys sys;
    @SerializedName("timezone")
    @Expose
    public Integer timezone;
    @SerializedName("id")
    @Expose
    public Integer id;
    @SerializedName("name")
    @Expose
    public String name;
    @SerializedName("cod")
    @Expose
    public Integer cod;

}
class Weather {

    @SerializedName("id")
    @Expose
    public Integer id;
    @SerializedName("main")
    @Expose
    public String main;
    @SerializedName("description")
    @Expose
    public String description;
    @SerializedName("icon")
    @Expose
    public String icon;

}
class Clouds {
    @SerializedName("all")
    public float all;
}

class Rain {
    @SerializedName("3h")
    public float h3;
}

class Wind {

    @SerializedName("speed")
    @Expose
    public Double speed;
    @SerializedName("deg")
    @Expose
    public Integer deg;

}

 class Main {

    @SerializedName("temp")
    @Expose
    public double temp;
    /*@SerializedName("feels_like")
    @Expose
    public float feelsLike;
    @SerializedName("temp_min")
    @Expose
    public float tempMin;
    @SerializedName("temp_max")
    @Expose
    public float tempMax;*/
    @SerializedName("pressure")
    @Expose
    public float pressure;
    @SerializedName("humidity")
    @Expose
    public float humidity;

}


class Sys {

    @SerializedName("type")
    @Expose
    public Integer type;
    @SerializedName("id")
    @Expose
    public Integer id;
    @SerializedName("country")
    @Expose
    public String country;
    @SerializedName("sunrise")
    @Expose
    public Integer sunrise;
    @SerializedName("sunset")
    @Expose
    public Integer sunset;

}
class Coord {
    @SerializedName("lon")
    public float lon;
    @SerializedName("lat")
    public float lat;
}

WebService Interface Class

    package com.deitel.apiretrofitweatherapp;

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;

public interface WeatherService {
    @GET("data/2.5/weather?")
    Call <WeatherResponse>getCurrentWeatherData(@Query("lat") String lat, @Query("lon") String lon, @Query("APPID") String app_id);
}

Solution

  • Can you try this once?

     JSONObject tempObject = arrWeatherList.getJSONObject(i).getJSONObject("temp");
    
     String temperature = changeTemp(tempObject.get("day").toString());
    
     private String changeTemp(String x) {
        Double celsius = Double.parseDouble(x) - 273.0;
        Integer i = celsius.intValue();
        return String.valueOf(i);
    }