Pages Navigation Menu

Coding is much easier than you think

What is the difference between HTTP and REST and SOAP in web services?

Posted by in Soap-UI, Web technology

 

SOAP uses WSDL for communication between consumer and provider, whereas REST just uses XML or JSON to send and receive data
 
WSDL Defines contract between client and service and is static by its nature. In case of REST contract is somewhat complicated and is defined by HTTP, URI, Media Formats and Application Specific Coordination Protocol. It’s highly dynamic unlike WSDL.
 

SOAP doesn’t return human readable result, while REST result is readable with is just plain XML or JSON

 
This is not true. Plain XML or JSON are not RESTful at all. None of them define any controls(i.e. links and link relations, method information, encoding information etc…) which is against REST as far as messages must be self contained and coordinate interaction between agent/client and service.

With links + semantic link relations clients should be able to determine what is next interaction step and follow these links and continue communication with service.

It is not necessary that messages be human readable, it’s possible to use cryptic format and build perfectly valid REST applications. It doesn’t matter whether message is human readable or not.

Thus, plain XML(application/xml) or JSON(application/json) are not sufficient formats for building REST applications. It’s always reasonable to use subset of these generic media types which have strong semantic meaning and offer enough control information(links etc…) to coordinate interactions between client and server.

REST is over only HTTP. HTTP is most widely used and when we talk about REST web services we just assume HTTP. HTTP defines interface with it’s methods(GET, POST, PUT, DELETE, PATCH etc) and various headers which can be used uniformly for interacting with resources. This uniformity can be achieved with other protocols as well.

REST permits many different data formats where as SOAP only permits XML.
While this may seem like it adds complexity to REST because you need to handle multiple formats, in my experience it has actually been quite beneficial. JSON usually is a better fit for data and parses much faster. REST allows better support for browser clients due to it’€™s support for JSON.
 

REST doesn’t add any specific functionality to HTTP but is an architectural style that was developed alongside HTTP and most commonly uses HTTP for its application layer protocol.

 
HTTP is an application protocol. REST is a set of rules, that when followed, enable you to build a distributed application that has a specific set of desirable constraints.

 
If you are looking for the most significant constraints of REST that distinguish a RESTful application from just any HTTP application, I would say the “self-description” constraint and the hypermedia constraint (aka Hypermedia as the Engine of Application State (HATEOAS)) are the most important.

The self-description constraint requires a RESTful request to be completely self descriptive in the users intent. This allows intermediaries (proxies and caches) to act on the message safely.

The HATEOAS constraint is about turning your application into a web of links where the client’s current state is based on its place in that web. It is a tricky concept and requires more time to explain than I have right now.
 

Read More

How to create transparent activity in android

Posted by in Android

 

Sometimes, we need an activity to some processing but we need not necessarily have a UI.
For this, you can do just not do a setContentView() call. This will show up an activity with the default theme applied.
 
** UPDATE: Android Complete tutorial now available here.
 
But, what if you need a transparent activity? It should be showing what is behind it and still do some stuff. Just follow this post to get it done.

Add the following style In your res/values/styles.xml file (if you don’t have one, create it.) Here’€™s a complete file:

<!--?xml version="1.0" encoding="utf-8"?-->

(the value @color/transparent is the color value #00000000 which I put in res/values/color.xml file. You can also use @android:color/transparent in later Android versions)

Then apply the style to your activity, for example:

...

 

Additionally, you can call requestWindowFeature(Window.FEATURE_NO_TITLE) to remove the title bar.

Sometimes, you may need to display a dialog on this activity. Say, you are processing something and want to show the user the progress. For this, you can create a ProgressDialog and show it. But, in case of a transparent activity, it displays an outer box for the dialog.

To get rid of this, in your onCreateDialog or the place where you are initializing the dialog do the following:

@Override
protected Dialog onCreateDialog(int id) {

switch(id) {
case 100:
dialog = new ProgressDialog(this, ProgressDialog.STYLE_SPINNER);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setMessage("Message");
//The below line does the trick!!
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
return dialog;
}
return super.onCreateDialog(id);
}

 

Read More

How to make an Android Spinner with initial text €œSelect One€

Posted by in Android

 
There is two methods to do this. One is through XML and another is through Programmatically.
 

Method 1 : XML

First, you might be interested in the “prompt” attribute of the Spinner class. See the picture below, “Choose a Planet” is the prompt that can be set in the xml with android:prompt=””

I was going to suggest subclassing Spinner, where you could maintain two adapters internally. One adapter that has the “Select One” option, and the other real adapter (with the actual options), then using the OnClickListener to switch the adapters before the choices dialog is shown.. however after trying implement that idea I’ve come to the conclusion you cannot receive OnClick events for the widget itself.

You could wrap the spinner in a different view, intercept the clicks on the view, and then tell your CustomSpinner to switch the adapter, but seems like an awful hack.

 
** UPDATE: Android Complete tutorial now available here.
 

Method 2: Programtically

Here’s a general solution that overrides the Spinner view. It overrides setAdapter() to set the initial position to -1, and proxies the supplied SpinnerAdapter to display the prompt string for position<0.

/**
 * A modified Spinner that doesn't automatically select the first entry in the list.
 *
 * Shows the prompt if nothing is selected.
 *
 * Limitations: does not display prompt if the entry list is empty.
 */
public class NoDefaultSpinner extends Spinner {

    public NoDefaultSpinner(Context context) {
        super(context);
    }

    public NoDefaultSpinner(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public NoDefaultSpinner(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void setAdapter(SpinnerAdapter orig ) {
        final SpinnerAdapter adapter = newProxy(orig);

        super.setAdapter(adapter);

        try {
            final Method m = AdapterView.class.getDeclaredMethod("setNextSelectedPositionInt",int.class);
            m.setAccessible(true);
            m.invoke(this,-1);

            final Method n = AdapterView.class.getDeclaredMethod("setSelectedPositionInt",int.class);
            n.setAccessible(true);
            n.invoke(this,-1);

        } catch( Exception e ) {
            throw new RuntimeException(e);
        }
    }

    protected SpinnerAdapter newProxy(SpinnerAdapter obj) {
        return (SpinnerAdapter) java.lang.reflect.Proxy.newProxyInstance(
                obj.getClass().getClassLoader(),
                new Class[]{SpinnerAdapter.class},
                new SpinnerAdapterProxy(obj));
    }

    /**
     * Intercepts getView() to display the prompt if position < 0
     */
    protected class SpinnerAdapterProxy implements InvocationHandler {

        protected SpinnerAdapter obj;
        protected Method getView;

        protected SpinnerAdapterProxy(SpinnerAdapter obj) {
            this.obj = obj;
            try {
                this.getView = SpinnerAdapter.class.getMethod("getView",int.class,View.class,ViewGroup.class);
            } catch( Exception e ) {
                throw new RuntimeException(e);
            }
        }

        public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
            try {
                return m.equals(getView) && (Integer)(args[0])
Read More

Debugging a service

Posted by in Android

Case:

You may have written/called a service for your application that may run at the background once it is created. But when you run your application you won€™t be able to see the services when your activity uses the services as it runs at the background and it is not visible. But once the service is completed, it will automatically get closed.  So in this case, it is not possible to debug your service like it is done manually for an activity.

Solution:

Please follow the steps below in order to debug your service,

Step 1: In the first interesting method of your service (Used in create ()):

/* (non-Javadoc)

* @see android.app.Service#onCreate()

*/

@Override

public void onCreate() {

super.onCreate();

//whatever else you have to to here…

android.os.Debug.waitForDebugger();  // this line is key

}

Step 2: Set break points after the waitForDebuggerCommand.

Step 3: Launch app via debug button in Eclipse.

Step 4: Now launch adb and run the command to start a service as follows,

cd $PLATFORM_TOOLS

adb shell

am startservice -n com.google.android.apps.gtalkservice/com.google.android.gtalkservice.service.GTalkService

Read More

how to get cookies value in java servlet

Posted by in Java

Cookies come as part of the HttpServletRequest object. The Cookie Class provides an easy way to read Cookies. You can use getCookies() method to retrieve all the cookies in your servlet program.You can gain access to any cookies sent by the browser from the javax.servlet.http.HttpServletRequest passed to the servlet’s doGet, doPost, etc methods.

This getCookies() method returns an array of cookie objects. In this example we will show you how you can retrieve all the cookies and display using servlets.

// Check for cookies
Cookie[] cookie_jar = request.getCookies();

// Check to see if any cookies exists
if (cookie_jar != null)
{
	for (int i =0; i< cookies.length; i++)
	{
		Cookie aCookie = cookie_jar[i];
		pout.println ("Name : " + aCookie.getName());
		pout.println ("Value: " + aCookie.getValue());
	}
}
Read More
Page 3 of 1512345...10...Last»