麻烦使用改造

时间:2015-07-01 17:48:11

标签: android get retrofit cloudsight

我试图通过改造从服务器获取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(),我试图了解当前如何工作。

1 个答案:

答案 0 :(得分: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);
        }
    });