json cause crash - Android - java

I'm using Json with PHP to connect my android app with a remote mySql db.
but for some reason the JSON parser returns and error saying there's a problem with the json string.
The json string is: {"success":1,"message":"sit created."}
the error is: Error parsing data org.json.JSONException: A JSONObject text must begin with '{' at character 1 of {"success":1,"message":"sit created."}
Can anyone tell me what the problem is, I can't figure it out.

Related

Convert DynamoJson to something compatible with the Dynamo client

I've got a big dump of DynamoJson e.g.
{"Item": {"id":{"N":"896"}, "name": {"S": "Tom"}}}
I want to parse this JSON and put it to my DynamoDB table...
I've tried:
import com.amazonaws.services.dynamodbv2.document.Item;
Item item = Item.fromJSON(BLOB);
But unfortunately its not smart enough to parse the DynamoDB Json format and doesn't deal with the inner types (S, N etc)... When I try to put I get errors like:
Type mismatch for key id expected: N actual: M
Related Questions:
AWS DynamoDB on Android: Inserting JSON Directly?
This does not work for the DynamoJson format.
Unmarshall DynamoDB JSON
This is exactly what I need but its in NodeJS

Converting from HttpURLConnection TO Retrofit2 Issues

I'm upgrading some legacy Android / Java code that pulls in data from a server. The legacy code uses HttpURLConnection and pulls in a inputStream and converts it to a GZipInputStream. The result is then converted to a JSONObject. Then the data is parsed manually. This all works fine, meaning valid json is returned as it starts with '{'
I'm trying to updgrade this code using Retrofit2 and the GsonConverterFactory so I can auto map the json to Classes.
I'm getting an error: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
This tells me that the data is coming in as a string and not a JSON object.
NOTE: I'm sending the same headers and query parameters for both my legacy code and new Retrofit implementation --- but different result -- can't figure out why.
The issue was that the server was expecting all of my query parameters to be in one field and the field named "Object" and in a json format i.e: Object={"field1":"fieldOneValue","field2":"fieldTwoValue"} AND had to be Url Uncoded Format:
#FormUrlEncoded
#POST("testing/myapi.api")
Observable<MyClass> getMyApiData(#Url String url, #Field("Object") String object);

How do I get the value of a key if it contains a space in Rest Assured / Serenity?

I am trying to use Rest Assured in the Serenity framework to validate an endpoint response. I send an xml body to the endpoint and expect a JSON response back like so:
{"Entry ID" : "654123"}
I want to send the XML and verify in the JSON response that the value of the key "Entry ID" is not empty or null. The problem is, the key has a space in it, and I believe it is causing an error. Here is what I have so far:
SerenityRest.given().contentType(ContentType.XML)
.body(xmlBody)
.when().accept(ContentType.JSON).post(endpoint)
.then().body("Entry ID", not(isEmptyOrNullString()))
.and().statusCode(200);
This produces the error:
java.lang.IllegalArgumentException: Invalid JSON expression:
Script1.groovy: 1: unable to resolve class Entry
# line 1, column 33.
Entry ID
^
1 error
I have tried wrapping the "Entry ID" term in different ways to no avail:
.body("'Entry ID'", not(isEmptyOrNullString()))
.body("''Entry ID''", not(isEmptyOrNullString()))
.body("\"Entry ID\"", not(isEmptyOrNullString()))
.body("['Entry ID']", not(isEmptyOrNullString()))
.body("$.['Entry ID']", not(isEmptyOrNullString()))
Is it possible to get the value of a key that contains a space in Rest Assured?
You just need to escape the key with single quotes:
then().body("'Entry ID'", not(isEmptyOrNullString()))
Here's an example (tested in version 3.0.6):
// Given
String json = "{\"Entry ID\" : \"654123\"}";
// When
JsonPath jsonPath = JsonPath.from(json);
// Then
assertThat(jsonPath.getString("'Entry ID'"), not(isEmptyOrNullString()));

JAX-RS response - remove invalid XML characters before read entity

Is there any possibility to remove invalid characters from XML response before trying to read entity?
Response searchResponse = webTarget.request(MediaType.APPLICATION_XML).get();
/..
SearchOutput searchOutput = searchResponse.readEntity(SearchOutput.class);
This code works fine when XML is correct but fails when response contains invalid Unicode characters.
How to workaround that assuming I can't change these invalid data in a database?

org.json.JSONException: Unterminated string at 737 [character 738 line 1]

I'm using the org.json.JSONObject to parse some json being sent to my servlet by an iphone. I was stuck for a while by why I would be getting an error message at all. The error message was:
org.json.JSONException: Unterminated string at 737 [character 738 line 1]
After printing out what I received, I see that the string sent was indeed cut short and stopped mid-json. I can't understand why it would be cut short. There's no limit on String size is there (or at least only a memory limit surely).
Has anyone else had thins error?
Cheers
Joe
json work well with \n but if you have any other special charachters in your meesage like
\ , # , & , # etc.. first convert them into their respective HEX value and then send your message.
If you're using the HTTP GET method to send data using query parameters, realize that there's a practical limit on the amount of data you can send that way. It's about 2000 characters (varies by server and client). You can easily exceed that when URL encoding a shorter string.
Json won't work if received string contain new line character like \n. Try to check for it and escape the character.

Categories