问题描述
我是Pusher的新手.我已经在我的应用程序中成功实现了公共频道订阅.我目前无法使用Android私人频道订阅.
I am new to Pusher. I had successfully implemented public channel subscription in my app. I am currently stuck at Android private channel subscription.
我们应该在服务器的post端点中传递请求主体参数.在我的iOS应用程序中,我们正在创建自定义授权方,以在PusherOption中发送请求正文.但是在Android中,只有2个选项可以在PusherOptions中发送参数,例如setHeaders()和setQueryStringParameters().
We are supposed to pass request body parameters in post endpoint of our server. In my iOS application, We are creating custom Authorizer to send the request body in the PusherOption. But in Android there are only 2 options to send parameters in PusherOptions like setHeaders() and setQueryStringParameters().
我必须发送以下带有URL的正文:
I have to send following body with the URL:
["data": ["socket_id": SOCKET_ID, "user_id": USER_ID, "channel": "private-CHANNEL_NAME"]]
我目前在onAuthenticationFailure上以[com.pusher.client.AuthorizationFailureException:java.io.FileNotFoundException:MY ENDPOINT URL]出现异常.
I am currently getting exception at onAuthenticationFailure as [com.pusher.client.AuthorizationFailureException: java.io.FileNotFoundException: MY ENDPOINT URL].
我当前的Android代码如下:
My current Android code is as follows:
public static void subscribePrivateChannel(final String channelName, final String userId, final String authKey) {
final HttpAuthorizer authorizer = new HttpAuthorizer(MY_ENDPOINT_URL);
PusherOptions options = new PusherOptions().setEncrypted(true).setAuthorizer(authorizer);
final Pusher pusher = new Pusher(Config.PUSHER_APP_KEY, options);
pusher.connect(new ConnectionEventListener() {
@Override
public void onConnectionStateChange(ConnectionStateChange connectionStateChange) {
if (connectionStateChange.getCurrentState().toString().equalsIgnoreCase("CONNECTED")) {
String socketId = pusher.getConnection().getSocketId();
HashMap<String, String> parameters = new HashMap<>();
parameters.put("socket_id", socketId);
parameters.put("channel", channelName);
parameters.put("user_id", userId);
authorizer.setQueryStringParameters(parameters);
pusher.subscribePrivate(channelName, new PrivateChannelEventListener() {
@Override
public void onAuthenticationFailure(String s, Exception e) {
Log.d(TAG, "onAuthenticationFailure() called with: s = [" + s + "], e = [" + e + "]");
}
@Override
public void onSubscriptionSucceeded(String s) {
Log.d(TAG, "onSubscriptionSucceeded() called with: s = [" + s + "]");
}
@Override
public void onEvent(String s, String s1, String s2) {
Log.d(TAG, "onEvent() called with: s = [" + s + "], s1 = [" + s1 + "], s2 = [" + s2 + "]");
}
});
}
}
@Override
public void onError(String s, String s1, Exception e) {
Log.d(TAG, "onError() called with: s = [" + s + "], s1 = [" + s1 + "], e = [" + e + "]");
}
});
}
推荐答案
您可以通过Pusher的 UrlEncodedConnectionFactory
添加任意请求正文参数:
You can add arbitrary request body parameters via Pusher's UrlEncodedConnectionFactory
:
HttpAuthorizer(authEndpoint, UrlEncodedConnectionFactory(mapOf(
"key1" to "value",
"key2" to "value",
)))
(使用 com.pusher:pusher-java-client:2.2.5
)
令人沮丧的是,这没有更好的文档记录/更容易找到...
Frustrating that this isn't better documented/easier to find...
这篇关于HttpAuthorizer中的Android Pusher Request Body实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!