我想在Java中使用JSON进行简单的HTTP POST。
假设网址是 www.site.com
www.site.com
并采用{"name":"myname","age":"20"}标记'details'为例如的值。
{"name":"myname","age":"20"}
'details'
我将如何为POST创建语法?
我似乎也无法在JSON Javadocs中找到POST方法。
这是你需要做的:
代码大致看起来像(你仍然需要对其进行调试并使之正常工作)
//Deprecated //HttpClient httpClient = new DefaultHttpClient(); HttpClient httpClient = HttpClientBuilder.create().build(); //Use this instead try { HttpPost request = new HttpPost("http://yoururl"); StringEntity params =new StringEntity("details={\"name\":\"myname\",\"age\":\"20\"} "); request.addHeader("content-type", "application/x-www-form-urlencoded"); request.setEntity(params); HttpResponse response = httpClient.execute(request); //handle response here... }catch (Exception ex) { //handle exception here } finally { //Deprecated //httpClient.getConnectionManager().shutdown(); }