Wednesday, September 18, 2013

Connecting to the URL

Today I will be adding to my Blog App by getting data from the web and putting it into my app.

The way we do this is that

1)  We get data from the web
2)  Request data from the world wide web
3) Parse the response
4)Handle errors

If something goes wrong we throw an exception which lets us know something is wrong.

public static final int NUMBER_OF_POST = 20;  /*final constant number that doesn't change; can use 20 in multiple places and only have to update in one place*/
    public static final String TAG = MainListActivity.class.getSimpleName(); /* variables are in all caps b/c it lets us know that these variables hold constant variables and cannot be changed */

The way we find this is by using:  malFormedUrl exception which This exception is thrown when a program attempts to create an URL from an incorrect specification.

We also use the method URL:  Uniform Resource Locator that identifies the location of an Internet resource as specified by RFC 1738.

Putting it together we have:


        try{
            URL blogFeedUrl = new URL("http://blog.teamtreehouse.com/api/get_recent_summary/?count=" + NUMBER_OF_POST);
            }
            catch(MalformedURLException e){
                Log.e(TAG, "Exception caught: ", e);
            }

For more try catch block examples and proper practices visit:
http://source.android.com/source/code-style.html
MalformedURL method
http://developer.android.com/reference/java/net/MalformedURLException.html
URL method
http://developer.android.com/reference/java/net/URL.html

Questions:

Inside onCreate(), declare a URL variable named "treehouseUrl" and initialize it with the URL constructor, using the "URL" member variable as the parameter. Put this line inside a "try" block and then catch a MalformedURLException named "e". Don't do anything in the "catch" block yet.

      try{
      URL treehouseURL = new URL(URL);
      }
      catch(MalformedURLException e){
        Log.e("DOG", "Exception caught: ", e);  // e catches the exception, param 2 is a string, param 1 is a string
      }



try{
            URL blogFeedUrl = new URL("http://blog.teamtreehouse.com/api/get_recent_summary/?count=" + NUMBER_OF_POST);
            HttpURLConnection connection = (HttpURLConnection) blogFeedUrl.openConnection();
            connection.connect();
           
            int responseCode = connection.getResponseCode();
            Log.i(TAG, "Code: " + responseCode);
        }
            catch(MalformedURLException e){ /*only executed when there is a malformed URL, try to create a URL object then run extra code */
                Log.e(TAG, "Exception caught: ", e);
            }
           catch(IOException e){
               Log.e(TAG, "Exception caught: ", e);
           }
        catch(Exception e){
            Log.e(TAG, "Exception caught: ", e);   /*a catch of a generic exception will catch everything and none of the other catch blocks would be used */
        }

 Network connections:  2 kinds

1)HttpURLConnection  2) Apache HTTP Client 

1)HTTP URL Connection 

  • Lightweight HTTP application
  • Suitable for most applications
  • Automatically connects to SNI (server name indication) allows multiple HTTPS host to share an IP address
  • Fully cached responses from local storage (Because no network connection needs to be made such responses are available immediately.)
  • uncached responses are from the internet, placed in the response cache from the internet

2) Apache HTTP Client

  • 1) Large API; hard to improve
  • 2) Implementation is stable
  • 3) Android team working sparsely on it

HTTP URL CONNECTION vs Apache HTTP Client: 

    which to use:

  • Use Apache HTTP Client for Eclair and Froyo
  • Use HttpURLConnection for Gingerbread and above

Using HTTPConnection:
http://developer.android.com/reference/java/net/HttpURLConnection.html
Using IO Exceptions:
http://developer.android.com/reference/java/io/IOException.html
Choosing the right Client for your app:
android-developers.blogspot.com/2011/09/androids-http-clhttp://android-developers.blogspot.com/2011/09/androids-http-clients.htmlients.html


Questions:

Inside the "try" block, add an HttpURLConnection variable named "connection". Initialize it using the openConnection() method of the treehouseUrl variable. The openConnection() method returns a URLConnection object, so don't forget to cast it to HttpURLConnection.

 try {
            URL treehouseUrl = new URL(URL);
            HttpURLConnection connection = (HttpURLConnection) treehouseUrl.openConnection();
        }
        catch (MalformedURLException e) {
            Log.e("CodeChallenge", "MalformedURLException caught!", e);
        }
        catch (IOException e) {
          
        }

 

Saturday, September 7, 2013

Finishing my first Application

  1. Toast Notifications
  2. Log/debugging
  3. Getting ready for Google Play
1. Toast Notifications: these are limited popup notifications that fade in and out automatically. They do not require user input. Things to consider before reading: Gravity is a method that allows you to manipulate the area you want to set the toast. Because we are using static , we do not need to use the new keyword. This refers to the current class you are referring too.
  1. Toast notifications


  • How to make it with 3 lines
    • String toastMessage = "Yay! Our acitivy was created!";
    • Toast.makeText(this, toastMessage, Toast.LENGTH_LONG);
    • welcomeToast.show();
  • How to make it in two lines:
    • Toast.makeText(this, "Yay! Our Activity was created!";
    • Toast.LENGTH_LONG).show();
  • How to manipulate the toast to make it show up where you want it on the  screen:
    • Toast welcomeToast = Toast.makeText(this, "Look here!", Toast.LENGTH_LONG);
    • welcomeToast.setGravity(Gravity.TOP,0,0);
    • welcomeToast.show();   
    2. Debugging
    • Logs: helpful information of why an application has crashed
      • writing data to the log and how to view the log
      • use logcat view
      • e: errors, w: warn, i: info, D: Debug, v: verbose      

    • log.d("TAG", We're logging from the onCreate() method!");
    • public static final String TAG = MainActivity.class.getName(); 


    Google play steps:
    1.  Develop the app
    2.  Sign up for a publisher account on Google Play
    3.  Sign up for a publisher for a one time fee of 25 dollars.
    4.  Fill out a form to add a new app to your account.
    5.  Publish the application(No review process)

    *Google gets 25% of profits from the app(you get 75%)

    Cannot let com.example.crystal.ball on Google Play, must change example name.

    i.e. com.MichaelTo.crystalball

    Publishing on Google Play => build the release ready APK => has debut certificate

    private Digital Signature

    go to file = > export = > expand android = > create new keystore
     * You usually have one keystore for all applications which holds your private key to sign into all apps.

    i.e.  Doan.keystore   pw: KidKuddi  alias: Doan_key

    Upload application => choose file => open(crystalball app)  => upload

    Create screen shots by using the DDMS perspective view: select emulator and camera, icon ==> save

    Now you're done. 

    Tuesday, September 3, 2013

    Making my First APP

    As of right now, I am in the process of writing an Android App using

    Animation Drawable: frame by frame animation
             -drawable-mdpi which is the medium resolution picture
             -we use xml for the animation b/c it keeps the design flow organized and to keep designs
    Tween Animations: Alpha Animations: changing alpha property in the text from 0 to 1
             -call it a tween animation b/c it allows you to go between 0 which is transparent to 1 which is opaque.
             -allows you to set the duration of how long the animation plays
              -allows you to set the fill which sets whether or not the change of the opaque/transparency persists                 when the animation is done
    Sound: MediaPlayer: allows you to play sound
               -MediaPlayer is set to play streams
               -set a new folder is the res folder and name it what you need to name it and instantiate it
               -MediaPlayer must call release() on the media player so not to many instances of it will call it again
    OnCompletion listener: listens for when MediaPlayer is done playing to release media player

    Sensors: Acelerometer: senses the movement
                 -use the sensorManager gives us the system service that manages senses.
                 -Accelerometer is a sensor that we want to listen to
                 -SENSOR_DELAY_UI:  used for the speed of the interface which is 2, sutiable for the UI
    ***System services are constantly running in the background, we need to stop using system services to go to the background****Can kill battery life faster if you don't stop it so we use:
    use Base Activity Class: onResume, onPause, onStop: override the Activity class to save battery life
    Activity-Graph





    ----I'll try to get pics up about this app to you...coming soon.


    Thursday, May 9, 2013

    First Day: consistency and earnestness

    Today will be my first day journeying into making both Iphone and Android applications knowing that this is not an easy journey at all, it will take time and effort at its best.  Rome was not built in a day kind of mentality I will try to work consistently and earnestly to deliver what I call to you a complete product.

    In these blogs I will keep you up to date on the newest or what I think are the newest trends of coding and improvements of my code.  I will post up my progress and some of my general code.  Anything too complicated I will make into a facade...can't give all my secrets away ;).

    Hope that you'll keep up and learn something.  Stay tuned.