我想在 Android Studio 的 Retrofit 中将温度值从单位转换为英制

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

我在 Retrofit 中遇到问题。我想将以单位为单位的温度值转换为英制。

意味着我从天气应用程序的 API 键获取数据,当我在 Android 中使用 运行 应用程序时,它会给我这样的温度值(285,200 等)。这是错误的,因为这个温度不正确。现在我们的伊斯兰堡(巴基斯坦)城市温度是 15,它给了我 285、200 等,但是 JSON 转换器给了我我所在城市的准确温度,但是 Android Studio 没有生成它。

我想要 API 给我的城市的确切温度,但是 android 工作室生成了他自己的值,这是不正确的。

主要ActivityClass

    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;
}

网络服务接口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);
}

你能试试这个吗?

 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);
}