我正在开发天气android应用,我想以相应的说明将天气情况显示为图标,但图像视图未显示网址图标 在我的终点以下
public interface ApiService {
@GET("/v1/search.json?")
// return type is List
Call<List<Weather>> getWeather(
@Query("key") String mKey,
@Query("q") String mCityName);
@GET("v1/forecast.json")
Call<ForecastEndingPoint> forecast(
@Query("key") String key,
@Query("q") String condition,
@Query("days") int days
);
}
在我已声明API URL的WeatherClient.java类下面
public class WeatherClient {
private static final String ROOT_URL = "http://api.apixu.com";
/**
* Get Retrofit Instance
*/
private static Retrofit getRetrofitInstance() {
return new Retrofit.Builder()
.baseUrl(ROOT_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
/**
* Get API Service
*
* @return API Service
*/
public static ApiService getApiService() {
return getRetrofitInstance().create(ApiService.class);
}
}
在我的ConditionAdapter.java类下,该类已实现了天气描述和图标
public class ConditionAdapter extends RecyclerView.Adapter<ConditionAdapter.CustomViewHolder> {
public List<ForecastEndingPoint> forecastList;
public Context context;
public Context picassoContext;
private final int imageWidthPixels = 1024;
private final int imageHeightPixels = 768;
public ConditionAdapter(List<ForecastEndingPoint> forecastList, Context context, Context picassoContext) {
this.forecastList = forecastList;
this.context = context;
this.picassoContext = picassoContext;
}
@Override
public ConditionAdapter.CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.weather_list, parent, false);
return new CustomViewHolder(itemView);
}
@Override
public void onBindViewHolder(CustomViewHolder holder, int position) {
// String icon = condition1.getCurrent().getCondition().getIcon();
// int r = map.get(icon);
ForecastEndingPoint forecastEndingPoint = forecastList.get(position);
holder.weatherDescription.setText((String.valueOf(forecastEndingPoint.getCurrent().getCondition().getText())));
// Condition condition = new Condition();
Picasso.get().load(forecastEndingPoint.getCurrent().getCondition().getIcon())
.fit().centerCrop().into(holder.weatherIcon);
// @@ -80,7 +80,7 @@ public int getItemCount() {
// Picasso.get().load(forecastEndingPoint.getCurrent().getCondition().getIcon((holder.weatherIcon)));
// holder.weatherCode.setText(condition2.getCode());
// Drawable drawable = ContextCompat.getDrawable(context, r);
// holder.weatherIcon.setImageDrawable(drawable);
// Picasso.get(getApplication).load((forecastEndingPoint.getCurrent().getCondition().getIcon(holder.weatherIcon)));
//holder.weatherCode.setText(icon);
//holder.weatherIcon.setImageDrawable(drawable);
//holder.weatherDescription.setText(condition.getText());
}
@Override
public int getItemCount() {
return forecastList.size();
}
public class CustomViewHolder extends RecyclerView.ViewHolder {
public TextView weatherDescription;
public ImageView weatherIcon;
public CustomViewHolder(View view) {
super(view);
weatherDescription = view.findViewById(R.id.weatherDescription);
// weatherDescription = (TextView)view.findViewById(R.id.weatherDescription);
weatherIcon = view.findViewById(R.id.weatherIcon);
}
}
}
在我实现了城市名称和地区的WeatherAdapter类下面
public class WeatherAdapter extends RecyclerView.Adapter<WeatherAdapter.CustomViewHolder> {
private List<Weather> weathers;
private List<Condition> conditionList;
private Context context;
public WeatherAdapter(List<Weather> weathers) {
this.weathers = weathers;
}
@Override
public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.weather_list, parent, false);
return new CustomViewHolder(itemView);
}
@Override
public void onBindViewHolder(CustomViewHolder holder, int position) {
Weather weather = weathers.get(position);
holder.andijanView.setText(weather.getName());
holder.regionView.setText(weather.getRegion());
}
@Override
public int getItemCount() {
return weathers.size();
}
public class CustomViewHolder extends RecyclerView.ViewHolder {
public TextView andijanView, regionView;
public CustomViewHolder(View view) {
super(view);
andijanView = (TextView) view.findViewById(R.id.andijanView);
regionView = (TextView) view.findViewById(R.id.regionView);
}
}
}
below my weather_list.xml class
public class WeatherAdapter extends RecyclerView.Adapter<WeatherAdapter.CustomViewHolder> {
private List<Weather> weathers;
private List<Condition> conditionList;
private Context context;
public WeatherAdapter(List<Weather> weathers) {
this.weathers = weathers;
}
@Override
public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.weather_list, parent, false);
return new CustomViewHolder(itemView);
}
@Override
public void onBindViewHolder(CustomViewHolder holder, int position) {
Weather weather = weathers.get(position);
holder.andijanView.setText(weather.getName());
holder.regionView.setText(weather.getRegion());
}
@Override
public int getItemCount() {
return weathers.size();
}
public class CustomViewHolder extends RecyclerView.ViewHolder {
public TextView andijanView, regionView;
public CustomViewHolder(View view) {
super(view);
andijanView = (TextView) view.findViewById(R.id.andijanView);
regionView = (TextView) view.findViewById(R.id.regionView);
}
}
}
和activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recycler_view"
android:scrollbars="vertical"/>
</RelativeLayout>
我正在获取天气响应的MainActivity.java类下面
public class MainActivity extends AppCompatActivity {
private List<Weather> weatherArrayList;
private List<ForecastEndingPoint> conditionList;
private ProgressDialog pDialog;
private RecyclerView recyclerView;
private WeatherAdapter adapter;
private ConditionAdapter conditionAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading Data.. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
//Creating an object of our api interface
ApiService api = WeatherClient.getApiService();
/**
* Calling JSON
*/
Call<List<Weather>> call = api.getWeather("6bed69052a864d44a8e165653183008", "Andijan");
Call<ForecastEndingPoint> conditionCall = api.forecast("6bed69052a864d44a8e165653183008", "Andijan", 10);
/**
* Enqueue Callback will be call when get response...
*/
call.enqueue(new Callback<List<Weather>>() {
@Override
public void onResponse(Call<List<Weather>> call, Response<List<Weather>> response) {
//Dismiss Dialog
// Log.d("url", call.request().url().toString());
pDialog.dismiss();
if (response.isSuccessful()) {
/**
* Got Successfully
*/
weatherArrayList = response.body();
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
LinearLayoutManager eLayoutManager = null;
adapter = new WeatherAdapter((List<Weather>) weatherArrayList);
eLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(eLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
}
}
@Override
public void onFailure(Call<List<Weather>> call, Throwable t) {
// Log.d("error", "" + t.getMessage());
pDialog.dismiss();
}
});
conditionCall.enqueue(new Callback<ForecastEndingPoint>() {
@Override
public void onResponse(Call<ForecastEndingPoint> conditionCall, Response<ForecastEndingPoint> response) {
//Dismiss Dialog
// Log.d("url", call.request().url().toString());
if (pDialog!=null && pDialog.isShowing()){
pDialog.dismiss();
}
if (response.isSuccessful()) {
/**
* Got Successfully
*/
conditionList = Collections.singletonList(response.body());
recyclerView = findViewById(R.id.recycler_view);
LinearLayoutManager eLayoutManager = null;
Context context = getApplicationContext();
Context picassoContext = getApplicationContext();
conditionAdapter = new ConditionAdapter( conditionList, context,picassoContext );
eLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(eLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(conditionAdapter);
}
}
@Override
public void onFailure(Call<ForecastEndingPoint> conditionCall, Throwable t) {
pDialog.dismiss();
}
});
}
}
对天气情况的响应不足
{
"location": {
"name": "Andijan",
"region": "Andijon",
"country": "Uzbekistan",
"lat": 40.78,
"lon": 72.34,
"tz_id": "Asia/Tashkent",
"localtime_epoch": 1535973113,
"localtime": "2018-09-03 16:11"
},
"current": {
"last_updated_epoch": 1535972213,
"last_updated": "2018-09-03 15:56",
"temp_c": 30.5,
"temp_f": 86.9,
"is_day": 1,
"condition": {
"text": "Partly cloudy",
"icon": "//cdn.apixu.com/weather/64x64/day/116.png",
"code": 1003
},
"wind_mph": 3.8,
"wind_kph": 6.1,
"wind_degree": 284,
"wind_dir": "WNW",
"pressure_mb": 1015,
"pressure_in": 30.4,
"precip_mm": 0,
"precip_in": 0,
"humidity": 17,
"cloud": 1,
"feelslike_c": 28.2,
"feelslike_f": 82.8,
"vis_km": 20,
"vis_miles": 12
},
"forecast": {
"forecastday": [
{
"date": "2018-09-03",
"date_epoch": 1535932800,
"day": {
"maxtemp_c": 32,
"maxtemp_f": 89.6,
"mintemp_c": 19.6,
"mintemp_f": 67.3,
"avgtemp_c": 26.3,
"avgtemp_f": 79.4,
"maxwind_mph": 4.7,
"maxwind_kph": 7.6,
"totalprecip_mm": 0,
"totalprecip_in": 0,
"avgvis_km": 17.5,
"avgvis_miles": 10,
"avghumidity": 23,
"condition": {
"text": "Partly cloudy",
"icon": "//cdn.apixu.com/weather/64x64/day/116.png",
"code": 1003
},
"uv": 7.3
},
"astro": {
"sunrise": "05:39 AM",
"sunset": "06:40 PM",
"moonrise": "11:26 PM",
"moonset": "01:19 PM"
}
}
]
}
}
**below json response for city name and region**
{
"id": 2679267,
"name": "Andidzhan, Andijon, Uzbekistan",
"region": "Andijon",
"country": "Uzbekistan",
"lat": 40.78,
"lon": 72.34,
"url": "andidzhan-andijon-uzbekistan"
},
{
"id": 2679268,
"name": "Andijan, Andijon, Uzbekistan",
"region": "Andijon",
"country": "Uzbekistan",
"lat": 40.78,
"lon": 72.34,
"url": "andijan-andijon-uzbekistan"
},
{
"id": 2681416,
"name": "Khakan, Andijon, Uzbekistan",
"region": "Andijon",
"country": "Uzbekistan",
"lat": 40.75,
"lon": 72.37,
"url": "khakan-andijon-uzbekistan"
},
{
"id": 2683610,
"name": "Stantsiya Andizhan Vtoroy, Andijon, Uzbekistan",
"region": "Andijon",
"country": "Uzbekistan",
"lat": 40.82,
"lon": 72.37,
"url": "stantsiya-andizhan-vtoroy-andijon-uzbekistan"
},
{
"id": 2679273,
"name": "Andizhan Vtoroy, Andijon, Uzbekistan",
"region": "Andijon",
"country": "Uzbekistan",
"lat": 40.82,
"lon": 72.37,
"url": "andizhan-vtoroy-andijon-uzbekistan"
},
{
"id": 2680107,
"name": "Dal'verzin, Andijon, Uzbekistan",
"region": "Andijon",
"country": "Uzbekistan",
"lat": 40.77,
"lon": 72.28,
"url": "dalverzin-andijon-uzbekistan"
},
{
"id": 2679050,
"name": "Agullik, Andijon, Uzbekistan",
"region": "Andijon",
"country": "Uzbekistan",
"lat": 40.73,
"lon": 72.35,
"url": "agullik-andijon-uzbekistan"
},
{
"id": 2681417,
"name": "Khaken, Andijon, Uzbekistan",
"region": "Andijon",
"country": "Uzbekistan",
"lat": 40.73,
"lon": 72.37,
"url": "khaken-andijon-uzbekistan"
},
{
"id": 2680130,
"name": "Darkhan, Andijon, Uzbekistan",
"region": "Andijon",
"country": "Uzbekistan",
"lat": 40.73,
"lon": 72.37,
"url": "darkhan-andijon-uzbekistan"
},
{
"id": 1315502,
"name": "Dzhalimbek, Osh, Kyrghyzstan",
"region": "Osh",
"country": "Kyrghyzstan",
"lat": 40.73,
"lon": 72.32,
"url": "dzhalimbek-osh-kyrghyzstan"
}
]
答案 0 :(得分:0)
您需要更正图标位置的网址。
final String icon = forecastEndingPoint.getCurrent()
.getCondition().getIcon().replace("//", "");
Picasso.get().load(icon)
.fit().centerCrop().into(holder.weatherIcon);
如果Picasso缺少协议,则可能需要添加。
final String icon = "http:" + forecastEndingPoint.getCurrent()
.getCondition().getIcon();
Picasso.get().load(icon)
.fit().centerCrop().into(holder.weatherIcon);