OpenWeatherMap图标下载android
我正在开发一款使用天气服务的android应用程序。我目前正在使用openweathermap.org的API,我注意到在响应中还有一个图标标签,我想知道您是如何实际显示此图像的。我曾试图摆弄它,但似乎弄不明白 这是我的主要活动:OpenWeatherMap图标下载android,android,api,openweathermap,Android,Api,Openweathermap,我正在开发一款使用天气服务的android应用程序。我目前正在使用openweathermap.org的API,我注意到在响应中还有一个图标标签,我想知道您是如何实际显示此图像的。我曾试图摆弄它,但似乎弄不明白 这是我的主要活动: public class MainActivity extends Activity { String description; @Override protected void onCreate(Bundle savedInstanceState) { s
public class MainActivity extends Activity {
String description;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onStart() {
super.onStart();
Double lat = 55.676098;
Double lon = 12.568337;
String apiKey = "70c5bf4e84a725a8aeb3dd8c7df4c254";
String urlAPI = "http://api.openweathermap.org/data/2.5/weather?lat=" + lat + "&lon=" + lon + "&APPID=" + apiKey;
String imgURL = "http://openweathermap.org/img/w/" + description + ".png";
Weather weatherApi = new Weather();
weatherApi.execute(urlAPI);
weatherApi.execute(imgURL);
}
public void goToShowFishActivity(View view) {
Intent intent = new Intent(this, ShowFishiesActivity.class);
startActivity(intent);
}
public void goToAddNewCatchActivity(View view) {
Intent intent = new Intent(this, AddCatch.class);
startActivity(intent);
}
public void goToLogin(View view) {
Intent intent = new Intent(this, CreateUserActivity.class);
startActivity(intent);
}
private class Weather extends ReadHttpTask{
@Override
protected void onPostExecute(CharSequence charSequence){
String text = charSequence.toString();
Integer start = text.indexOf("icon\":\"" ) + "icon\":\"".length();
Integer end = text.indexOf("\"}",start);
description = text.substring(start, end);
TextView weatherTry = (TextView) findViewById(R.id.weatherTry);
weatherTry.setText(description);
}
}
}
我的ReadHttpTask类:
public class ReadHttpTask extends AsyncTask<String, Void, CharSequence> {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
protected CharSequence doInBackground(String...urls) {
String urlString = urls[0];
try{
CharSequence result = HttpHelper.GetHttpResponse(urlString);
return result;
}
catch (IOException ex){
cancel(true);
String errorMessage = ex.getMessage() + "\n" + urlString;
Log.e("Something went wrong", errorMessage);
return errorMessage;
}
}
}
json中的图标值,比如09d或50d就是图标代码。要获取图标,需要创建url,如: 我建议您将图标代码存储为
字符串
,并使用来实际显示图标
String icon = yourJsonObject.getString("icon");
String iconUrl = "http://openweathermap.org/img/w/" + icon + ".png";
Picasso.with(context).load(iconUrl).into(yourImageView);
json中的图标值,比如09d或50d就是图标代码。要获取图标,需要创建url,如: 我建议您将图标代码存储为
字符串
,并使用来实际显示图标
String icon = yourJsonObject.getString("icon");
String iconUrl = "http://openweathermap.org/img/w/" + icon + ".png";
Picasso.with(context).load(iconUrl).into(yourImageView);
它可以帮助你,但我使用滑翔
String icon = items.get(position).weather.get(0).icon;
String iconUrl = "http://openweathermap.org/img/w/" + icon + ".png";
Glide.with(context).load(iconUrl).into(holder.image_weather);
它可以帮助你,但我使用滑翔
String icon = items.get(position).weather.get(0).icon;
String iconUrl = "http://openweathermap.org/img/w/" + icon + ".png";
Glide.with(context).load(iconUrl).into(holder.image_weather);