Question

I have made a Java class (client) to connect with the server, my requirement is to send the JSON object to server and get the response back .

I have already a JSON object however I have no idea how to send the JSON object to the server in Java.

Was it helpful?

Solution

try this it may help

   public boolean reset() {
        DataInputStream is;
        DataOutputStream os;
        boolean result = true;
        String noReset = "Could not reset.";
        String reset = "The server has been reset.";

        try {
            Socket socket = new Socket(InetAddress.getByName("x.x.x.x"), 3994);
            String string = "{\"id\":1,\"method\":\"object.deleteAll\",\"params\":[\"subscriber\"]}";
            is = new DataInputStream(socket.getInputStream());
            os = new DataOutputStream(socket.getOutputStream());
            PrintWriter pw = new PrintWriter(os);
            pw.println(string);
            pw.flush();

            BufferedReader in = new BufferedReader(new InputStreamReader(is));
            JSONObject json = new JSONObject(in.readLine());
            if(!json.has("result")) {
                System.out.println(noReset);
                result = false;
            }
            is.close();
            os.close();

        } catch (IOException e) {
            result = false;
            System.out.println(noReset);
            e.printStackTrace();            
        } catch (JSONException e) {
            result = false;
            System.out.println(noReset);
            e.printStackTrace();
        }
        System.out.println(reset);
        return result;
    }

OTHER TIPS

Use Restlet:

// Create the client resource  
ClientResource resource = new ClientResource("http://restlet.org");  

// Write the response entity on the console  
resource.post(yourJsonObject).write(System.out);  

See http://restlet.org/learn/tutorial/2.1/#part02 for more details.

you can use Jackson JSON processor to convert your Java Object to JSON String and send it to server.

ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
String jsonString = ow.writeValueAsString(yourObject);

Then you can use HTTPClient to post the String to server:

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost request = new HttpPost("http://x.x.x.x:3994");
request.addHeader("Content-Type", "application/json");
request.setEntity(jsonString);
HttpResponse response = httpClient.execute(request);
...
httpClient.getConnectionManager().shutdown();

Or, if you are connected using Sockets:

Socket socket = new Socket(InetAddress.getByName("x.x.x.x"), 3994);
DataInputStream is = new DataInputStream(socket.getInputStream());
DataOutputStream os = new DataOutputStream(socket.getOutputStream());
PrintWriter pw = new PrintWriter(os);
pw.println(jsonString);
pw.flush();

BufferedReader in = new BufferedReader(new InputStreamReader(is));
JSONObject json = new JSONObject(in.readLine());
....

is.close();
os.close();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top