异步任务中的 doinbackground() 无法正常工作
Posted
技术标签:
【中文标题】异步任务中的 doinbackground() 无法正常工作【英文标题】:doinbackground() in async task not working properly 【发布时间】:2019-03-22 05:14:10 【问题描述】:我正在从 API 获取一些数据并将每个项目显示为 RecyclerViewPager slide
。所以,我在onScrollListener
中创建了一个async task
来检查滚动动作,当用户滑动5 个项目时asynctask
获取第2 页的项目,在doInBackGround
函数中,数据将被添加到数组和数组中将被添加到recycler view pager
。
一切正常,但我需要在 5 幻灯片停止滚动几秒钟(至少 5 秒),否则 recyclerView
将不会显示第二页幻灯片。
如何解决这个问题。这是我用于onScrollListener
和asyncTask
函数的代码。
代码:
private void initRecycler()
final RecyclerViewPager mRecyclerView = (RecyclerViewPager) findViewById(R.id.list);
// setLayoutManager like normal RecyclerView, you do not need to change any thing.
final LinearLayoutManager layout = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);
mRecyclerView.setLayoutManager(layout);
//set adapter
//You just need to implement ViewPageAdapter by yourself like a normal RecyclerView.Adpater.
RecyclerViewAdapter adapter = new RecyclerViewAdapter(ImageUrls, ImageNames, ImageDesc, this);
mRecyclerView.setAdapter(adapter);
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener()
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy)
super.onScrolled(recyclerView, dx, dy);
visibleItemCount = layout.getChildCount();
totalItemCount = layout.getItemCount();
firstVisibleItem = layout.findFirstVisibleItemPosition();
// int aaa = totalItemCount - visibleItemCount;
// int bbb = firstVisibleItem + visibleThreshold;
// Log.d("tag" ,"totalItemCount : " + totalItemCount + "| visibleItemCount : " + visibleItemCount);
Log.d("tag" ,"totalItemCount : " + totalItemCount + "| previousTotal : " + previousTotal);
Log.d("tag", "firstVisibleItem :" + firstVisibleItem + " | totalItemCount :" + totalItemCount);
if (loading)
if (totalItemCount > previousTotal)
loading = false;
previousTotal = totalItemCount;
if (!loading && (firstVisibleItem + 5)
== (totalItemCount))
Log.i("Ya!", "end called");
page += 1;
String url = "http://url.in/wp-json/wp/v2/posts?page="+page+"&_embed";
Log.d( url ,": ");
// Do something
final OkHttpClient client2 = new OkHttpClient();
final Request request2 = new Request.Builder()
.url(url)
.build();
@SuppressLint("StaticFieldLeak") AsyncTask<Void, Void, String> asyncTask = new AsyncTask<Void, Void, String>()
private static final String TAG = "SlideFragment";
@Override
protected String doInBackground(Void... params)
try
Response response2 = client2.newCall(request2).execute();
if (!response2.isSuccessful())
Log.d(TAG, "doInBackground: REsponse Un Successfull - 56");
response2.body().close();
return null;
String Data2 = response2.body().string();
response2.body().close();
return Data2;
catch (Exception e)
e.printStackTrace();
Log.d(TAG, "doInBackground: Exceptione on line63");
return null;
@Override
protected void onPostExecute(String Data2)
super.onPostExecute(Data2);
if (Data2 != null)
Log.d(TAG, "onPostExecute: 188 LINE");
try
JSONArray json = new JSONArray(Data2);
for (int i = 0; i < json.length(); i++)
JSONObject post = json.getJSONObject(i);
String title = post.getJSONObject("title").getString("rendered");
String description = post.getJSONObject("excerpt").getString("rendered");
String imgURL = post.getJSONObject("_embedded").getJSONArray("wp:featuredmedia").getJSONObject(0).getJSONObject("media_details").getString("file");
String imagUrl = "http://url.in/wp-content/uploads/" + imgURL;
ImageNames.add(title);
ImageDesc.add(description);
ImageUrls.add(imagUrl);
Log.d(TAG, "onPostExecute: arrayList Created" );
catch(JSONException j)
j.printStackTrace();
Log.d(TAG, "onPostExecute: on line 121");
;
asyncTask.execute();
loading = true;
);
【问题讨论】:
【参考方案1】:我猜你应该在发出另一个请求之前手动取消任务,因为根据文档中给出的规则,一个任务只能执行一次
【讨论】:
以上是关于异步任务中的 doinbackground() 无法正常工作的主要内容,如果未能解决你的问题,请参考以下文章
异步任务HttpPost请求DoInbackground调用web api Android Studio Java,如何添加正文字符串数组?