I am trying to read simple json object in the Java program below:
public class Hello{
#POST
#Path("/jsonRequest")
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
public Response getJson(String url) throws InterruptedException, IOException {
JSONObject json= readJsonFromUrl(url);
Response.ResponseBuilder response = Response.ok(json);
return response.build();
}
public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
JSONObject json = new JSONObject(IOUtils.toString(new URL(url), Charset.forName("UTF-8")));
return json;
}
However, when I try to give a POST request via POSTMAN I get the following error:
javax.servlet.ServletException: org.glassfish.jersey.server.ContainerException: java.net.MalformedURLException: no protocol:{"name":"pallavi",
"location":"Bangalore"}
The POSTMAN request looks like:
Can someone please help me identify where exactly am I going wrong? Any help is highly appreciated
Related
In my Spring-MVC application, I am calling a .jsp page using AJAX GET request with passing some data. But I am getting an exception that is posted below. Please help.
Error
WARNING: Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public java.lang.String com.controllers.CustomerController.blankPage(java.lang.String) throws org.json.JSONException
AJAX
function blankPage(l) {
var formData = {
name : l
}
$.ajax({
type : "GET",
url : "/MyApp/blankPage",
data : formData
});
}
Java
#GetMapping(value = "/blankPage")
public String blankPage(#RequestBody String patientName) throws JSONException {
System.out.println(patientName);
return "redirect:/blank";
}
#RequestMapping(value = "/blank", method = RequestMethod.GET)
public String blank() {
return "blank";
}
GET with Request body doesn't make any sense.
make your request as POST request and send the request as Object.
because you are sending a JSON object as a request, not a string.
#PostMapping(value = "/blankPage")
public String blankPage(#RequestBody PatientRequest patientName) throws JSONException {
System.out.println(patientName);
return "redirect:/blank";
}
#getter #setter
class PatientRequest {
private String patientName;
}
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
I'm trying to post a JSON to some REST service, but I always end up with a HTTP Error 415: Unsupported Media Type.
The REST Documentation clearly notes I should use application/json, which I do. Surely I must be overlooking something.
public JSONObject fetchResponse() throws ResourceException, JSONException, IOException {
JRRequest jr = new JRRequest();
jr.setJql(jql);
jr.setMaxResults(Integer.parseInt(maxresults));
jr.setFields(fields);
Gson json = new Gson();
String payload = json.toJson(jr);
JSONObject jsObj = new JSONObject(getClientResource(restUri).post(payload,MediaType.APPLICATION_JSON).getText());
return jsObj;
}
private ClientResource getClientResource(String uri) {
ClientResource clientResource = new ClientResource(uri);
Application app = new Application();
clientResource.setChallengeResponse(ChallengeScheme.HTTP_BASIC,username, password);
return clientResource;
}
Okay, I found the solution. Instead of doing it all in one line, I tried this:
Representation rep = new StringRepresentation(payload, MediaType.APPLICATION_JSON);
JSONObject jsObj = new JSONObject(getClientResource(restUri).post(rep).getText());
And it works now!
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()));
I am using okhttp lib with Java and PHP. My Java client is running the following code.
public class Connection {
public static final MediaType JSON = MediaType
.parse("application/json; charset=utf-8");
OkHttpClient client = new OkHttpClient();
String post(String url, String json) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder().url(url).post(body).build();
Response response = client.newCall(request).execute();
return response.body().string();
}
public static void main(String[] args) throws IOException {
Connection example = new Connection();
String json = "{'input':'test'}";
String response = example.post("http://localhost/android_api/index.php", json);
System.out.println(response);
}
}
On the server-side I try to decode the JSON String with code following below but my webservice just return a NULL.
<?php
$rawData = file_get_contents("php://input");
$json = json_decode($rawData);
var_dump($json);
?>
What am I doint wrong?
First, you are calling an http request on the main-thread which will cause an error. So you use AsyncTask