RestAPi calling using Java code, need to write connections in separate code -


i wanted hide http connections using class http connections , wanted reduce code. can me doing this?

public class exportgroups {     public static void main(string[] args) {         try {             string username = "username";             string password = "pass@word123";             string userpassword = username + ":" + password;             string encoding = new sun.misc.base64encoder().encode(userpassword.getbytes());              url url = new url("https://agilepointserver/extension/exportgroups");             httpurlconnection conn = (httpurlconnection) url.openconnection();             conn.setdooutput(true);             conn.setrequestproperty("authorization", "basic " + encoding);             conn.setrequestmethod("post");             conn.setrequestproperty("content-type", "application/json");             string input = "{\"groupnames\": [\"accounts payable\"]}";             outputstream os = conn.getoutputstream();             os.write(input.getbytes());             os.flush();             bufferedreader br = new bufferedreader(new inputstreamreader((conn.getinputstream())));              string output;             system.out.println("output server .... \n");             while ((output = br.readline()) != null) {                 system.out.println(output);             }             conn.disconnect();         } catch (malformedurlexception e) {             e.printstacktrace();         } catch (ioexception e) {             e.printstacktrace();         }     } } 

perhaps want class named "connectionfactory" handles connection initialization. can have method "createconnection" username, password, url arguments, , initializes httpurlconnection returns connection. when want create connection, call

httpurlconnection connection = connectionfactory.createconnection(username, password, url); 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -