0

我正在尝试从服务器获取使用改造的 android 应用程序的回复。我设法发送请求没有问题,但回调似乎总是为空,而且它似乎永远不会失败或成功,这听起来很奇怪。我不明白的其他事情是改造日志确实显示了响应,但回调变量似乎没有得到它们?!

我打算每隔几秒钟轮询一次服务器以检查是否发生了变化,我不确定如何执行此操作,所以我尝试一遍又一遍地发送请求但没有运气,并且使用 Observable 不会似乎也可以,所以我只是使用异步回调(同步会很好,但 android 似乎不太喜欢它)。

以下是一些可能有用的代码部分:get 接口:

interface CamFindGet {
    @Headers("Authorization: CloudSight XXX")
    @GET("/image_responses/{token}")
    void getResult(@Path("token") String token,
                   Callback<resultClassGet> callback);}

其余适配器:

final RestAdapter restAdapter = new RestAdapter.Builder()
            .setEndpoint(API_URL)
            .setLogLevel(RestAdapter.LogLevel.FULL)
            .setLog(new RestAdapter.Log() {
                @Override
                public void log(String msg) {
                    String[] blacklist = {"Access-Control", "Cache-Control", "Connection", "Content-Type", "Keep-Alive", "Pragma", "Server", "Vary", "X-Powered-By"};
                    for (String bString : blacklist) {
                        if (msg.startsWith(bString)) {
                            return;
                        }
                    }
                    Log.d("Retrofit", msg);
                }
            }).build();

CamFindGet camFindGet = restAdapter.create(CamFindGet.class);

                try { //NOTE : result.getToken() is a valid token
                    getCamResult(camFindGet, result.getToken());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

实际的 GET 请求:

static void getCamResult(CamFindGet camFindGet, String token) throws InterruptedException {
    Log.d(TAG, "GET CAM RESULT");


        Log.d(TAG, "LOOP");
        camFindGet.getResult(token, new Callback<resultClassGet>() {
            @Override
            public void success(resultClassGet resultClass, Response response) {
                //Log.d(TAG, resultClass.getName());

                if (resultClass.getStatus().equals("completed") || resultClass.getStatus().equals("not found"))
                    completedGet = 1;

                if(completedGet==1) {
                    resultString = new String(resultClass.getName());
                    Log.d(TAG, "RESULT : "+resultString);
                }
                Log.d(TAG, resultClass.getStatus());
            }

            @Override
            public void failure(RetrofitError retrofitError) {
                Log.d(TAG, retrofitError.getMessage());
                Log.d(TAG, "FAILURE?");
            }
        });

}

最后,日志:

07-01 17:20:23.838    4626-4650/app D/Retrofit﹕ ---> HTTP GET https://api.cloudsightapi.com/image_responses/dUYO5-77ETRJx4bzjL6LEw
07-01 17:20:23.838    4626-4650/app D/Retrofit﹕ Authorization: CloudSight XXX
07-01 17:20:23.840    4626-4650/app D/Retrofit﹕ ---> END HTTP (no body)
07-01 17:20:23.984    4626-4650/app D/Retrofit﹕ <--- HTTP 200 https://api.cloudsightapi.com/image_responses/dUYO5-77ETRJx4bzjL6LEw (143ms)
07-01 17:20:23.984    4626-4650/app D/Retrofit﹕ : HTTP/1.1 200 OK
07-01 17:20:23.984    4626-4650/app D/Retrofit﹕ Content-Length: 52
07-01 17:20:23.984    4626-4650/app D/Retrofit﹕ Date: Wed, 01 Jul 2015 17:20:35 GMT
07-01 17:20:23.984    4626-4650/app D/Retrofit﹕ ETag: "d5143f21b04e73bd70aa104b52ea7bb7"
07-01 17:20:23.984    4626-4650/app D/Retrofit﹕ Set-Cookie: _imagetag_session=dDE1R1lCQ2xUa0VxOUxXZTNKVzdraHIxNW9UOGpKaGpEVHdwSmhtb1NoSU5Jc2htbUVtZGI0Q2Flc1RZMmtadWZQMnRpTkI3L1ZveFlhaFlrZjY2RHc9PS0tdHZVMGxpcDRjbkF5THlxYlB0MjdUQT09--24783a1b51c595beeb955ac9e860498ad8e7c7cb; path=/; HttpOnly
07-01 17:20:23.984    4626-4650/app D/Retrofit﹕ X-Android-Received-Millis: 1435771223983
07-01 17:20:23.984    4626-4650/app D/Retrofit﹕ X-Android-Response-Source: NETWORK 200
07-01 17:20:23.984    4626-4650/app D/Retrofit﹕ X-Android-Sent-Millis: 1435771223840
07-01 17:20:23.984    4626-4650/app D/Retrofit﹕ X-Content-Type-Options: nosniff
07-01 17:20:23.984    4626-4650/app D/Retrofit﹕ X-Frame-Options: SAMEORIGIN
07-01 17:20:23.984    4626-4650/app D/Retrofit﹕ X-Request-Id: 327b11e7-8f38-431b-97bb-3f1d1738395f
07-01 17:20:23.984    4626-4650/app D/Retrofit﹕ X-Runtime: 0.044161
07-01 17:20:23.984    4626-4650/app D/Retrofit﹕ X-UA-Compatible: chrome=1
07-01 17:20:23.984    4626-4650/app D/Retrofit﹕ X-XSS-Protection: 1; mode=block
07-01 17:20:23.984    4626-4650/app D/Retrofit﹕ [ 07-01 17:20:23.985  4626: 4650 D/Retrofit ]{"status":"completed","name":"green square drawing"}
07-01 17:20:23.985    4626-4650/app D/Retrofit﹕ <--- END HTTP (52-byte body)

注意:为了两个成功,调试中应该出现一些 LOG(或者FAILURE?或者get.status()两者都没有)

编辑:我可能正在做一些事情,显然对于异步请求,您需要.setExecutors()在其余适配器创建中创建一个,我正在尝试了解目前将如何工作。

4

1 回答 1

0

问题解决了,只需要以这种方式创建一个执行器:

Executor executor = Executors.newCachedThreadPool();

然后在制作其余适配器时,添加 setExecutors 行:

 RestAdapter.Builder builder = new RestAdapter.Builder();
    builder.setEndpoint(API_URL);
    builder.setLogLevel(RestAdapter.LogLevel.FULL);
    builder.setExecutors(executor, null); //<<-- this line
    builder.setLog(new RestAdapter.Log() {
        @Override
        public void log(String msg) {
            String[] blacklist = {"Access-Control", "Cache-Control", "Connection", "Content-Type", "Keep-Alive", "Pragma", "Server", "Vary", "X-Powered-By"};
            for (String bString : blacklist) {
                if (msg.startsWith(bString)) {
                    return;
                }
            }
            Log.d("Retrofit", msg);
        }
    });
于 2015-07-01T18:58:28.950 回答