我正在为一个作业开发一个android天气应用程序,我在youtube上遵循了这个教程来创建它一切正常,但是我无法在下面的json上显示“天气”对象的图标。我可以获取字符串形式的数据并显示它,但我不知道如何使用链接显示图标。openweathermap中指向天气图标的示例链接
api json格式
{
"coord": {
"lon": -122.08,
"lat": 37.39
},
"weather": [
{
"id": 800,
"main": "Clear",
"description": "clear sky",
"icon": "01d"
}
],
"base": "stations",
"main": {
"temp": 282.55,
"feels_like": 281.86,
"temp_min": 280.37,
"temp_max": 284.26,
"pressure": 1023,
"humidity": 100
},
"visibility": 16093,
"wind": {
"speed": 1.5,
"deg": 350
},
"clouds": {
"all": 1
},
"dt": 1560350645,
"sys": {
"type": 1,
"id": 5122,
"message": 0.0139,
"country": "US",
"sunrise": 1560343627,
"sunset": 1560396563
},
"timezone": -25200,
"id": 420006353,
"name": "Mountain View",
"cod": 200
}
apiclient.java文件
package com.dhanurjan.weather.Retrofit;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class ApiClient {
private static Retrofit retrofit = null;
public static Retrofit getClient(){ //creating object
if (retrofit == null){
retrofit = new Retrofit.Builder() //Retrofit.Builder class uses the Builder API to allow defining the URL end point for the HTTP operations and finally build a new Retrofit instance.
//http://api.openweathermap.org/data/2.5/weather?q=London&APPID=76a35a17f3e1bce771a09f3555b614a8
.baseUrl("https://api.openweathermap.org/data/2.5/")
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
API接口.java
package com.dhanurjan.weather.Retrofit;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;
public interface ApiInterface {
@GET("weather?APPID=76a35a17f3e1bce771a09f3555b614a8&units=metric")//http://api.openweathermap.org/data/2.5/weather?q=London&APPID=76a35a17f3e1bce771a09f3555b614a8
Call<Example> getWeatherData(@Query("q") String name);
}
天气.java
package com.dhanurjan.weather.Retrofit;
import com.google.gson.annotations.SerializedName;
public class Weather {
@SerializedName("main")
String main;
@SerializedName("description")
String description;
@SerializedName("icon")
String icon;
public String getMain() { return main; }
public void setMain(String main) { this.main = main; }
public String getDescription() { return description; }
public void setDescription(String description) { this.description = description; }
public String getIcon() { return icon; }
public void setIcon(String icon) { this.icon = icon; }
}
主活动.java
package com.dhanurjan.weather;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.dhanurjan.weather.Retrofit.ApiClient;
import com.dhanurjan.weather.Retrofit.ApiInterface;
import com.dhanurjan.weather.Retrofit.Example;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class MainActivity extends AppCompatActivity {
Button searchBtn;
TextView tempText , feelText , humidityText, lon, lat, country, sunrise, sunset, descText, windSpeed;
EditText cityName;
ImageView icon;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
searchBtn = findViewById(R.id.searchBtn);
tempText = findViewById(R.id.tempText);
feelText = findViewById(R.id.feelText);
humidityText = findViewById(R.id.humidityText);
country = findViewById(R.id.country);
sunrise = findViewById(R.id.sunrise);
sunset = findViewById(R.id.sunset);
lon = findViewById(R.id.lonText);
lat = findViewById(R.id.latText);
cityName = findViewById(R.id.cityName);
descText = findViewById(R.id.descText);
icon = findViewById(R.id.icon);
windSpeed = findViewById(R.id.windSpeed);
searchBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getWeatherData(cityName.getText().toString().trim());
}
});
}
private void getWeatherData(String name){
ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);
Call<Example> call = apiInterface.getWeatherData(name);
call.enqueue(new Callback<Example>() {
@Override
public void onResponse(Call<Example> call, Response<Example> response) {
tempText.setText(response.body().getMain().getTemp()+" ℃");
feelText.setText("Feels Like :"+" "+response.body().getMain().getFeels_like()+" ℃");
lon.setText("Longitude :"+" "+response.body().getCoord().getLon()+" °E");
lat.setText("Latitude :"+" "+response.body().getCoord().getLat()+" °N");
humidityText.setText("Humidity :"+" "+response.body().getMain().getHumidity()+" %");
country.setText(response.body().getSys().getCountry());
sunrise.setText("Sunrise :"+" "+response.body().getSys().getSunrise()+" AM");
sunset.setText("Sunset :"+" "+response.body().getSys().getSunset()+" PM");
windSpeed.setText("windSpeed :"+" "+response.body().getWind().getSpeed()+" KM/H");
descText.setText(response.body().getWeatherList().get(0).getDescription());
//icon.setImageIcon(response.body().getWeatherList().get(0).getIcon());
}
@Override
public void onFailure(Call<Example> call, Throwable t) {
}
});
}
}
暂无答案!
目前还没有任何答案,快来回答吧!