Here is the code example (replace with your code example):
public class VkBotTest extends LongPollBot {
private static final String GROUP_TOKEN = "TOKEN_HERE";
public static void main(String[] args) throws VkApiException {
new VkBotTest().startPolling();
}
@Override
public void onMessageEvent(MessageEvent messageEvent) {
System.out.println(messageEvent.getEventId());
System.out.println(messageEvent.getConversationMessageId());
System.out.println(messageEvent.getPayload());
}
@Override
public String getAccessToken() {
return GROUP_TOKEN;
}
}
}
Stacktrace:
Exception in thread "main" com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Expected value at line 1 column 1 path $
at com.google.gson.Gson.fromJson(Gson.java:902)
at com.google.gson.Gson.fromJson(Gson.java:852)
at com.google.gson.Gson.fromJson(Gson.java:801)
at com.google.gson.Gson.fromJson(Gson.java:773)
at api.longpoll.bots.adapters.deserializers.PayloadDeserializer.deserialize(PayloadDeserializer.java:23)
at api.longpoll.bots.adapters.deserializers.PayloadDeserializer.deserialize(PayloadDeserializer.java:14)
at com.google.gson.internal.bind.TreeTypeAdapter.read(TreeTypeAdapter.java:69)
at com.google.gson.TypeAdapter$1.read(TypeAdapter.java:199)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:129)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220)
at com.google.gson.Gson.fromJson(Gson.java:887)
at com.google.gson.Gson.fromJson(Gson.java:952)
at com.google.gson.internal.bind.TreeTypeAdapter$GsonContextImpl.deserialize(TreeTypeAdapter.java:162)
at api.longpoll.bots.adapters.deserializers.UpdateDeserializer.deserialize(UpdateDeserializer.java:66)
at api.longpoll.bots.adapters.deserializers.UpdateDeserializer.deserialize(UpdateDeserializer.java:47)
at com.google.gson.internal.bind.TreeTypeAdapter.read(TreeTypeAdapter.java:69)
at com.google.gson.TypeAdapter$1.read(TypeAdapter.java:199)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:129)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220)
at com.google.gson.Gson.fromJson(Gson.java:887)
at com.google.gson.Gson.fromJson(Gson.java:852)
at com.google.gson.Gson.fromJson(Gson.java:801)
at com.google.gson.Gson.fromJson(Gson.java:773)
at api.longpoll.bots.methods.impl.VkMethod.execute(VkMethod.java:130)
at api.longpoll.bots.LongPollBot.startPolling(LongPollBot.java:73)
at com.ubivashka.java.test.VkBotTest.main(VkBotTest.java:14)
Caused by: com.google.gson.stream.MalformedJsonException: Expected value at line 1 column 1 path $
at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1559)
at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:590)
at com.google.gson.stream.JsonReader.peek(JsonReader.java:425)
at com.google.gson.Gson.fromJson(Gson.java:883)
... 28 more
I'm using java-vk-bots-longpoll-api version (replace with version you are using): 4.1.0
Could you please help me to understand what is wrong here?
My thoughts: This is issue with payload. I am sending its as an string. But api.longpoll.bots.adapters.deserializers.PayloadDeserializer tries to parse it as json. Yes, i can fix this issue with just changing payload. But i hope you`ll fix that
I think fixed version would be:
public class PayloadDeserializer implements JsonDeserializer<JsonElement> {
/**
* {@link Gson} object.
*/
private static final Gson GSON = new Gson();
@Override
public JsonElement deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) throws JsonParseException {
return jsonElement;
}
}
Because GSON.fromJson(jsonElement.getAsString(), JsonElement.class) cannot process simple strings.
I'm executing this request (replace with URI from your logs):
https://lp.vk.com/wh*******
with parameters (replace with params from your logs):
wait=25&act=a_check&key=***&ts=19695
and I'm receiving (replace with response from your logs):
Here is the code example (replace with your code example):
Stacktrace:
I'm using
java-vk-bots-longpoll-api
version (replace with version you are using):4.1.0
Could you please help me to understand what is wrong here?
My thoughts: This is issue with payload. I am sending its as an string. But
api.longpoll.bots.adapters.deserializers.PayloadDeserializer
tries to parse it as json. Yes, i can fix this issue with just changing payload. But i hope you`ll fix thatI think fixed version would be:
Because
GSON.fromJson(jsonElement.getAsString(), JsonElement.class)
cannot process simple strings.Good luck! And have a good day!