HTTP POST using JSON in Java

前端 未结 11 1216
南笙
南笙 2020-11-22 07:24

I would like to make a simple HTTP POST using JSON in Java.

Let\'s say the URL is www.site.com

and it takes in the value {\"name\":\"mynam

11条回答
  •  无人共我
    2020-11-22 08:20

    protected void sendJson(final String play, final String prop) {
         Thread t = new Thread() {
         public void run() {
            Looper.prepare(); //For Preparing Message Pool for the childThread
            HttpClient client = new DefaultHttpClient();
            HttpConnectionParams.setConnectionTimeout(client.getParams(), 1000); //Timeout Limit
            HttpResponse response;
            JSONObject json = new JSONObject();
    
                try {
                    HttpPost post = new HttpPost("http://192.168.0.44:80");
                    json.put("play", play);
                    json.put("Properties", prop);
                    StringEntity se = new StringEntity(json.toString());
                    se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
                    post.setEntity(se);
                    response = client.execute(post);
    
                    /*Checking response */
                    if (response != null) {
                        InputStream in = response.getEntity().getContent(); //Get the data in the entity
                    }
    
                } catch (Exception e) {
                    e.printStackTrace();
                    showMessage("Error", "Cannot Estabilish Connection");
                }
    
                Looper.loop(); //Loop in the message queue
            }
        };
        t.start();
    }
    

提交回复
热议问题