I am using the GSON API 1.7.1 sometimes having following error. Is there a bug for this version? Do I need to upgrade the current version 2.x.x
com.google.gson.JsonParseException: Expecting object found: "<!DOCTYPE"
at com.google.gson.JsonObjectDeserializationVisitor.visitFieldUsingCustomHandler(JsonObjectDeserializationVisitor.java:100)
at com.google.gson.ReflectingFieldNavigator.visitFieldsReflectively(ReflectingFieldNavigator.java:63)
at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:120)
at com.google.gson.JsonDeserializationContextDefault.fromJsonPrimitive(JsonDeserializationContextDefault.java:85)
at com.google.gson.JsonDeserializationContextDefault.deserialize(JsonDeserializationContextDefault.java:56)
The server is google push notification server GCM,
Here is my client code.
Gson gson = new Gson();
com.sun.jersey.api.client.ClientResponse webResponse=
resource.header("Authorization", authorizationKey).
header("Content-Type", MediaType.APPLICATION_JSON).
post(com.sun.jersey.api.client.ClientResponse.class, payloadStr);
String responsePayload = webResponse.getEntity(String.class);
GcmResponse response = gson.fromJson(responsePayload, GcmResponse.class); ===>>> Exception point
Is there a bug for this version?
No. Looks like your server is returning a non-JSON response or you're passing wrong parameters, this has nothing to do with the version you're using.
Try to check if the server is working as expected and is returning a JSON.
<!DOCTYPE is not valid Json; your server sometimes sends the wrong messages to your client. Since your client works 99% of the time, the problem is clearly in the server somehow, not the client.
However, since you've only provided us information here from the client side, there isn't enough for us to fix the problem. The best thing to do would be to attempt to capture the complete error message, then use searches or post a different question "Why does my android-scm server send messages that begin with <!DOCTYPE when they should be Json?"
To capture this complete errant message, I would do:
if(response.startsWith("<!DOCTYPE")) {
someLoggingMethod(response);`
} else {
// process the response
Related
I am running a Java Servlet webserver which receives the following request:
http://localhost:8080/ForVen/Recebimento/recebeDispositivo.jsp?lista={"dispositivo":[{"Id":0,"DataMod":"2021-09-28T16:55:55.3528819-03:00","SeSincronizar":0,"NrVersaoReg":1,"DataSincronizacao":"2021-09-28T16:55:55.3538812-03:00","Guid":"BFEBFBFF000206A7","TipoDispositivo":0,"URL":"","VendedorId":1}]}
When I send it, it gives me the following error:
javax.servlet.ServletException: org.json.JSONException: Missing value at character 1 of {"dispositivo":[{"Id":0,"DataMod":"2021-09-28T16:55:55.3528819-03:00","SeSincronizar":0,"NrVersaoReg":1,"DataSincronizacao":"2021-09-28T16:55:55.3538812-03:00","Guid":"BFEBFBFF000206A7","TipoDispositivo":0,"URL":"","VendedorId":1}]}
If I try to send the same request, but with the JSON "formatted", it works.
http://localhost:8080/ForVen/Recebimento/recebeDispositivo.jsp?lista={"dispositivo":[{"Id":0,"DataMod":"2021-09-28T16:55:55.3528819-03:00","SeSincronizar":0,"NrVersaoReg":1,"DataSincronizacao":"2021-09-28T16:55:55.3538812-03:00","Guid":"BFEBFBFF000206A7","TipoDispositivo":0,"URL":"","VendedorId":1}]}
This is the way I am deserializing the JSON:
JSONObject jso = new JSONObject(myIncomingJson);
I don't know if it has something to do with some Apache configuration, but I hope so, it would be extremely painful to change all of the client-side requests.
As f1sh said, I have to encode the JSON that is being send on the URL, my mistake that I didn't saw it. :)
This is how the URL should be:
http://192.168.1.58:8080/ForVen/Recebimento/recebeDispositivo.jsp?lista=%7B%5C%22dispositivo%5C%22%3A%5B%7B%5C%22Id%5C%22%3A0%2C%5C%22DataMod%5C%22%3A%5C%222021-09-30T09%3A15%3A37.4138271-03%3A00%5C%22%2C%5C%22SeSincronizar%5C%22%3A0%2C%5C%22NrVersaoReg%5C%22%3A1%2C%5C%22DataSincronizacao%5C%22%3A%5C%222021-09-30T09%3A15%3A37.4148274-03%3A00%5C%22%2C%5C%22Guid%5C%22%3A%5C%22BFEBFBFF000206A7%5C%22%2C%5C%22TipoDispositivo%5C%22%3A0%2C%5C%22URL%5C%22%3A%5C%22%5C%22%2C%5C%22VendedorId%5C%22%3A1%7D%5D%7D
I have a monolithic legacy application that I need to read and submit data to. It's using Google Web Kit and Java Servlets.
I have access to the source code, but I'm new to both Servlets and GWT.
I'm trying to encapsulate a rest client in my project that can communicate with GET/POST rest calls to the legacy server.
I've been able to send a POST request using Postman, and then used Reactive Spring 5.0 framework to sending that request.
When I try to deserialize the response, I'm running into a ton of errors.
How would I deserialize this payload?
7|0|7|http://localhost:8080/testproject/
|29F4EA1240F157649C12466F01F46F60|
com.test.client.GreetingService|greetServer|java.lang.String|
myInput1|myInput2|1|2|3|4|2|5|5|6|7|
I've searched all day, and followed a few blogs like these:
https://docs.google.com/document/d/1eG0YocsYYbNAtivkLtcaiEE5IOF5u4LUol8-LL0TIKU/edit#
https://blog.gdssecurity.com/labs/2009/10/8/gwt-rpc-in-a-nutshell.html
I'm not sure code wise how I can serialize them into my own object for my new service.
static WebClient webClient = WebClient.create();
public static void main(String[] args) {
Mono<String> body = Mono.just("7|0|7|http://localhost:8080/testproject/|29F4EA1240F157649C12466F01F46F60|com.test.client.GreetingService|greetServer|java.lang.String|myInput1|myInput2|1|2|3|4|2|5|5|6|7|");
Mono<String> response = webClient.post()
.uri("http://localhost:8080/testproject/")
.header("Content-Type", "text/x-gwt-rpc;charset=UTF-8")
.header("X-GWT-Module-Base", "http://localhost:8080/testproject/")
.header("X-GWT-Permutation", "29F4EA1240F157649C12466F01F46F60")
.cookie("JSESSIONID", "2BCEBF12GE2C3A0335F5012812A73638")
.body(body, String.class)
.retrieve()
.bodyToMono(String.class);
String unBlocked = response.block();
System.out.println(unBlocked);
//OK[1,1,["java.lang.Integer/3438228391"],0,2]
try {
ServerSerializationStreamReader streamReader = new ServerSerializationStreamReader(
Thread.currentThread().getContextClassLoader(), null);
streamReader.prepareToRead(unBlocked);
System.out.println(streamReader.readObject());
} catch ( Exception e) {
e.printStackTrace();
}
}
Error:
com.google.gwt.user.client.rpc.IncompatibleRemoteServiceException: This application is out of date, please click the refresh button on your browser. ( Malformed or old RPC message received - expecting version between 5 and 7 )
I've tried every version of GWT because of the malformed RCP message.
Also, I tried to stick it into a string, which I'm sure is failing on its own.
You are trying to decode the server response with the code meant decode the client request. At present they use a different format for historical reasons - note how the response starts with "//OK", but the request has the version/flags/stringcount "7|0|7" beginning.
In at least a small part this is because when a client calls a server, it needs to describe what version it is speaking and where the server should find the file listing the set of classes that the client expects are allowed to be serialized. When the server responds, since the client already told it about the typed that can be serialized, it doesn't need to tell the client the same thing again.
Reading the com.google.gwt.user.client.rpc.impl.ClientSerializationStreamReader class and its docs will show the response format and how it can be decoded into objects. There is presently no server-side code that I'm aware of that is intended to do this job, but could probably be written with fairly little difficulty, just some persistence.
I was meet a problem when I tried used net/jsonrpc package to build a server and a Java client with jsonrpc4j
The problem is jsonrpc4j is when error happen, golang`s method will return error and encoding to json.
I got this json object in client
{"id": -6028374044949000, "result": null, "error": "some error return message"}
This object cast failed in java's json4j.
http://www.jsonrpc.org/specification#error_object
After I checked the jsonrpc page, it is said the error field MUST a json object with fields [code, message, date], the golang jsonrpc package not meet the require.
So I`m confused how to solve this.
Change the jsonrpc lib,
Just replace the rpc way to thrift/gRpc,
Avoid to return error but send error in reply and let Java check the response,
Or just edited the json4j or golang's source code ( I'm very horrible about this option)
Thanks for watch.
If you need JSON-RPC 2.0 support for Go you can try https://github.com/powerman/rpc-codec
Sorry if this is a duplicate question but google isn't smart enough to understand me or I'm too dumb to express my question simple enough for it to understand.
I don't know if this is my problem but I'm 90% sure this is it.
I'd like to know how to represent a Unix path within a GET request so that my web service doesn't return a 404. I think it's because one of my JSON fields in the query is a Unix path and because of the slashes, the webservice thinks it's part of the URL and not a part of my query.
For example, I'm using a RESTClient that's an add-on to Mozilla to test my web service. For the POST request, I enter as the url
http://mytestserver:8080/mydir/
and in the body, I put in my JSON object
{"filename":"obit.jpg", "Path":"test/2/1"}
This method works fine. I get a status code 200 and a return JSON object with the expected output.
When I use the same string for a GET request, I get a status code 404 and no return JSON object. I put as the url in the RESTClient
http://mytestserver:8080/mydir/{"filename":"obit.jpg", "Path":"test/2/1"}
and I get a status code 404 and the response body just says 404 - Not found
To further test my theory, I entered the following url in a GET request, removing the /2/1 from the path, which works.
http://mytestserver:8080/mydir/{"filename":"obit.jpg", "Path":"test"}
I've tried encapsulating the whole JSON string in quotes but that didn't work either so I've run out of things to try.
Thanks in advance for any help you can give me. If I need to show some code, please let me know, although, I don't think it's a code problem, I think it's a representation problem. Thanks.
Found out that JSON objects are usually sent via POST, not GET. Since I appended it to the URL via GET, it gave me problems. Per How to send a GET request with a "/" in the query
I recently moved over to Java and am attempting to write some REST tests against the netflix REST service.
I'm having an issue in that my response using rest assured either wants to send a gzip encoded response or "InputStream", neither of which provide the actual XML text in the content of the response. I discovered the "Accept-Encoding" header yet making that blank doesn't seem to be the solution. With .Net I never had to mess with this and I can't seem to find the proper means of returning a human readable response.
My code:
RestAssured.baseURI = "http://api-public.netflix.com";
RestAssured.port = 80;
Response myResponse = given().header("Accept-Encoding", "").given().auth().oauth(consumerKey, consumerSecret, accessToken, secretToken).param("term", "star wars").get("/catalog/titles/autocomplete");
My response object has a "content" value with nothing but references to buffers, wrapped streams etc. Trying to get a ToString() of the response doesn't work. None of the examples I've seen seem to work in my case.
Any suggestions on what I'm doing wrong here?
This has worked for me:
given().config(RestAssured.config().decoderConfig(DecoderConfig.decoderConfig().noContentDecoders())).get(url)
I guess in Java land everything is returned as an input stream. Using a stream reader grabbed me the data I needed.
Until its version 1.9.0, Rest-assured has been providing by default in the requests the header "Accept-Encoding:gzip,deflate" with no way of changing it.
See
https://code.google.com/p/rest-assured/issues/detail?id=154
It works for me:
String responseJson = get("/languages/").asString();