I'm trying to send a JSON object to a server on a custom port. I never get any response back from the server however. I think the error might be in the string entity but I can't figure out what it is.
I've had a look at these other issues:
How to build a http post request with the correct entity with Java and not using any library?
How to POST data in Android to server in JSON format?
None of them have solved my problem. I'm trying the solution (16 votes) from "HTTP POST using JSON in Java" but it wont work.
Here's my code:
public void reset() {
DefaultHttpClient httpClient = new DefaultHttpClient();
try {
System.out.println("Start");
jsonURL = "http://x.x.x.x:3994";
HttpPost request = new HttpPost(jsonURL);
StringEntity params = new StringEntity("{\"id\":1,\"method\":\"object.deleteAll\",\"params\":[\"subscriber\"]}");
request.addHeader("Content-Type", "application/json");
request.setEntity(params);
HttpResponse response = httpClient.execute(request);
System.out.println("End");
} catch (Exception ex) {
ex.printStackTrace();
} finally {
httpClient.getConnectionManager().shutdown();
}
}
The console prints "Start" when it begins but I never get any output from "End" nor any stack trace from the exception. When debugging the debugger stops at the "httpClient.execute(request)
" line and never continues.
When I run this command from my terminal:
echo '{"id":1, "method":"object.deleteAll", "params":["subscriber"]} ' | nc x.x.x.x 3994
Everything is executed properly and my request is received by the server.
I think my problem might be with the StringEntity
but I'm not sure.
EDIT:
Using Wireshark I was able to capture this packet being sent to the server:
POST / HTTP/1.1 Content-Type: application/json Content-Length: 60
Host: x.x.x.x:3994 Connection: Keep-Alive User-Agent:
Apache-HttpClient/4.2.1 (java 1.5)
{"id":1,"method":"object.deleteAll","params":["subscriber"]}
and this packet coming back from the server:
{"id":null,"error":{"code":-32700,"message":"Unexpected character ('P' (code 80)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')"}}
{"id":null,"error":{"code":-32700,"message":"Unexpected character ('c' (code 99)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')"}}
{"id":null,"error":{"code":-32700,"message":"Unexpected character ('C' (code 67)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')"}}
{"id":null,"error":{"code":-32700,"message":"Unexpected character ('C' (code 67)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')"}}
{"id":null,"error":{"code":-32700,"message":"Unexpected character ('H' (code 72)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')"}}
{"id":null,"error":{"code":-32700,"message":"Unexpected character ('C' (code 67)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')"}}
{"id":null,"error":{"code":-32700,"message":"Unexpected character ('U' (code 85)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')"}}
{"id":null,"error":{"code":-32700,"message":"Line must contain a JSON object"}}