简体   繁体   中英

Implementing Java API Sample Code

I am looking into IBM's quickfile API. Now I am a front-end developer and I do not know much about Java.

I would like to know how I can implement some of the sample code they provide on the developers website : here is one of the sample codes:

Here is an example of how to use the API to get a list of users. This Java program gets the list of users using a REST request, parses the result as a regular expression, and then prints out the total number of users to the console.

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.commons.codec.binary.Base64;


public class QuickFileUserCount {
  public static void main(String[] args) {
  try {
    if (args.length < 4) {
      System.out.println("Usage is java QuickFileUserCount <host> <port> <userid> <password>");
      System.exit(0);
    }
    String quickFileServer   = args[0];
    String quickFilePort     = args[1];
    String quickFileUser     = args[2];
    String quickFilePassword = args[3];
    String quickFileUserQuery = "http://" + quickFileServer + ":" + quickFilePort + "/quickfile/rest/admin/users/0/0";
    URL url = new URL(quickFileUserQuery);
    HttpURLConnection uc = (HttpURLConnection) url.openConnection();
    uc.setRequestMethod("GET");
    uc.setDoInput(true);
    String uidpw = quickFileUser + ":" + quickFilePassword;
    String encodedPassword = Base64.encodeBase64String(uidpw.getBytes()); 
    uc.setRequestProperty("Authorization", "Basic " + encodedPassword);
    InputStream is = uc.getInputStream();
    StringBuffer sb = new StringBuffer();
    int c;
    while ((c = is.read()) != -1) {
      sb.append((char) c);
    }
    String ss = sb.toString();
    String ps = "(.*?)\"totalRows\":(.+?),.*";
    String userCount = ss.replaceAll(ps, "$2");
    System.out.println("\nNumber of QuickFile users on server <" + quickFileServer + "> = " + userCount);       
    uc.disconnect();
  } catch (Exception e) {
      System.out.println("Exception: " + e.getMessage());
    }
  }
}

Do I need to build a custom APP to see this in action? If so, how do I go about this? Since I am new at programming, where do I start?

I would appreciate any answer

I understand that by "implement" you mean that you want to take this sample code and run it.

Each Java file contains a top-level class that's named the same as the file. In this case, the class's name is QuickFileUserCount and the file should be named QuickFileUserCount.java . If the file had a "package" statement at the top, then that would specify the directory that the file is in; since it doesn't, it'll be in whatever your working directory is.

Java programs start by calling the main method on some class. Any class can have a main method, and so you specify the class that you want to start running in.

One detail that makes this example a little more involved is the import from the Apache Commons. This tells Java that the QuickFileUserCount class uses the Base64 class from Apache Commons Codec . You'll need the Commons Codec library to compile or run this example.

It's usually easiest to use an IDE such as Eclipse to develop Java applications, but for a simple trial of this example:

  • Download the Commons Codec library from the Apache Web site. Unpack commons-codec-1.8.jar into a temporary directory.
  • Save the example with the filename QuickFileUserCount.java .
  • Compile it with the following command (you'll need to have the Java Development Kit installed):

     javac -classpath commons-codec-1.8.jar QuickFileUserCount.java 

    The "classpath" option tells the Java compiler to look for the Base64 class inside that jar file.

  • Run it using the following command:

     java -classpath commons-codec-1.8.jar QuickFileUserCount 

    Note that you don't use the ".java" this time. You should see the usage statement printed out.

If your question is how to use the library (or binary ) for use in your code, then all you need to do is import the library.

Depending on which IDE you use, this can be done various ways.

In Eclipse, for example, you can go to Project > Properties > Java Build Path > Libraries and click Add External JARs... . Navigate to your JAR file (the library), and then click OK . The library should now have its resources accessible through your code, and you can run example codes like what you have now.

There are many IDEs. The way you add a library may be different for each one. It may help to look at the website for your respective IDE to find out how to import Java libraries and binaries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM