Saturday, September 24, 2011

Android - Stop/Disable Orientation

Add the Following Lines:

In AndroidManifest.xml


Find the Activity section in which you want stop/disable the orientation and add the following attribute


android:configChanges="keyboardHidden|orientation"


In the Activity(In which you want to stop/disable orientation) class add the following code:



@Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }

RESTful Webservice

RESTful Webservice:

What is REST?
REST is a term coined by Roy Fielding in his Ph.D. dissertation to describe an architecture style of networked systems. REST is an acronym standing for Representational State Transfer. Here is Roy Fielding's explanation of the meaning of Representational State Transfer:

"Representational State Transfer is intended to evoke an image of how a well-designed Web application behaves: a network of web pages (a virtual state-machine), where the user progresses through an application by selecting links (state transitions), resulting in the next page (representing the next state of the application) being transferred to the user and rendered for their use."

What is RESTful webservice?
REST is not a standard. Its just an architectural style. While REST is not a standard, it does use standards:

. HTTP
. URL
. XML/HTML/GIF/JPEG/etc (Resource Representations)
. text/xml, text/html, image/gif, image/jpeg, etc (MIME Types)

REST Web Services Characteristics:

Client-Server: a pull-based interaction style: consuming components pull representations.
Stateless: each request from client to server must contain all the information necessary to understand the request, and cannot take advantage of any stored context on the server.
Cache: to improve network efficiency responses must be capable of being labeled as cacheable or non-cacheable.
Uniform interface: all resources are accessed with a generic interface (e.g., HTTP GET, POST, PUT, DELETE).
Named resources: the system is comprised of resources which are named using a URL.
Interconnected resource representations: the representations of the resources are interconnected using URLs, thereby enabling a client to progress from one state to another.
Layered components: intermediaries, such as proxy servers, cache servers, gateways, etc, can be inserted between clients and resources to support performance, security, etc.

Principles of REST Web Service Design
  • The key to creating Web Services in a REST network (i.e., the Web) is to identify all of the conceptual entities that you wish to expose as services. Above we saw some examples of resources: parts list, detailed part data, purchase order.
  • Create a URL to each resource. The resources should be nouns, not verbs. For example, do not use this:
               http://www.parts-depot.com/parts/getPart?id=00345
               Note the verb, getPart. Instead, use a noun:
               http://www.parts-depot.com/parts/00345
  • Categorize your resources according to whether clients can just receive a representation of the resource, or whether clients can modify (add to) the resource. For the former, make those resources accessible using an HTTP GET. For the later, make those resources accessible using HTTP POST, PUT, and/or DELETE.
  • All resources accessible via HTTP GET should be side-effect free. That is, the resource should just return a representation of the resource. Invoking the resource should not result in modifying the resource.
  • No man/woman is an island. Likewise, no representation should be an island. In other words, put hyperlinks within resource representations to enable clients to drill down for more information, and/or to obtain related information.
  • Design to reveal data gradually. Don't reveal everything in a single response document. Provide hyperlinks to obtain more details.
  • Specify the format of response data using a schema (DTD, W3C Schema, RelaxNG, or Schematron). For those services that require a POST or PUT to it, also provide a schema to specify the format of the response.
  • Describe how your services are to be invoked using either a WSDL document, or simply an HTML document.

Wednesday, September 21, 2011

Android - Accessing Your Preference From Hardware Menu Button


1) Create New XML File By Clicking "File->New->Other->Android XML File.
2) In Newly Open Dialog Box:
   a) File = Name of Your Preference File Name
   b) "In What type Of Resource would you like to create?" Select "PREFERENCE"
   c) Don't Change The Folder Path
   d) "In Select the root element for XML File" Select "PREFERENCESCREEN"
   e) Click Finish.

3) Select The Type of Preference You want to add. All with very good exmaples are shown here
   http://www.kaloer.com/android-preferences

4) In "AndroidManifest.xml" add the following segment
   <activity
        android:name=".Name_of_your_preference_file">
   </activity>

5) Add New Activity which extends from PreferenceAcivity
   *To know how to add new activity take a look here
    http://codersapprentice.blogspot.com/2011/09/android-easiest-way-to-add-new-activity.html

6) In OnCreate function of the newly created Activity Add the following lines
   
    addPreferencesFromResource(R.xml.mypeference);//Your Preference XML Resource
    PreferenceManager.setDefaultValues(ApplicationPreference.this, R.xml.mypeference, false);//To Set Default Values

7) To Call Preference Screen From Hardware Menu Button Add the following code on the activity from which you want to call the preference:
   @Override
    public boolean onKeyDown(int keycode, KeyEvent event)
    {
    if(keycode == KeyEvent.KEYCODE_MENU)
    {    
    Intent settingsActivity = new Intent(getApplicationContext(),ApplicationPreference.class);
    startActivity(settingsActivity);
    }
    return super.onKeyDown(keycode,event);
    }

Android: Hardware Menu button click event

The easiest way is to capture the onKeyDown event for the menu button click:



@Override
public boolean onKeyDown(int keycode, KeyEvent event ) {
 if(keycode == KeyEvent.KEYCODE_MENU)
{
  AlertDialog.Builder dialogBuilder
  = new AlertDialog.Builder(this)
  .setMessage("Test")
  .setTitle("Menu dialog");
  dialogBuilder.create().show();
 }
 return super.onKeyDown(keycode,event); 
}

Android - Easiest Way to Add New Activity


you can create the activity via the manifest editor like this:
  1. Double click on AndroidManifest.xml in the package explorer.
  2. Click on the "Application" tab of the manifest editor
  3. Click on "Add.." under the "Application Nodes" heading (bottom left of the screen)
  4. Choose Activity from the list in the dialog that pops up (if you have the option, you want to create a new top-level element)
  5. Click on the "Name*" link under the "Attributes for" header (bottom right of the window) to create a class for the new activity.
When you click Finish from the new class dialog, it'll take you to your new activity class so you can start coding.

Sunday, September 18, 2011

Android - Integrate Jetty Server In Your Application

First Add the following 6 JARs in your project
Import the following packages:

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;



then Add the following Function:


public static final int SERVERPORT = 1234;



Handler handler = new AbstractHandler()
    {
    //@Override
public void handle(String target, Request request, HttpServletRequest MainRequestObject,
HttpServletResponse response) throws IOException, ServletException
{
try
{
//How to get Query String/
Log.i("Query String", target);

//URI format
//http://127.0.0.1:1234/Function/para1/para2

//Http Request Type: GET/POST/PUT/DELETE
Log.i("HTTP Verb", MainRequestObject.getMethod());

BufferedReader in = new BufferedReader(new InputStreamReader(MainRequestObject.getInputStream()));
String line = null;
                 
StringBuilder PostedData = new StringBuilder();

while ((line = in.readLine()) != null)
{    
Log.i("Received Message Line by Line", line);
PostedData.append(line);
}

//Http Request Data Type
Log.i("Posted Data Type", MainRequestObject.getContentType());

//Http Request Type: GET/POST/PUT/DELETE
Log.i("Posted Data", PostedData.toString());

//How To Send Responce Back
response.setContentType("text/html");
           response.setStatus(HttpServletResponse.SC_OK);
           response.getWriter().println("<h1>Hello</h1>");
           ((Request)MainRequestObject).setHandled(true);
}
        catch (Exception ex)
        {
        Log.i("Error", ex.getMessage());
}
}
    };

In "onCreate" function:

Server server = new Server(SERVERPORT);
server.setHandler(handler);
server.start();


*Now you can handle all the HTTP request from with-in your android app.