AsyncTask onPost执行正在运行,但doInBackground不运行

时间:2016-04-25 20:01:23

标签: java android xml debugging android-asynctask

我有一个问题。我正在开发一个Android应用程序,在我的活动中,我正在使用asyncTask。我从onCreate调用任务并在onPostExecute中更新我的UI。我有一个listView,在asyncTask中我执行Http请求以从服务器获取信息。但我的问题是,有时在运行活动时我没有得到任何输出。

我尝试过调试,似乎我从onPostExecute获取了一个打印,但是没有来自doInBackground。如果相关,我也会使用Google Cloud Endpoints进行Http请求。

请帮忙。

这是我的班级:

public class ILiked extends CustomFragment {
public ArrayList<User> ilikedList = new ArrayList<>();
ListView userList;
MyAdapter adapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View v = inflater.inflate(R.layout.fragment_iliked, null);
    ILikedAsyncTask task = new ILikedAsyncTask();
    task.execute(new Pair<>(getShared().getString("username", ""), v));
    return v;
}

public SharedPreferences getShared() {
    SharedPreferences shared;
    if (Login.sharedPreferences != null) {
        shared = this.getActivity().getSharedPreferences(Login.MyPreferences, Context.MODE_PRIVATE);
    } else {
        shared = this.getActivity().getSharedPreferences(Signup.MyPreferences, Context.MODE_PRIVATE);
    }
    return shared;
}

public class ILikedAsyncTask extends AsyncTask<Pair<String, View>, Void, ArrayList<User>> {
    private UserApi myApiService = null;
    View context;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        System.out.println("I AM IN PRE EXECUTE");
    }

    @Override
    protected ArrayList<User> doInBackground(Pair<String, View>... params) {
        context = params[0].second;
        if (myApiService == null) {  
            UserApi.Builder builder = new UserApi.Builder(AndroidHttp.newCompatibleTransport(),
                    new AndroidJsonFactory(), null)

                    .setRootUrl("MYURL")
                    .setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
                        @Override
                        public void initialize(AbstractGoogleClientRequest<?> abstractGoogleClientRequest) throws IOException {
                            abstractGoogleClientRequest.setDisableGZipContent(true);
                        }
                    });

            myApiService = builder.build();
        }

        ArrayList<User> list = new ArrayList<>();
        try {
            String username = params[0].first;
            UserCollection users = myApiService.getUsersILiked(username).execute();
            System.out.println("CONTENT" + users.getItems().toString());
            if (users != null) {
                List<User> userList = users.getItems();
                list.addAll(userList);
            }

        } catch (IOException e) {

        }
        return list;

    }


    @Override
    protected void onPostExecute(final ArrayList<User> result) {
        super.onPostExecute(result);
        userList = (ListView) context.findViewById(R.id.iliked_list);

        adapter = new MyAdapter(context.getContext(), result);
        userList.setAdapter(adapter);
        //System.out.println("List Content: "+result.get(0));
        userList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Profile fr = new Profile();
                FragmentManager fm = getFragmentManager();
                FragmentTransaction ft = fm.beginTransaction();
                Bundle args = new Bundle();
                args.putString("username", result.get(position).getUsername());
                fr.setArguments(args);
                ft.replace(R.id.content_frame, fr);
                ft.commit();

            }
        });
        System.out.println("DID THE ASYNC");
    }
}

}

1 个答案:

答案 0 :(得分:0)

可能只是简单的 myApiService 引发了一些 IOException 。 您捕获 IOExceptions 但不打印任何日志或堆栈跟踪。让我们 先检查一下这个。

 catch (IOException e) {

        }

如果每次都有来自 onPostExecute 的日志,那么就会调用 doInBackground 当然(Android就是这样)。

无论如何你应该删除所有这些

    userList = (ListView) context.findViewById(R.id.iliked_list);

    adapter = new MyAdapter(context.getContext(), result);
    userList.setAdapter(adapter);
    //System.out.println("List Content: "+result.get(0));
    userList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Profile fr = new Profile();
            FragmentManager fm = getFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            Bundle args = new Bundle();
            args.putString("username", result.get(position).getUsername());
            fr.setArguments(args);
            ft.replace(R.id.content_frame, fr);
            ft.commit();

        }
    });

来自onPostExecute并在onCreate或代码的其他部分进行初始化。在onPostExecute中,您应该只更新所需的UI元素,notifyAdapters等。应该提前完成初始化。考虑一下这种方法。