【问题标题】:Android Pusher AuthorizationFailureException: FileNotFoundExceptionAndroid Pusher AuthorizationFailureException: FileNotFoundException
【发布时间】:2019-02-12 09:27:24
【问题描述】:

我已经查看了 StackOverflow 中的许多帖子及其 git 问题。几乎所有这些问题都与 HttpAuthorizer 的标头有关。但它仍然不适合我。谁能帮我找出我的代码有什么问题?谢谢。

注意:我不知道错误是否在服务器端,因为当我的团队使用浏览器中的简单聊天应用程序对其进行测试时,它可以正常工作。

public class PusherService {
    private static final String CHANNEL = "private-User." + accountId;

    private static PusherService instance;
    private Pusher pusher;

    public static synchronized PusherService getInstance() {
        if (instance == null) {
            instance = new PusherService();
        }

        return instance;
    }

    private PusherService() {
        pusher = initiatePusher();
    }

    private static Pusher initiatePusher() {
        HttpAuthorizer httpAuthorizer = new HttpAuthorizer(BuildConfig.PUSHER_AUTH_URL);
        Map<String, String> headers = new HashMap<>();
        headers.put("Authorization", Session.getToken());
        headers.put("Accept", "application/json");
        headers.put("Content-Type", "application/json");
        httpAuthorizer.setHeaders(headers);

        PusherOptions options = new PusherOptions()
                .setCluster(BuildConfig.PUSHER_CLUSTER)
                .setEncrypted(true)
                .setAuthorizer(httpAuthorizer);

        Pusher pusher = new Pusher(BuildConfig.PUSHER_KEY, options);
        pusher.connect(new ConnectionEventListener() {
            @Override
            public void onConnectionStateChange(ConnectionStateChange connectionStateChange) {
                Log.i("Pusher", "Connection State Change: " + connectionStateChange.getCurrentState().name());
            }

            @Override
            public void onError(String s, String s1, Exception e) {
                Log.i("Pusher", String.format("Connection Error: [%s], exception: [%s]", s, e));
            }
        });

        return pusher;
    }

    public PrivateChannel getChannel() {
        PrivateChannel privateChannel = pusher.getPrivateChannel(CHANNEL);
        if (privateChannel == null) {
            privateChannel = pusher.subscribePrivate(CHANNEL, new PrivateChannelEventListener() {
                @Override
                public void onAuthenticationFailure(String s, Exception e) {
                    // Authentication failed
                    Log.i("Pusher", String.format("Authentication failure due to [%s], exception: [%s]", s, e));
                }

                @Override
                public void onSubscriptionSucceeded(String s) {
                    Log.i("Pusher", "Private connection succeeded");
                }

                @Override
                public void onEvent(String channel, String event, String data) {
                    Log.i("Pusher", data);
                }
            });
        }

        return privateChannel;
    }
}

然后为了测试它,我刚刚调用了

PusherService.getInstance().getChannel();

【问题讨论】:

    标签: android pusher


    【解决方案1】:

    经过 2 天的搜索,我们找到了答案。我只需要删除 Content-Type 标头。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多