我有一些Json
数据,我希望使用Json
接收Retrofit
个数据,并希望在我的RecyclerView
中显示。
这是我的Pojo
班级
public class Movie implements Parcelable {
@SerializedName("title")
public String title;
@SerializedName("movie_name")
public String movieName;
@SerializedName("link")
public String link;
protected Movie(Parcel in) {
title = in.readString();
movieName = in.readString();
link = in.readString();
}
public Movie(String title , String movieName , String link){
this.title = title;
this.movieName = movieName;
this.link = link;
}
public static final Creator<Movie> CREATOR = new Creator<Movie>() {
@Override
public Movie createFromParcel(Parcel in) {
return new Movie(in);
}
@Override
public Movie[] newArray(int size) {
return new Movie[size];
}
};
public String getLink() {
return link;
}
public String getMovieName() {
return movieName;
}
public String getTitle() {
return title;
}
public void setLink(String link) {
this.link = link;
}
public void setMovieName(String movieName) {
this.movieName = movieName;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(title);
dest.writeString(movieName);
dest.writeString(link);
}
}
这是我的回复课。
public class Movies {
@SerializedName("results")
private ArrayList<Movie> movies = new ArrayList<>();
public ArrayList<Movie> getMovies() {
return movies;
}
}
My Api Interface class
public interface ApiInterFace {
@GET("sort_by")
Call<Movies> discoverSongList(@Path("sort_by") String sort_by);
}
我的获取数据类
public class FetchSongList extends AsyncTask<String , Void , List<Movie>>{
public static final String TAG_NAME = FetchSongList.class.getSimpleName();
public Lisener lisener;
public FetchSongList(Lisener lisener){
this.lisener = lisener;
}
@Override
protected List<Movie> doInBackground(String... params) {
if (params.length==0){
return null;
}
String name = params[0];
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://banglahdnatok.com/Hindi%20Movie%20Songs/")
.addConverterFactory(GsonConverterFactory.create()).build();
ApiInterFace apiInterFace = retrofit.create(ApiInterFace.class);
Call<Movies> call = apiInterFace.discoverSongList(name);
try{
Response<Movies> response = call.execute();
Movies movies = response.body();
return movies.getMovies();
}
catch (IOException e){
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(List<Movie> movies) {
super.onPostExecute(movies);
if (movies!=null){
lisener.onFetchLisener(movies);
}
else {
lisener.onFetchLisener(new ArrayList<Movie>());
}
}
public interface Lisener{
void onFetchLisener(List<Movie> movies);
}
}
这是我的MainActivity类
public class MainActivity extends AppCompatActivity implements FetchSongList.Lisener{
public RecyclerView recyclerView;
public MyRecycleViewAdapter adapter;
public ArrayList<Movie> movieArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
fetch("just_added");
adapter = new MyRecycleViewAdapter(this , movieArrayList);
}
public void fetch(String name){
FetchSongList fetchSongList = new FetchSongList(this);
fetchSongList.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, name);
}
@Override
public void onFetchLisener(List<Movie> movies) {
adapter.add(movieArrayList);
}
}
当我运行我的应用程序时,它会显示一些错误
FATAL EXCEPTION: AsyncTask #1 java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:864)
Caused by: java.lang.IllegalArgumentException: URL "sort_by" does not contain "{sort_by}". (parameter #1)
我如何解决这个问题?
答案 0 :(得分:2)
而不是每次都可以在主线程上运行这个新的AsyncTask,而Retrofit本身在后台线程上运行网络调用。 以下是示例代码:
POST /session
希望它能解决问题。