0

我正在使用 SwipeRefreshLayout 远程获取数据,我使用 AsyncHttpClient 来处理网络方面的事情。

一切都按预期工作,除了一个场景:应用程序的第一次 onCreate 启动。

主要活动:

public class MainActivity extends Activity implements AdapterView.OnItemClickListener, SwipeRefreshLayout.OnRefreshListener{

    ListView mainListView;
    JSONMainAdapter mJSONAdapter;
    SwipeRefreshLayout swipeLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        swipeLayout = (SwipeRefreshLayout) findViewById(R.id.main_swipe_container);
        swipeLayout.setOnRefreshListener(this);
        swipeLayout.setColorScheme(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);

        mainListView = (ListView) findViewById(R.id.main_listview);
        mainListView.setOnItemClickListener(this);

        mJSONAdapter = new JSONMainAdapter(this, getLayoutInflater());
        mainListView.setAdapter(mJSONAdapter);

        getGames();
    }

onRefresh:

@Override 
public void onRefresh() {
    getGames();
}

进行网络提升的getGames:

private void getGames() {

        swipeLayout.setRefreshing(true);
        //gets logged on first onCreate load
        Log.d("DEBUG", "IN GET GAMES");

        MyRestClient.get("games", null, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(JSONObject jsonObject) {
                //does NOT get logged on first onCreate
                //but does get logged on refresh and other onCreate calls 
                Log.d("DEBUG", "IN GET GAMES ON SUCCESS");
                swipeLayout.setRefreshing(false);
                Toast.makeText(getApplicationContext(), "Success!", Toast.LENGTH_LONG).show();
                mJSONAdapter.updateData(jsonObject.optJSONArray("games"));
            }

            @Override
            public void onFailure(int statusCode, Throwable throwable, JSONObject error) {
                Log.d("DEBUG", "IN GET GAMES ON FAIL");
                swipeLayout.setRefreshing(false);
                Toast.makeText(getApplicationContext(), "Error: " + statusCode + " " + throwable.getMessage(), Toast.LENGTH_LONG).show();
                Log.e("ERROR", statusCode + " " + throwable.getMessage());
            }
        });
    }

因此,当我从我的 IDE 启动应用程序时,onCreate调用该getGames()函数并记录它进入那里,但网络MyRestClient.get("games", null, new JsonHttpResponseHandler() {没有被调用,因为它不获取数据并且从不登录 onSuccess 或 onFailure。

现在,如果我滑动刷新它可以工作并成功地用获取的数据填充我的列表视图并记录它在 onSuccess 中。

如果我通过后退按钮关闭应用程序并重新打开它可以正常工作并刷新我的列表视图。

因此,当从我的 IDE 启动到模拟器时,它不仅适用于第一个 onCreate 调用——但在那之后,第一个初始 onCreate 不能正常工作。

我不确定我在这里缺少什么,感谢您的帮助,谢谢。

SwipeRefreshLayout: https ://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html

AsyncHttpClient: http ://loopj.com/android-async-http/

编辑:

public class MyRestClient {
    private static final String BASE_URL = "http://10.10.1.10:3000/api/";

    private static AsyncHttpClient client = new AsyncHttpClient();

    public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
        addAuthHeaders();
        client.get(getAbsoluteUrl(url), params, responseHandler);
    }

    public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
        addAuthHeaders();
        client.post(getAbsoluteUrl(url), params, responseHandler);
    }

    private static String getAbsoluteUrl(String relativeUrl) {
        return BASE_URL + relativeUrl;
    }

    public static void addAuthHeaders(){
        client.addHeader("blah", "xxx");
        client.addHeader("blah", "xxx");
    }
}

编辑2:

> 05-05 08:51:20.477: E/memtrack(1696): Couldn't load memtrack module
> (No such file or directory) 05-05 08:51:20.477:
> E/android.os.Debug(1696): failed to load memtrack module: -2 05-05
> 08:51:20.857: D/AndroidRuntime(1696): Calling main entry
> com.android.commands.am.Am 05-05 08:51:20.937: I/ActivityManager(380):
> START u0 {act=android.intent.action.MAIN
> cat=[android.intent.category.LAUNCHER] flg=0x10000000
> cmp=com.example.myapp.app/.MainActivity} from pid 1696 05-05
> 08:51:21.047: D/gralloc(49): Registering a buffer in the process that
> created it. This may cause memory ordering problems. 05-05
> 08:51:21.047: E/libEGL(49): called unimplemented OpenGL ES API 05-05
> 08:51:21.057: E/libEGL(49): called unimplemented OpenGL ES API 05-05
> 08:51:21.057: E/libEGL(49): called unimplemented OpenGL ES API 05-05
> 08:51:21.057: E/libEGL(49): called unimplemented OpenGL ES API 05-05
> 08:51:21.057: E/SurfaceFlinger(49): glCheckFramebufferStatusOES error
> 1620973071 05-05 08:51:21.057: E/SurfaceFlinger(49): got
> GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot 05-05
> 08:51:21.057: E/libEGL(49): called unimplemented OpenGL ES API 05-05
> 08:51:21.057: E/libEGL(49): called unimplemented OpenGL ES API 05-05
> 08:51:21.067: W/WindowManager(380): Screenshot failure taking
> screenshot for (328x546) to layer 21005

这是应用程序启动时我的日志中唯一看起来与众不同的部分。

4

2 回答 2

0

你的意思是, onCreate 它不调用 getGames() 吗?您是否尝试将 getGames() 放入 onResume() ?

于 2014-05-06T02:05:42.050 回答
0

经过进一步测试,我注意到它只发生在模拟器上。

启动到真实设备时,应用程序会按预期响应。在这一点上,我将其归结为与模拟器相关的问题,但如果有任何具体的发展,我会保持更新此线程。

于 2014-05-07T15:19:36.687 回答