Java HTTP POST方法调用API而不使用主体的参数名

Java HTTP POST方法调用API而不使用主体的参数名,java,api,http,post,nimble,Java,Api,Http,Post,Nimble,我正在使用Java。如何生成HTTP POST调用API并仅在正文中通知“JSON”值(不带参数名) 根据示例,调用此URL:(POST方法)和in body将仅具有以下内容(不带参数名称): 如果这是正确的,请有人给我举个例子?将此库用于网络部件: 将此库用于json部件: 例如: public String examplePost(DataObject data) { HttpClient httpClient = new DefaultHttpClient();

我正在使用Java。如何生成HTTP POST调用API并仅在正文中通知“JSON”值(不带参数名)

根据示例,调用此URL:(POST方法)和in body将仅具有以下内容(不带参数名称):


如果这是正确的,请有人给我举个例子?

将此库用于网络部件:


将此库用于json部件:

例如:

public String examplePost(DataObject data) {
        HttpClient httpClient = new DefaultHttpClient();

        try {
            HttpPost httppost = new HttpPost("your url");
            // serialization of data into json
            Gson gson = new GsonBuilder().serializeNulls().create();
            String json = gson.toJson(data);
            httppost.addHeader("content-type", "application/json");

            // creating the entity to send
            ByteArrayEntity toSend = new ByteArrayEntity(json.getBytes());
            httppost.setEntity(toSend);

            HttpResponse response = httpClient.execute(httppost);
            String status = "" + response.getStatusLine();
            System.out.println(status);
            HttpEntity entity = response.getEntity();

            InputStream input = entity.getContent();
            StringWriter writer = new StringWriter();
            IOUtils.copy(input, writer, "UTF8");
            String content = writer.toString();
            // do something useful with the content
            System.out.println(content);
            writer.close();
            EntityUtils.consume(entity);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            httpClient.getConnectionManager().shutdown();
        }
    }

希望能有所帮助。

非常感谢您的回复,但它不起作用,我已经修改了代码并将json值放入json var字符串中,但在启动执行之前发生错误:
code
java.util.concurrent.ExecutionException:org.apache.catalina.LifecycleeException:无法启动组件[StandardEngine[catalina].StandardHost[localhost].StandardContext[/nimble]]我正在尝试使用此代码,但我收到409 http错误:(.再次感谢!您确定您的json有效吗?json不正确,我认为“验证正确,但不正确”,“验证正确”。谢谢。
public String examplePost(DataObject data) {
        HttpClient httpClient = new DefaultHttpClient();

        try {
            HttpPost httppost = new HttpPost("your url");
            // serialization of data into json
            Gson gson = new GsonBuilder().serializeNulls().create();
            String json = gson.toJson(data);
            httppost.addHeader("content-type", "application/json");

            // creating the entity to send
            ByteArrayEntity toSend = new ByteArrayEntity(json.getBytes());
            httppost.setEntity(toSend);

            HttpResponse response = httpClient.execute(httppost);
            String status = "" + response.getStatusLine();
            System.out.println(status);
            HttpEntity entity = response.getEntity();

            InputStream input = entity.getContent();
            StringWriter writer = new StringWriter();
            IOUtils.copy(input, writer, "UTF8");
            String content = writer.toString();
            // do something useful with the content
            System.out.println(content);
            writer.close();
            EntityUtils.consume(entity);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            httpClient.getConnectionManager().shutdown();
        }
    }