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

时间:2015-01-02 11:10:59

标签: java json

在下面给出的类中,我试图将一个JSON对象写入Given 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();

        }

    }

运行后出现错误

Exception in thread "main" java.lang.RuntimeException: Failed : HTTP error code : 200
    at com.wiesoftware.rest.client.NetClientPost.main(NetClientPost.java:142)

1 个答案:

答案 0 :(得分:4)

    if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
            throw new RuntimeException("Failed : HTTP error code : "
                    + conn.getResponseCode());
        }
当你回复代码是200时,你在这里手动抛出异常,这意味着绝对成功。删除该行并应该工作