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?) Commented 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;
    }
Sign up to request clarification or add additional context in comments.

8 Comments

thanks for the help , will this work for the standalone application ? as my application is not a web app .
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 ?
@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
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 .
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 .
|
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.

Comments

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.