I am trying to send following Integer value to server.
int mStoreArea;
I use this link as REST client.
here is Request:
RestClient client = new RestClient(my_url);
client.AddParam("area", String.valueOf(c.getStoreArea()));
and the Error I face is : Int value required!
I retrieve this integer from a json object saved to a file, its procedure is described below:
public myClass(JSONObject json) throws JSONException {
mStoreArea = json.optInt(JSON_TAG);
}
public JSONObject toJSON() throws JSONException {
JSONObject json = new JSONObject();
json.put(JSON_TAG, mStoreArea);
return json;
}
I think you should use this:
client.AddParam("area", Integer.parseInt(c.getStoreArea()));
Related
I'm presently migrating from the Java ASK-SDK v1 to Java ASK SDK v2.
I'm trying to return a webhook call using the ResponseBuilder class that I built my response up and the data is correct, however when I try to populate the HTTP body with the JSON text, the ResponseBuilder.toString() value doesn't just populate the data with just the string, I get the following:
Optional[class Response {
outputSpeech: class SsmlOutputSpeech {
class OutputSpeech {
type: SSML
playBehavior: null
}
ssml: <speak>Some of the things you can say are What would you like to do?</speak>
}
card: null
reprompt: class Reprompt {
outputSpeech: class SsmlOutputSpeech {
class OutputSpeech {
type: SSML
playBehavior: null
}
ssml: <speak>You can say ..., is that what you want?</speak>
}
}
directives: []
shouldEndSession: false
canFulfillIntent: null
}]
Is there another way to get the string for the body of the response? The BaseSkillResponse has a getResponse() call, however, I cannot figure out how to use the class to generate the String response output.
I was able to get the string with the following in my class:
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
myFunction(){
try{
return toJsonString(responseBuilder.build().get());
} catch(IOException e) {
e.printStackTrace();
}
}
public String toJsonString(Response response)throws IOException {
return OBJECT_MAPPER.writeValueAsString(response);
}
Solve this by doing the following:
public String toJsonString(Response response)throws IOException
{
JacksonSerializer jacksonSerializer = new JacksonSerializer();
constructedResponse = jacksonSerializer.serialize(response);
JSONObject jsonObject = new JSONObject();
jsonObject.put("response",constructedResponse);
}
Error on getting JSON data from Couchbase DB in JAVA Restful API.
I am trying to retrieve JSON data from Couchbase DB and return it through an API.
Error :
"HTTP Status 500 - java.lang.ClassCastException: java.lang.String
cannot be cast to com.couchbase.client.java.document.JsonDocument"
Code:
#Override
public JsonDocument getProduct(String productId)
throws URISyntaxException, IOException, InterruptedException, Exception {
JsonDocument response = null;
CouchbaseConnectionManager couchbaseConnectionManager = new CouchbaseConnectionManager();
response = (JsonDocument) couchbaseConnectionManager.getCouchbaseClient().get(productId);
return response;
}
Could you please help.
Thanks
Looks like the API returns a string and you are casting to a JsonDocument. You could take the return as a string and then use org.json or other Json helpers to convert to JSON
This question already has answers here:
Jersey 415 Unsupported Media Type
(3 answers)
Closed 7 years ago.
My restful API method looks like this
#GET
#Produces(MediaType.APPLICATION_JSON)
public JSONArray getMessage()
{
FreeDriversService f=new FreeDriversService();
try {
return f.executeFreeDrivers(); // this method return a JSONArray
}
catch(Exception e) {
System.out.println(e.toString());
return new JSONArray();
}
}
When I use the toString() method on JSONArray it does produce a result, but I would like JSON as output. How can i do that?
I am getting this error
A message body writer for Java class org.json.JSONArray, and Java type class org.json.JSONArray, and MIME media type application/json
was not found
Problem Summary:-
You have mentioned the output as JSON in the #Produces annotation as #Produces(MediaType.APPLICATION_JSON) but instead of sending JSONObject your method getMessage is returning JSONArray.
You can not convert a JSON to an JSONArray simply, because in JSONArray same type of JSONObject can be repeated multiple times with same keys, which can be replaced by the later values of the multiple JSONObject.
Solution :-
You can create a JSONObject and can put the JSONArray inside it as a value for a user defined key.
#GET
#Produces(MediaType.APPLICATION_JSON)
public Response getMessage(){
JSONObject finalJson = new JSONObject ();
JSONArray inputArray = new JSONArray();
FreeDriversService f=new FreeDriversService();
try{
inputArray = f.executeFreeDrivers(); // this method return a JSONArray
}catch(Exception e){
System.out.println(e.toString());
}
finalJson.put("array",inputArray );
return Response.status(200).entity(finalJson).build();
}
Returing a JSONObject right now from a restful webservice using Jersey. Its working perfectly fine and returning a JSSONObject as follows.
#GET
#Path("/LoginGetValues")
#Produces({"application/x-javascript"})
public JSONObject GetValues(#QueryParam("request") String request)
{ ...
JSONObject value = new JSONObject();
value = null ;
return value ;
}
But intending to convert that response into JsonP response , tried to append that with a callback function (as the following refering ( here) but then not getting the required response , indeed its asking to define the #JSONP annotation . Also if i have to define the callback then how and where i have to do that ! Kindly help to get a JSONP response
#GET
#JSONP(queryParam="callback")
#Path("/LoginGetValues")
#Produces({"application/x-javascript"})
public JSONWithPadding GetValues( #QueryParam("callback") String callback ,#QueryParam("request") String request)
{ ...
JSONObject value = new JSONObject();
value = null ;
return new JSONWithPadding(value , callback);
}
#GET
#Produces(MediaType.APPLICATION_JSON)
public List<ProductData> getAllProductList(#QueryParam("hotel_id") int hotel_id) throws SQLException{
System.out.println("Hotel id id==="+hotel_id);
ProductData productData=new ProductData();
List<ProductData> products = new ArrayList<ProductData>();
rs=stmt.executeQuery("select * from products where hotel_id="+hotel_id);
while(rs.next()){
productData.setProductName(rs.getString("name"));
productData.setProductCategory(rs.getString("category"));
productData.setProductRate(rs.getDouble("rate"));
productData.setProductLogoPath(rs.getString("productLogoPath"));
products.add(productData);
}
return products;
}
I have passed List as JsonObject.Now i tried to get List value like
void handleResponse(String response) throws JSONException {
JSONObject jsonObject=new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("products");
}
but i can't get the List value.anyBody can help me?
There is the simple way to convert json string to object :
Try this :
ObjectMapper mapper = new ObjectMapper();
POJO obj = mapper.readValue(yourJSONString, POJO.class);
Use method signature something similar like-
public Response getAllProduct..
&
return like-
return Response.status(Status.OK).entity(products).build();
For intg. layer use-
public ClientResponse<> similarSignatureMethod..
&
call via the client and then get response entity as-
clientResponse.getEntity();