Я новичок в разработке для Android и пытаюсь разобрать сложную структуру JSON с помощью библиотеки rertofit из http://api.tvmaze.com/search/shows?q=girls. Но мой проект не загружает данные.Я полагаю, что проблема в модели GSON, но я в полном недоумении, что именно не так.Мне нужно получить только изображение и заголовок. Я уже включил интернет-разрешение в ответ манифеста:
[
{
"score": 17.314238,
"show": {
"id": 139,
"url": "http://www.tvmaze.com/shows/139/girls",
"name": "Girls",
"type": "Scripted",
"language": "English",
"genres": [
"Drama",
"Romance"
],
"status": "Ended",
"runtime": 30,
"premiered": "2012-04-15",
"officialSite": "http://www.hbo.com/girls",
"schedule": {
"time": "22:00",
"days": [
"Sunday"
]
},
"rating": {
"average": 6.7
},
"weight": 94,
"network": {
"id": 8,
"name": "HBO",
"country": {
"name": "United States",
"code": "US",
"timezone": "America/New_York"
}
},
"webChannel": null,
"externals": {
"tvrage": 30124,
"thetvdb": 220411,
"imdb": "tt1723816"
},
"image": {
"medium": "http://static.tvmaze.com/uploads/images/medium_portrait/31/78286.jpg",
"original": "http://static.tvmaze.com/uploads/images/original_untouched/31/78286.jpg"
},
"summary": "<p>This Emmy winning series is a comic look at the assorted humiliations and rare triumphs of a group of girls in their 20s.</p>",
"updated": 1538265422,
"_links": {
"self": {
"href": "http://api.tvmaze.com/shows/139"
},
"previousepisode": {
"href": "http://api.tvmaze.com/episodes/1079686"
}
}
}
},
{
"score": 13.229476,
"show": {
"id": 23542,
"url": "http://www.tvmaze.com/shows/23542/good-girls",
"name": "Good Girls",
"type": "Scripted",
"language": "English",
"genres": [
"Drama",
"Comedy",
"Crime"
],
"status": "Running",
"runtime": 60,
"premiered": "2018-02-26",
"officialSite": "https://www.nbc.com/good-girls?nbc=1",
"schedule": {
"time": "22:00",
"days": [
"Monday"
]
},
"rating": {
"average": 7
},
"weight": 92,
"network": {
"id": 1,
"name": "NBC",
"country": {
"name": "United States",
"code": "US",
"timezone": "America/New_York"
}
},
"webChannel": null,
"externals": {
"tvrage": null,
"thetvdb": 328577,
"imdb": "tt6474378"
},
"image": {
"medium": "http://static.tvmaze.com/uploads/images/medium_portrait/141/354343.jpg",
"original": "http://static.tvmaze.com/uploads/images/original_untouched/141/354343.jpg"
},
"summary": "<p><b>Good Girls</b> follows three \"good girl\" suburban wives and mothers who suddenly find themselves in desperate circumstances and decide to stop playing it safe, and risk everything to take their power back.</p>",
"updated": 1532345475,
"_links": {
"self": {
"href": "http://api.tvmaze.com/shows/23542"
},
"previousepisode": {
"href": "http://api.tvmaze.com/episodes/1425058"
}
}
}
},
......
]
Я строю свой класс модели для получения изображения и заголовка, потому что мне нужно только два элемента: общедоступный класс Имидж:
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Image {
@SerializedName("medium")
@Expose
private String medium;
@SerializedName("original")
@Expose
private String original;
public String getMedium() {
return medium;
}
public void setMedium(String medium) {
this.medium = medium;
}
public String getOriginal() {
return original;
}
public void setOriginal(String original) {
this.original = original;
}
}
общедоступный фильмМоя основная деятельность:
public class MainActivity extends AppCompatActivity {
private SearchView searchView;
private RecyclerView recyclerView;
private MovieAdapter movieAdapter;
private List<Movie> movieList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
movieList = new ArrayList<>();
recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
movieAdapter = new MovieAdapter();
recyclerView.setAdapter(movieAdapter);
ApiInterface apiService = TvMazeApiClient.getClient().create(ApiInterface.class);
Call<List<Movie>> call = apiService.getMovies();
call.enqueue(new Callback<List<Movie>>() {
@Override
public void onResponse(Call<List<Movie>> call, Response<List<Movie>> response) {
if(movieList != null) {
movieList = response.body();
Log.d("TAG", "Response = " + movieList);
movieAdapter.setMovieList(getApplicationContext(), movieList);
}
else{
Toast.makeText(MainActivity.this,"error", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<List<Movie>> call, Throwable t) {
Log.d("TAG","Response = "+t.toString());
Toast.makeText(MainActivity.this,t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.options_menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.action_search)
.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
searchView.setMaxWidth(Integer.MAX_VALUE);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
movieAdapter.getFilter().filter(query);
return false;
}
@Override
public boolean onQueryTextChange(String query) {
movieAdapter.getFilter().filter(query);
return false;
}
});
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_search) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
if (!searchView.isIconified()) {
searchView.setIconified(true);
return;
}
super.onBackPressed();
}}
мой xml-файл для элемента:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="wrap_content"
>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="160dp"
android:orientation="horizontal"
android:padding="8dp">
<ImageView
android:id="@+id/image"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_marginRight="8dp"
android:scaleType="fitXY" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:textSize="16sp"
android:textStyle="bold"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
Интерфейс:
public interface ApiInterface {
@GET("search/shows?q=girls")
Call <List<Movie>> getMovies();
}
Клиент:
public class TvMazeApiClient {
public static String BASE_URL ="http://api.tvmaze.com/";
private static Retrofit retrofit;
public static Retrofit getClient(){
if(retrofit == null){
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
Спасибозаранее за любую помощь