我尝试过调试,似乎我从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");
}
}
}
答案 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等。应该提前完成初始化。考虑一下这种方法。