将数据发布到URL时,java.lang.RuntimeException:失败:HTTP错误代码:200。是否发生。。?

将数据发布到URL时,java.lang.RuntimeException:失败:HTTP错误代码:200。是否发生。。?,java,json,Java,Json,在下面给定的类中,我试图将JSON对象写入给定的HttpURL连接。但当我尝试运行我的类时,它给了我一些值,我已经打印了这些值,但以一个错误结束。当我在每个JSON对象中调试它时,它会得到值,最后我将所有JSON对象放在一个JSON对象中,然后尝试写入url,但最后出现以下错误 下面给出了主类,其中我编写了获取系统信息的代码。在这里,我想获取系统信息,然后想将该信息发送到url,以便可以从该url获取该信息。但是在获得值之后,它以一个错误结束 public class NetClientPost

在下面给定的类中,我试图将JSON对象写入给定的HttpURL连接。但当我尝试运行我的类时,它给了我一些值,我已经打印了这些值,但以一个错误结束。当我在每个JSON对象中调试它时,它会得到值,最后我将所有JSON对象放在一个JSON对象中,然后尝试写入url,但最后出现以下错误

下面给出了主类,其中我编写了获取系统信息的代码。在这里,我想获取系统信息,然后想将该信息发送到url,以便可以从该url获取该信息。但是在获得值之后,它以一个错误结束

public class NetClientPost {

    public static void main(String[] args) {


        try {

            URL url = new URL(
                    "http://projects.kpmpjdc.org/artist/artist_api/test");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json");
            JSONObject obj = new JSONObject();




            JSONObject obj1 = new JSONObject();

            OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
            for (Method method : operatingSystemMXBean.getClass().getDeclaredMethods()) {
                method.setAccessible(true);
                if (method.getName().startsWith("get") 
                        && Modifier.isPublic(method.getModifiers())) {
                    Object value;
                    try {
                        value = method.invoke(operatingSystemMXBean);
                    } catch (Exception e) {
                        value = e;
                    }
                    // try
                    System.out.println(method.getName() + " = " + value);
                    obj1.put(method.getName(), value);
                }}


FileSystemView filesystemBean = FileSystemView.getFileSystemView();
File[] roots = filesystemBean.getRoots();
JSONObject obj2 = new JSONObject();
obj2.put("Roots", roots[0]);

filesystemBean.getHomeDirectory();

obj2.put("HomeDirectory", filesystemBean.getHomeDirectory());
File[] f = File.listRoots();
obj.put("RD", obj2);
for (int i = 0; i<f.length; i++)
{
    JSONObject temp = new JSONObject();
    temp.put("Drive", f[i]);
    temp.put("Display name", filesystemBean.getSystemDisplayName(f[i]));
    temp.put("Is drive", filesystemBean.isDrive(f[i]));
    temp.put("Is floppy", filesystemBean.isFloppyDrive(f[i]));
    temp.put("Readable", f[i].canRead());
    temp.put("Writable", f[i].canWrite());
    temp.put("Total Space", f[i].getTotalSpace());
    temp.put("Space Use", f[i].getFreeSpace());
    temp.put("Space Free", f[i].getUsableSpace());

        obj.put("TEMP", temp);



}


            obj.put("SystemInfo1", obj1);
            OutputStreamWriter os = new OutputStreamWriter(conn.getOutputStream());
            os.write(obj.toString());
            System.out.println(obj.toString());
            os.flush();
        if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
                throw new RuntimeException("Failed : HTTP error code : "
                        + conn.getResponseCode());
            }

            BufferedReader br = new BufferedReader(new InputStreamReader(
                    (conn.getInputStream())));

            String output;
            System.out.println("Output from Server .... \n");
            while ((output = br.readLine()) != null) {

                System.out.println(output);
            }

            conn.disconnect();

        } catch (MalformedURLException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();

        }

    }

当您回复代码为200时,您在这里手动抛出异常,这意味着绝对成功。删除该行并使用“| | conn.getResponseCode!”扩展if语句HttpURLConnection.OK'可能也有意义。我正在与此进行比较。等于201。所以我需要做出回应。否则我将无法从服务器获取任何输出。要检索201响应,您应该使服务器端返回201,而不是200。以只读方式显示客户端响应代码。此外,我强烈主张抛出捕获的异常,而不是运行时异常。
Exception in thread "main" java.lang.RuntimeException: Failed : HTTP error code : 200
    at com.wiesoftware.rest.client.NetClientPost.main(NetClientPost.java:142)
    if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
            throw new RuntimeException("Failed : HTTP error code : "
                    + conn.getResponseCode());
        }