I am trying to invoke rest API using Java transformation getting compile error as cannot find symbol set Requestmethod

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.

1 answer

  • answered 2022-04-03 13:59 cyberbrain

    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.

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum