我正在尝试使用Java转换获取编译错误,以调用REST API,因为找不到符号set requestMethod

发布于 2025-01-18 14:07:37 字数 1480 浏览 4 评论 0原文

以下是Java软件包和编码。

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.HttpURLConnection;
import java.util.HashMap;
import java.net.URLConnection;
import java.io.OutputStream;
import java.io.Reader;
import java.io.BufferedReader;
import java.io.InputStream;


try{
    URL url=new  URL("https://dit2--abc.com");
    HttpURLConnection conn=(HttpURLConnection)url.openConnection();
    conn.setDoOutput(true);
    conn.setRequestProperty("Authorization",access_token);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Accept","application/json");
    String requestBody ="{\"Id\":\"Id\", \"OwnerId\":\"OwnerId\"}";
    try(OutputStream os =conn.getOutputStream()){
        byte[] input=requestBody.getBytes("utf-8");
        os.write(input,0,input.length);
    } catch(Exception e) {
      System.out.println(e);
    }
   
//    String line;

    StringBuffer response= new StringBuffer();
    BufferedReader reader =new BufferedReader(new InputStreamReader(conn.getInputStream()));
    if (conn.getResponseCode() ==HttpURLConnection.HTTP_OK){
        while ((line=reader.readLine())!= null){
        response.append(line);
      }
fi= response.toString().substring(response.toString().length() -10);
}
    reader.close();
}catch(Exception e){
logInfo("Error");
fi =e.toString();
}

上面的代码是更新的代码,没有编译错误,但是工作流程继续运行并没有结束,就好像代码在循环中运行一样。

一旦检查此代码。

Below are the java packages and code written.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.HttpURLConnection;
import java.util.HashMap;
import java.net.URLConnection;
import java.io.OutputStream;
import java.io.Reader;
import java.io.BufferedReader;
import java.io.InputStream;


try{
    URL url=new  URL("https://dit2--abc.com");
    HttpURLConnection conn=(HttpURLConnection)url.openConnection();
    conn.setDoOutput(true);
    conn.setRequestProperty("Authorization",access_token);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Accept","application/json");
    String requestBody ="{\"Id\":\"Id\", \"OwnerId\":\"OwnerId\"}";
    try(OutputStream os =conn.getOutputStream()){
        byte[] input=requestBody.getBytes("utf-8");
        os.write(input,0,input.length);
    } catch(Exception e) {
      System.out.println(e);
    }
   
//    String line;

    StringBuffer response= new StringBuffer();
    BufferedReader reader =new BufferedReader(new InputStreamReader(conn.getInputStream()));
    if (conn.getResponseCode() ==HttpURLConnection.HTTP_OK){
        while ((line=reader.readLine())!= null){
        response.append(line);
      }
fi= response.toString().substring(response.toString().length() -10);
}
    reader.close();
}catch(Exception e){
logInfo("Error");
fi =e.toString();
}

The above code is updated code , after compiling no errors but the workflow keeps on running does not end, as if the code is running in loop.

Kindly once check this code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不甘平庸 2025-01-25 14:07:37

我会尝试一一检查错误,即使是屏幕截图中未显示的错误:

第 200 行:构造函数 Url() 的大写/小写问题,应该是 URL url = new URL("https://ditabc.com");

第 210 行:需要为 e 声明 Exception 类,因此 } catch (java.io.IOException e) { 这里是正确的。

第 213 行:这里不能执行 os.flush(); ,因为 os 只能在上面的 try-with-resources 块中知道。当 try-with-resources 块完成时, os 也会自动关闭,因此根本不需要刷新。

第 217 行:充满拼写错误,应该是 BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

第 218 行:您无法比较 StringBuffer 响应甚至还没有用整数常量读出。应该是 if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {

第 223 行:您没有要关闭的 writer,只需删除此行即可。

第225行:这个问题是218行的if (response==HttpURLConnection.HTTP_OK){导致的,没有正确关闭。您已经弄乱了缩进,因此您无法轻易看到它,但是在 fi= response.toString().substring(response.toString().length() -10); 行之后222 缺少 }

第 225 行之后的所有错误都在生成的代码中,因此它们可能来自我已经提到的缺少的 }

您没有以类似的方式处理所有异常,您确定这是正确的方法吗?
我非常确定第 207 至 212 行中的内部 try-with-resources 块会引起麻烦:如果您无法成功写出请求,则随后尝试读取响应是没有意义的。我会简单地获取没有额外 try-catch 块的 os,然后关闭它,而不是第 223 行中的 writer。

此外,您还缺少一个重要步骤:您不连接。
在执行 OutputStream os = conn.getOutputStream() 之前添加 conn.connect()

您粘贴的代码还缺少几个 import 语句,这些语句将无法编译:(

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

我在代码中看到代码生成器将插入导入的部分,并且在问题的先前版本中其中一些在额外的代码中。您需要以某种方式导入上述类,但不能与您自己的代码片段一起导入。)

作为一般建议:不要尝试使用该工具学习 Java,使用为您生成代码的开发环境确实很困难提供没有语法突出显示等的编辑器。(我从类似工具的经验中知道这一点。)

更新:在第 214 行的 try-with-resources 块中,您首先错过了上述导入之一(您导入了 java.io.OutputStreamReader ,但它必须是java.io.OutputStream),并且关键字new在这里令人不安,请删除它。

I will try to go through the errors one by one, even for errors that are not shown on your screenshots:

Line 200: upper/lowercase problem with the constructor Url(), should be URL url = new URL("https://ditabc.com");

Line 210: you need to declare the Exception class for e, so } catch (java.io.IOException e) { would be correct here.

Line 213: you cannot do os.flush(); here, as os is only known inside the try-with-resources block above. os also will automatically be closed when that try-with-resources block is finished, so flush should not be necessary at all.

Line 217: full of typos, should be BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

Line 218: you cannot compare the StringBuffer response that has not even be read out with an integer constant. Should be if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {

Line 223: you don't have a writer to close, just drop this line.

Line 225: this problem is caused by the if (response==HttpURLConnection.HTTP_OK){ in line 218. It is not properly closed. You have messed up the indenting a bit so you cannot easily see it, but after the fi= response.toString().substring(response.toString().length() -10); in line 222 there is a } missing.

All errors after line 225 are in generated code, so they probably come from the missing } that I already mentioned.

You are not handling all exceptions similar, are you sure this is the correct way?
I'm quite sure that the inner try-with-resources block in the lines 207 to 212 will cause trouble: if you cannot write out the request successfully, it does not make sense to try to read the response afterwards. I would simply get the os without an extra try-catch block, and close it instead of the writer in line 223.

Also you are missing an important step: you do not connect.
Add a conn.connect() before you do the OutputStream os = conn.getOutputStream().

The code you pasted is also missing several import statements, these will miss to make it compile:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

(I saw a section in the code where the code generator would insert imports, and in a previous version of your question you had some of them in an extra piece of code. You will need to import the above classes somehow, but not in one piece with your own code snippet.)

As a general advice: don't try to learn Java with that tool, it is really hard to work with a development environment that generates code for you and only provides editors without syntax highlighting etc. (I know that from experience with similar tools.)

UPDATE: in the try-with-resources block at line 214 you first miss one of the above imports (you imported java.io.OutputStreamReader, but it has to be java.io.OutputStream) and the keyword new is disturbing here, please remove it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文