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

in #utopian-io6 years ago

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

Sort:  

Thank you for your contribution.

  • Try to come up with new and more innovative/useful ways to utilize java application.
  • The tutorial is very simple, it only prints the values of two accounts.

Either way is well explained.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Hey @team2dev
Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!