1

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.

1
  • what is the advantage to send it as an Object rather than just as a JSON string? What is the server? (HTTP? / Socket?) Mar 4, 2014 at 4:17

3 Answers 3

1

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;
    }
8
  • thanks for the help , will this work for the standalone application ? as my application is not a web app . Mar 4, 2014 at 7:44
  • aaah thanks a lot its worked :) m able to send the request to server . However i m gettign an error : net.sf.json.JSONException: Error while setting property=requestStatus type class java.lang.String , could you please suggest me what could be the reason of this error ? Mar 4, 2014 at 8:11
  • @user3377374 i didnt get your point paste the error in other question and acpet this answer beacuse i dont know where the error is occuring its difficult to read code in comment
    – Engineer
    Mar 4, 2014 at 8:16
  • ok i will post new question for the error . Yes this is a right answer becoz now i am able to send the JSON object to server . Mar 4, 2014 at 8:18
  • ok sure , m sorry m new :( i didnt knew that , well where is the button to click for the right answer i will do that . please elt me know . Mar 4, 2014 at 8:40
0

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.

0

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();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.