android 中的 AsyncTaskLoader 问题
AsyncTaskLoader issue in android
您好,我在我的应用程序中使用了 AsyncTaskLoader,并且我已经在 MovieTaskLoader class 中实现了它,但是当我在我的片段中实现 Loader 回调时,我在 onCreateLoader() 方法中遇到了类型转换错误。
MovieTaskLoader class:
class MovieTaskLoader extends AsyncTaskLoader<ArrayList<Movies>> {
MovieTaskLoader(Context context) {
super(context);
}
@Override
protected void onStartLoading() {
forceLoad();
}
@Override
public ArrayList<Movies> loadInBackground() {
//Building URL
URL url = null;
String jsonData = null;
Uri uri = Uri.parse(Utility.BASE_URL).buildUpon()
.appendPath("popular")
.appendQueryParameter("api_key", BuildConfig.api_key)
.build();
try {
url = new URL(uri.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
}
//Downloading json data
HttpURLConnection urlConnection;
try {
assert url != null;
urlConnection = (HttpURLConnection) url.openConnection();
if(urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK){
jsonData = readStream(urlConnection.getInputStream());
}
} catch (IOException e) {
e.printStackTrace();
}
return getMovieData(jsonData);
}
private String readStream(InputStream in) {
BufferedReader reader = null;
StringBuilder response = new StringBuilder();
try {
reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return response.toString();
}
private ArrayList<Movies> getMovieData(String jsonData){
ArrayList<Movies> movies = new ArrayList<>();
try {
JSONObject jsonObject = new JSONObject(jsonData);
JSONArray results = jsonObject.getJSONArray("results");
for(int i=0; i<20; i++){
Movies movie = new Movies();
JSONObject object = results.getJSONObject(i);
movie.setTitle(object.getString("title"));
movie.setSynopsis("overview");
movie.setVote_avg("vote_average");
movie.setDate("release_date");
movies.add(movie);
}
} catch (JSONException e) {
e.printStackTrace();
}
return movies;
}
}
片段class:
public class PopularFragment extends Fragment implements LoaderManager.LoaderCallbacks<ArrayList<Movies>> {
public PopularFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_popular, container, false);
RecyclerView popularView = (RecyclerView) view.findViewById(R.id.popular_view);
GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 2);
popularView.setLayoutManager(layoutManager);
popularView.setAdapter(new RecyclerViewAdapter());
return view;
}
@Override
public Loader<ArrayList<Movies>> onCreateLoader(int id, Bundle args) {
return new MovieTaskLoader(getContext());
}
@Override
public void onLoadFinished(Loader<ArrayList<Movies>> loader, ArrayList<Movies> data) {
}
@Override
public void onLoaderReset(Loader<ArrayList<Movies>> loader) {
}
}
我在 onCreateLoader() 方法中遇到错误,类型转换错误是:
必填:"Loader"
找到:MovieTaskLoader
检查您的导入。确保两者都是相同的版本
android.support.v4.content.AsyncTaskLoader<D>
android.support.v4.app.LoaderManager.LoaderCallbacks<D>
您好,我在我的应用程序中使用了 AsyncTaskLoader,并且我已经在 MovieTaskLoader class 中实现了它,但是当我在我的片段中实现 Loader 回调时,我在 onCreateLoader() 方法中遇到了类型转换错误。 MovieTaskLoader class:
class MovieTaskLoader extends AsyncTaskLoader<ArrayList<Movies>> {
MovieTaskLoader(Context context) {
super(context);
}
@Override
protected void onStartLoading() {
forceLoad();
}
@Override
public ArrayList<Movies> loadInBackground() {
//Building URL
URL url = null;
String jsonData = null;
Uri uri = Uri.parse(Utility.BASE_URL).buildUpon()
.appendPath("popular")
.appendQueryParameter("api_key", BuildConfig.api_key)
.build();
try {
url = new URL(uri.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
}
//Downloading json data
HttpURLConnection urlConnection;
try {
assert url != null;
urlConnection = (HttpURLConnection) url.openConnection();
if(urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK){
jsonData = readStream(urlConnection.getInputStream());
}
} catch (IOException e) {
e.printStackTrace();
}
return getMovieData(jsonData);
}
private String readStream(InputStream in) {
BufferedReader reader = null;
StringBuilder response = new StringBuilder();
try {
reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return response.toString();
}
private ArrayList<Movies> getMovieData(String jsonData){
ArrayList<Movies> movies = new ArrayList<>();
try {
JSONObject jsonObject = new JSONObject(jsonData);
JSONArray results = jsonObject.getJSONArray("results");
for(int i=0; i<20; i++){
Movies movie = new Movies();
JSONObject object = results.getJSONObject(i);
movie.setTitle(object.getString("title"));
movie.setSynopsis("overview");
movie.setVote_avg("vote_average");
movie.setDate("release_date");
movies.add(movie);
}
} catch (JSONException e) {
e.printStackTrace();
}
return movies;
}
}
片段class:
public class PopularFragment extends Fragment implements LoaderManager.LoaderCallbacks<ArrayList<Movies>> {
public PopularFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_popular, container, false);
RecyclerView popularView = (RecyclerView) view.findViewById(R.id.popular_view);
GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 2);
popularView.setLayoutManager(layoutManager);
popularView.setAdapter(new RecyclerViewAdapter());
return view;
}
@Override
public Loader<ArrayList<Movies>> onCreateLoader(int id, Bundle args) {
return new MovieTaskLoader(getContext());
}
@Override
public void onLoadFinished(Loader<ArrayList<Movies>> loader, ArrayList<Movies> data) {
}
@Override
public void onLoaderReset(Loader<ArrayList<Movies>> loader) {
}
}
我在 onCreateLoader() 方法中遇到错误,类型转换错误是:
必填:"Loader"
找到:MovieTaskLoader
检查您的导入。确保两者都是相同的版本
android.support.v4.content.AsyncTaskLoader<D>
android.support.v4.app.LoaderManager.LoaderCallbacks<D>