Friday, November 11, 2016

Android Quote App Using Retrofit 1.9


Two days ago I came across a post about creating an Inspiring Android App in One Hour. The post uses a library called Volley to consume REST API which brings a different quote every time a button is pressed. After reading the post, it made me remember that a few weeks earlier a friend asked me how I access REST APIs from Android. For the benefit of the friend, I decided to create this post using the same idea as the one from the post mentioned above.

I decided to write this post explaining how to use Retrofit 1.9 in Android which I am familiar with. Please note that by the time of this writing there is Retrofit 2 which I am yet to try.

I am not going to show you how to add create a project in Android Studio, however I will dig straight into the code. I know you know how to create the project and how to use Android Studio.

So we are going to jump straight into coding.

We start by adding our project dependencies. The dependencies I used for this project are;
  1. Butterknife
  2. Retrofit
  3. OKHttp
  4. Gson

dependencies {  
   compile fileTree(dir: 'libs', include: ['*.jar'])  
   .  
   .  
   compile 'com.jakewharton:butterknife:6.0.0'  
   compile 'com.squareup.retrofit:retrofit:1.9.0'  
   compile 'com.squareup.okhttp:okhttp:2.0.0'  
   compile 'com.google.code.gson:gson:2.3'  
 }  

That's it for adding the dependencies for my project. At this time you may sync your project to download the dependencies. Next step is to add our POJO. We create a new Java class, model.Quote.class and we paste the code below.


public class Quote {    
   @SerializedName("quoteLink")  
   @Expose  
   private String quoteLink;  
   @SerializedName("quoteAuthor")  
   @Expose  
   private String quoteAuthor;  
   @SerializedName("quoteText")  
   @Expose  
   private String quoteText;  
   @SerializedName("senderName")  
   @Expose  
   private String senderName;  
   @SerializedName("senderLink")  
   @Expose  
   private String senderLink;  
   //Getters and Setters  
   .  
   .  
 } 

Next step is to add an interface which I called QuoteService.class with the code as below.

public interface QuoteService {
    @GET("/?method=getQuote&format=json&lang=en")
    void getQuote(Callback<Quote> qt);
}

We edit our layout file to have two text views. I am using two since I am only showing the quote text and the quote author. My layout is very simple I will let the reader use their design skills to make this look beautiful.I have added two buttons which are optional here. One for New Quote and another one for Copying the quote text to clipboard. I have also added a progressbar which will be displayed as the application will be connecting and bringing the data from the server.

We have to change our activity so that the data is populated by the server. I will show you the part which is necessary for this application. I will not show you the progressbar and the clipboard copying button as those are out of the scope of this article.


//Quote.class
    private String quoteText = null;
    @InjectView(R.id.quoteView)
    TextView mQuoteView;
    @InjectView(R.id.quoteAuthor)
    TextView mQuoteAuthor;

    public static final String API_BASE_URL = "http://api.forismatic.com/api/1.0/";
    
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quote);
        ButterKnife.inject(this);
        mQuoteView.setText("From small beginnings come great things.");
        mQuoteAuthor.setText("Unknown");
        addQuote();
    }

   /*
    * Button to load a new quote and display it
    *
    */
    @OnClick(R.id.new_quote)
    public void newQuoteClicked(View v) {
        addQuote();
    }

   /*
    * Adds a quote from the API online and displays it on the screen
    *
    */
    private void addQuote() {
        if (isOnline()) {
            showProgress(true);
            RestAdapter adapter = new RestAdapter.Builder()
                    .setEndpoint(API_BASE_URL)
                    .build();

            QuoteService quoteService = adapter.create(QuoteService.class);

            quoteService.getQuote(new Callback<Quote>() {
                @Override
                public void success(Quote listQuote, Response response) {
                    //quoteText = listQuote.getQuoteText() + " - " + listQuote.getQuoteAuthor();
                    showProgress(false);
                    mQuoteView.setText(listQuote.getQuoteText());
                    mQuoteAuthor.setText(listQuote.getQuoteAuthor());
                }

                @Override
                public void failure(RetrofitError error) {
                    Log.e("retrofit", "failure: " + error.getMessage());
                }
            });
        } else {
            Toast.makeText(getBaseContext(), "Network isn't available", Toast.LENGTH_LONG).show();
        }
    } 
   /*
    * check if the device is connected to the Internet
    *
    */
    protected boolean isOnline() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        return netInfo != null && netInfo.isConnectedOrConnecting();
    }
 //Do the showProgress() and the clipboard copy
 .

At this point our application is ready. Oops wait, we must add the permissions to use the Internet in our AndroidManifest.xml file. When all is done then voila, we have a working application.

Enjoy, and thanks to forismatic.com for the API and also to iteachyouhowtocode.com for the inspiration.

Friday, January 10, 2014

Oracle Forms Reloaded

It's been ages since I last used the good old Oracle forms. I remember the days we used to make LOVs and Go_Item. Oh how times fly and how fate has taken me back to the good days. I am now a better developer and I am now back with Oracle forms. I last used Oracle forms 6i in 2004 I think. Since then I had not worked on Oracle forms but I am glad I joined a new project using Oracle forms 11g.

I may use this blog as my notepad on my new findings and I think I will write a lot this time. Stay tuned for some beginner notes etc.

Happy 2014.

Monday, August 26, 2013

Book Review: Instant PrimeFaces Starter

I am one guy who reads a lot of computer programming books. I have read on how to program using this or that tool and how to use framework this and that however, most of the time I like to go straight to writing code than reading long introductions with lots of tech jargon and confusing English. Before I write long introductions too :), let me tell you how excited I was, when I was invited to review the book Instant Primefaces Starter by Ian Hlavats. The book is short, fast and focused on developing a Primefaces applications. The author does not waste the reader's time explaining why, what and who Primefaces is but he makes short relevant introduction to the technology that is Primefaces.

From page 6 of this book the author goes straight to show you how to install the necessary tools for one to build state of the art Primefaces desktop and mobile applications. It shows you how to setup the development environment using Eclipse, Glassfish, Maven and MySQL. You will be guided on how to add Primefaces and related tools too.

The book teaches how to use Primefaces and other tools to build a website called "Show Time Guru" where people can meet and find fun and interesting things to do in their city. The author showed how to use some of the rich Primefaces components in building a modern social website. I have been a Primefaces developer for sometime but I had not used "PrimePush" and neither had I used "Primefaces mobile". After reading this book, Ian has opened my eyes to a lot of new possibilities I had never bothered to try using Primefaces.

This book has something for a new Primefaces developer as well as tons of nuggets of code for the experienced  Primefaces developer. The step by step approach to building a functional website using such a powerful framework makes this book a great read. The foreword was penned by Çağatay Çivici the author of Primefaces. Oh and did I mention the book is short. I know reading 250+ pages can intimidate anyone but this packs all the fun of learning and developing a functional website in just about 90 pages. Save for some code samples with bad indentation, I think this is a small rich oasis of Primefaces knowledge.

Wednesday, October 31, 2012

Accessing Jasper Reports from JSF application

I was asked by one of our client if I can prepare a reporting platform which they can access from the internet. So I made the following simple demo to show them how it can be implemented. I thought it nice to share with others. I will assume that you have JSF, hibernate and iReports knowledge. The demo is just how I put the technologies together.

Tools Used
  1. Netbeans 7.2
  2. iReports 4.7.1
  3. Oracle 10g XE database
  4. Glassfish*
  5. Primefaces*
  6. Hibernate*
*These come bundled with Netbeans IDE

Designing the Report Layout

The idea for the mock report was to show employees grouped by their manager. We will use Oracle SCOTT schema which comes bundled with Oracle databases.
To make the reports creation easy I created the report layout with the SQL from the report I will use. This is nice since I could preview my report with the data I will put. I trust you can create a report in iReport however I will attach the JRXML file for convenience of creating a similar report layout. I called the report file,
  1: EmployeesReportToJSFApp.jrxml


Which I compiled in iReport to a .jasper file.

The JSF Application



Just to make the application function like a normal report we will add a p: datatable to show the report elements before exporting it. The advantage of using Netbeans IDE is that it comes bundled with Primefaces, Hibernate and Glassfish server. We will create a new Web Project. I called mine ScottReportsOne but you can call it anything. I add framework JSF2 and choose Primefaces as the faces components. We will also add hibernate and a data source connection to our Oracle XE database.


Then Netbeans will create two JSF files, index.xhtml and welcomePrimefaces.xhtml and a hibernate.cfg.xml file for hibernate data source.


Managed Beans, DAOs and Entities


Our report will use a native query to populate the report. We will add the following entity to the database. Note this is just a normal POJO file and it does not have to be registered anyway in hibernate configuration. or any xml file for that matter.


EmployeesReport.java


  1: package com.tamla.model.entities;
  2: 
  3: public class EmployeesReport {
  4: 
  5:     private java.math.BigDecimal empNumber;
  6:     private String empName;
  7:     private String jobTitle;
  8:     private String empManager;
  9:     //getters and setters
 10: 
 11: }


We will create a the query to populate the file in,


EmployeeReportDao.java


  1: public class EmployeeReportDao implements Serializable{
  2: 
  3:     Session session = HibernateUtil.getSessionFactory().openSession();
  4: 
  5:     public List<EmployeesReport> pullReportAllEmployees() {
  6:         String queryString = "select a.empno as \"empNumber\", a.ename as \"empName\", a.job as \"jobTitle\",\n"
  7:                 + "       b.ename as \"empManager\"\n"
  8:                 + "  from emp a, emp b\n"
  9:                 + " where b.empno = a.mgr order by 4";
 10:         Query query = session.createSQLQuery(queryString).setResultTransformer(Transformers.aliasToBean(com.tamla.model.entities.EmployeesReport.class));
 11:         List<EmployeesReport> result = query.list();
 12:         return result;
 13:     }
 14: }


Notice that we have to create a HibernateUtil.java class first for this piece of code to work. In Netbeans this can be created automatically for you. We use hibernate transformers to transform the list from the query into a collection list of EmployeesReport class.


Next we create a managed bean to stitch the data objects to the JSF views. The managed bean is as below.


ReportsManagedBean.java


  1: @ManagedBean
  2: @ViewScoped
  3: public class ReportsManagedBean{
  4: 
  5:     private List<EmployeesReport> employeesReportList;
  6:     private JasperPrint jasperPrint;
  7: 
  8:     public ReportsManagedBean() {
  9:     }
 10: 
 11:     public void populateReport() {
 12:         EmployeeReportDao dao = new EmployeeReportDao();
 13:         employeesReportList = dao.pullReportAllEmployees();
 14:     }
 15: 
 16:     public List<EmployeesReport> getEmployeesReportList() {
 17:         return employeesReportList;
 18:     }
 19: 
 20:     
 21:     public void setEmployeesReportList(List<EmployeesReport> employeesReportList) {
 22:         this.employeesReportList = employeesReportList;
 23:     }
 24: 
 25:     public void reportBuilder() throws JRException {
 26:         JRBeanCollectionDataSource beanCollectionDataSource = new JRBeanCollectionDataSource(employeesReportList);
 27:         String report = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/resources/reports/EmployeesReportToJSFApp.jasper");
 28:         jasperPrint = JasperFillManager.fillReport(report, new HashMap(), beanCollectionDataSource);
 29:     }
 30: 
 31:     public void exportToPdf(ActionEvent actionEvent) throws JRException, IOException {
 32:         reportBuilder();
 33:         HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
 34:         httpServletResponse.addHeader("Content-disposition", "attachment; filename=Employees_List.pdf");
 35:         ServletOutputStream servletStream = httpServletResponse.getOutputStream();
 36:         JasperExportManager.exportReportToPdfStream(jasperPrint, servletStream);
 37:         FacesContext.getCurrentInstance().responseComplete();
 38:     }
 39: 
 40:     public void exportToXlsx(ActionEvent actionEvent) throws JRException, IOException {
 41:         reportBuilder();
 42:         HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
 43:         httpServletResponse.addHeader("Content-disposition", "attachment; filename=Employees_List.xlsx");
 44:         ServletOutputStream servletOutputStream = httpServletResponse.getOutputStream();
 45:         JRXlsxExporter docxExporter = new JRXlsxExporter();
 46:         docxExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
 47:         docxExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, servletOutputStream);
 48:         docxExporter.exportReport();
 49:         FacesContext.getCurrentInstance().responseComplete();
 50:     }
 51: }
 52: 



If  you don’t have the Jasper reports dependencies you should download them and add to the classpath. The required dependencies generating PDF and XLS jasper reports are.

  • jasperreports
  • commons-beanutils
  • commons-collections
  • commons-digester
  • commons-javaflow
  • commons-logging
  • groovy-all
  • iText
  • poi


Note all these dependencies can be downloaded together with the jasper reports community library from here. Just click on the download library button and they require you to login to access the downloads.


JSF


The java part is done next is the JSF. For the JSF we will only show the data in a p:datatable.  The table and the reports structure for this minimum example are different. For JSF instead of creating a new file for the demo I decided to use the already created template from Netbeans.


welcomePrimefaces.xhtml


  1: <p:layoutUnit position="center">   
  2:                     <p:panel header="Employee Report" style="font-size: 12px;">
  3:                         <h:form>
  4:                             <p:commandButton value="Populate Table" actionListener="#{reportsManagedBean.populateReport()}" update="myTable" />
  5:                             <p:dataTable value="#{reportsManagedBean.employeesReportList}" var="report" id="myTable"  >
  6:                                 <f:facet name="header">
  7:                                     List of Employees 
  8:                                 </f:facet>
  9:                                 <p:column >
 10:                                     <f:facet name="header">
 11:                                         Employee No.
 12:                                     </f:facet>
 13:                                     <h:outputText value="#{report.empNumber}" style="font-size: 12px;"  />                   
 14:                                 </p:column>
 15:                                 <p:column  >
 16:                                     <f:facet name="header">
 17:                                         Employee Name
 18:                                     </f:facet>
 19:                                     <h:outputText value="#{report.empName}" style="font-size: 12px;"  />                   
 20:                                 </p:column>                                
 21:                                 <p:column  >
 22:                                     <f:facet name="header">
 23:                                         Job Title
 24:                                     </f:facet>
 25:                                     <h:outputText value="#{report.jobTitle}" style="font-size: 12px;"  />                   
 26:                                 </p:column>
 27:                                 <p:column >
 28:                                     <f:facet name="header">
 29:                                         Reports To
 30:                                     </f:facet>
 31:                                     <h:outputText value="#{report.empManager}" style="font-size: 12px;"  />                   
 32:                                 </p:column>
 33:                             </p:dataTable>
 34:                             <p:toolbarGroup align="center" >
 35:                                 <p:commandButton value="Export To PDF" id="exportToPdfButton" actionListener="#{reportsManagedBean.exportToPdf}" ajax="false" />
 36:                                 <p:spacer width="10"/>
 37:                                 <p:commandButton value="Export To Excel" id="exportToXlsxButton" actionListener="#{reportsManagedBean.exportToXlsx}" ajax="false" />                
 38:                             </p:toolbarGroup>
 39:                         </h:form>
 40:                     </p:panel>
 41:                 </p:layoutUnit>


Notice that I only changed the p:layoutunit position=”center” to add all this. The remainder I did not change. Also note that we have put the jasper report file in the following folder ScottReportsOne\web\resources\reports\EmployeesReportToJSFApp.jasper. This file is the compiled reports file.


Running the Example



What the system is doing is just to fill the data into the report display and populating the template. A running war file with a working example can be downloaded here. Happy building reporting platforms with JSF and JasperReports.

Wednesday, May 12, 2010

Using Yii PHP framework on Netbeans 6.8

A few days ago I downloaded Yii PHP framework 1.1.2. I played around with the manual quickly to check if I can use any of the features in my future project. Yes I found Yii to be a very interesting PHP framework. The developers really did a nice job and I recommend it.

I quickly started a mock up project to see if I can use the features and the results were awesome. My system configuration is a WAMP using Netbean 6.8 IDE.

The application

Our application is a simple online shopping system.  It will be used to keep customer data for a small door to door delivery grocery shop called Matombo.  The idea is a customer registers on the company’s website. After registering the customer can order groceries online on the shop’s website. The shop then delivers the order. The order can be delivered to any address which can be serviced by the shop. For simplicity we won’t touch the payment module. The idea was borrowed from a few Zimbabwean online shops for sending groceries back home for the people in the Diaspora. Please see these website for a live system using the idea stated.

In this post and more posts to follow I will show you how I achieved such a website.

Setting up

I will put the Netbeans project in a seperate folder and will use the IDE’s capabilities to publish the files to the htdocs directory under Apache. So first we create the application using the yiic tool which is a command line tool from Yii.

  1. Start->run->cmd
  2. path=”c:\php” [put the path to your PHP directory if it’s not in system path otherwise skip this part if it’s in your path already]
  3. cd C:\JDeveloper\netbeanswork [this is the directory where I have my netbeans projects]
  4. C:\JDeveloper\yii-1.1.2.r2086\framework\yiic webapp Matombo [this will create an application called Matombo on the folder “C:\JDeveloper\netbeanswork”]

Your application will be ready and can be used from here. Now in Netbeans 6.8 IDE we create a new PHP project as follows.

ScreenHunter_01 May. 12 21.57

 

Next add the source files as illustrated below.

ScreenHunter_02 May. 12 22.01

Sources folder refer to the folder with the application which was created by the yiic tool. For project name I prefer the default one and for this application Netbeans picked Matombo.

On the run configuration step we have to setup the application URL and web root folder as illustrated below.

ScreenHunter_03 May. 12 22.11

For this application I had to move the files from the project directory to the web root.

Run As: I put the local website.

Project URL:  Do not forget to put the listening port for your application, on my Apache server it is on 3569.

Copy files to sources folder: Tick this checkbox if you have a setup like mine for moving files from one folder to the other. Put the web root folder in the Copy to folder textbox.

Click finish and the IDE will have the project loaded. I like to set the project as the main project in the IDE. This has an advantage that clicking the run button on the toolbar will run this project.

Now you have to add the includes to the IDE so that you are able to use the auto complete for the IDE.

On the project folder in the IDE right click the folder and move to Project Properties. On the dialog window move to the “PHP include files” option and add the Yii framework folder. At this point your IDE can use auto complete.

Test the auto complete by opening the PHP files in the protected->models folder and just try typing in something like

$this->

inside any function and see this pretty IDE showing the auto complete feature.

Click on the run button and the IDE runs your application.

 

On my next post we setup the database and use gii widget to create models and CRUD.

Monday, September 7, 2009

Welcome

I am a programmer who has no big project running right now. Actually as I write this I might be jobless soon. In my spare time I do battles with Oracle ADF, Adobe Flex, PHP, ICEFaces or anything interesting.

I am planning to be posting a few tutorials on this blog. Watch the space for a few how tos.