[Java Tutorial #9] - How to create java Application to compare 2 steemit account

Words
565
Reading
3 min
Listen
Play
8y

Repository

https://github.com/dmlloyd/openjdk

What Will I Learn?

In brief, write details of what the user is going to learn in a bullet list.

  • You will learn How to create GUI form for this aplication
  • You will learn API to show profil data
  • You will learn how to get data from user input
  • You will learn how to display profil data from 2 account to form

Requirements

  • Basic knowledges about java programing
  • Basic Knowledges about API
  • Need to Install Netbeans for easy pratice

Difficulty

  • Basic

Tutorial Contents

This tutorial is a tutorial for creating an application from java. which application we will use to compare two steemit accounts. When the user inputs 2 names from the steemit account in the available textfield and press the compare button, this application will display the detailed data of the 2 accounts.

Let's start the tutorial :

  • Open Netbeans IDE software and create new project. Choose Java Application and click next. Then Rename the project and click finish.
  • Add new jFrameForm in your Source Package
  • Click on panel component and drag it to the form
  • Add Label, TextField and Button on Panel, just follow the components on picture bellow :
  • We already have a form, Now open source and start to edit the code.
  • Create The method to get Data from URL. This method in java.net.* package. So before your start use this method import the package first
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
  • Then create a method to read url from web
public String getApi(String url) throws Exception{
        URL oracle=new URL(url);
        HttpURLConnection httpcon = (HttpURLConnection) oracle.openConnection();
        httpcon.addRequestProperty("User-Agent", "Mozilla/5.0");
        BufferedReader in = new BufferedReader(new InputStreamReader(httpcon.getInputStream()));
        String inputLine;
         while ((inputLine = in.readLine()) != null){
             return inputLine;
         }  
        in.close();
        return null;   
    }
CodeExplanation
getApi()Method Name
String urlThe Parameter
throws ExceptionSome method to catch error
URL oracle=new URL(url);Decrlarating new URL
HttpURLConnection httpcon = (HttpURLConnection) oracle.openConnection();class initiation to get URL connection.
httpcon.addRequestProperty("User-Agent", "Mozilla/5.0");Add browser property, Here I add Mozilla 5.-0
BufferedReader in = new BufferedReader(new InputStreamReader(httpcon.getInputStream()));method Read input from http connection.
return inputLine;return the output
in.close();close input stream reader
  • create a method to get dan display profil data
 public void getData() throws Exception{
}
  • get the user input
String user1=account1.getText();
String user2=account2.getText();
       String Data1 = getApi("https://uploadbeta.com/api/steemit/account/profile/?cached&id="+user1);
       String Data2 = getApi("https://uploadbeta.com/api/steemit/account/profile/?cached&id="+user2);
      //we add user1 and user2 variable to get data according id from api
  • convert the result from string to JSON Object. Here we need use java-json library. Dowload it here : http://www.java2s.com/Code/Jar/j/Downloadjavajsonjar.htm . Extract and add to project Libraries. Then import it to your file, then import org.json package
  • this code to convert string to object
JSONObject ObjData1=new JSONObject(Data1);
JSONObject ObjData2=new JSONObject(Data2);
  • get data of user id and reputation from both of account then display it using setText Method like code bellow
       usernrep1.setText("@"+ObjData1.getString("id")+"  ("+ObjData1.getDouble("rep")+")");
       usernrep2.setText("@"+ObjData2.getString("id")+"  ("+ObjData2.getDouble("rep")+")");
  • get data of user created from both of account then display it using setText Method
       created1.setText("Since "+ObjData1.getString("created"));
       created2.setText("Since "+ObjData2.getString("created"));
  • get data of user steem from both of account then display it using setText Method
       steem1.setText(ObjData1.getDouble("steem")+" STEEM");
       steem2.setText(ObjData2.getDouble("steem")+" STEEM");
  • get data of user SBD from both of account then display it using setText Method
       sbd1.setText(ObjData1.getDouble("sbd")+" SBD");
       sbd2.setText(ObjData2.getDouble("sbd")+" SBD");
  • Here the full code of getData() method
    public void getData() throws Exception{
       String user1=account1.getText();
       String user2=account2.getText();
       String Data1 = getApi("https://uploadbeta.com/api/steemit/account/profile/?cached&id="+user1);
       String Data2 = getApi("https://uploadbeta.com/api/steemit/account/profile/?cached&id="+user2);
       JSONObject ObjData1=new JSONObject(Data1);
       JSONObject ObjData2=new JSONObject(Data2);
       usernrep1.setText("@"+ObjData1.getString("id")+"  ("+ObjData1.getDouble("rep")+")");
       usernrep2.setText("@"+ObjData2.getString("id")+"  ("+ObjData2.getDouble("rep")+")");
       created1.setText("Since "+ObjData1.getString("created"));
       created2.setText("Since "+ObjData2.getString("created"));
       steem1.setText(ObjData1.getDouble("steem")+" STEEM");
       steem2.setText(ObjData2.getDouble("steem")+" STEEM");
       sbd1.setText(ObjData1.getDouble("sbd")+" SBD");
       sbd2.setText(ObjData2.getDouble("sbd")+" SBD");
    }
  • Call the getData() method in compare button. Open the application design form and double click on button. then add this code.
        try {
            getData();
        } catch (Exception ex) {
            Logger.getLogger(Cmp.class.getName()).log(Level.SEVERE, null, ex);
        }
  • save all code and try to run

  • Finish, the application is running rightly. You can get the full code on my gist. The link is bellow

Curriculum

Proof of Work Done

https://gist.github.com/team2dev/7dca6692ffbf9cc0d1ac6befeb1c3ac0

[Java Tutorial #9] - How to create java Application to compare 2 st... | Ecency