Wednesday, October 17, 2012

SOAP Request Bean Designing with implements KvmSerializable


import java.util.Hashtable;

import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;

public class UserLoginBean implements KvmSerializable {

       public static Class UserLoginBean_CLASS = UserLoginBean.class;

       private String UserName;
       private String Password;
       private String Status;

       public UserLoginBean() {
       }

       public UserLoginBean(SoapObject response) {
              this.UserName = response.getProperty("UserName").toString();
              this.Password = response.getProperty("Password").toString();
              this.Status = response.getProperty("Status").toString();
       }

       public String getUserName() {
              return UserName;
       }

       public void setUserName(String userName) {
              UserName = userName;
       }

      
       public String getPassword() {
              return Password;
       }

       public void setPassword(String password) {
              Password = password;
       }

      
       public String getStatus() {
              return Status;
       }

       public void setStatus(String status) {
              Status = status;
       }

       @Override
       public Object getProperty(int index) {
              Object object = null;
              switch (index) {
              case 0:
                     object = this.UserName;
              case 1:
                     object = this.Password;
                     break;
              case 2:
                     object = this.Status;
                     break;
              }
              return object;
       }

       @Override
       public int getPropertyCount() {
              return 3;
       }

       @Override
       public void getPropertyInfo(int index, Hashtable arg1,
                     PropertyInfo propertyInfo) {
              switch (index) {
              case 0:
                     propertyInfo.name = "UserName";
                     propertyInfo.type = PropertyInfo.STRING_CLASS;
                     break;
             
              case 1:
                     propertyInfo.name = "Password";
                     propertyInfo.type = PropertyInfo.STRING_CLASS;
                     break;
             
              case 2:
                     propertyInfo.name = "Status";
                     propertyInfo.type = PropertyInfo.STRING_CLASS;
                     break;
              }

       }

       @Override
       public void setProperty(int index, Object object) {

              switch (index) {
              case 0:
                     this.UserName = object.toString();
                     break;
             
              case 1:
                     this.Password = object.toString();
                     break;
             
              case 2:
                     this.Status = object.toString();
                     break;
              }
       }

}

SOAP Service Caller Method Which will return Response as OBJECT




public Object call(String soapAction, SoapSerializationEnvelope envelope,
                     String URL) {

              Object result = null;

              Marshal dateMarshal = new MarshalDate();
              dateMarshal.register(envelope);
              Marshal floatMarshal = new MarshalFloat();
              floatMarshal.register(envelope);

              final HttpTransportSE transportSE = new HttpTransportSE(URL);

              transportSE.debug = false;

              try {
                     transportSE.call(soapAction, envelope);
                     if (!isResultVector) {
                           result = envelope.getResponse();
                     } else {
                           result = envelope.bodyIn;
                     }
              } catch (final IOException e) {
                     e.printStackTrace();
              } catch (final XmlPullParserException e) {
                     e.printStackTrace();
              } catch (final Exception e) {
                     e.printStackTrace();
              }
              return result;
       }

Android Keyboard Setup AVD


Android Keyboard Setup AVD

In Eclipse

Window - > AVD Manager - > Android Virtual Device Manager ( Select AVD Name) - > Edit - > New - > (ADD) Keyboard Setup ('Yes').


Restart AVD

Thursday, August 16, 2012

How to use Intent in android?

Activity1: IntentEgActivity
IntentEgActivity.java

package intent.eg;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;

public class IntentEgActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button buttonOne = (Button) findViewById(R.id.button1);
        buttonOne.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
              EditText usernam = (EditText)findViewById(R.id.editText1);
              String name = usernam.getText().toString();
              EditText upas = (EditText)findViewById(R.id.editText2);
              String pass = upas.getText().toString();
              if(name.equals("xyz") && pass.equals("abc"))
              {
                     Intent i = new Intent(IntentEgActivity.this,InvokedActivity.class);
                     i.putExtra("value1", name);
                     i.putExtra("value2", pass);
                     startActivity(i);
              }
              else
              {      Toast.makeText(getApplicationContext(), "wrong user name or password", Toast.LENGTH_SHORT).show();

               }
           }
        });
    }
}

Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter User Name" />
   <EditText
          android:id="@+id/editText1"
          android:layout_width="232dp"
          android:layout_height="wrap_content" >
     <requestFocus />
  </EditText>
  <TextView
        android:id="@+id/textView2"
        android:layout_width="152dp"
        android:layout_height="wrap_content"
        android:text="Enter Password" />
  <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword" />
 <Button
              android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Go to Next Activity" />
</LinearLayout>

Activity2: InvokedActivity
InvokedActivity.java

package intent.eg;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class InvokedActivity extends Activity
{      @Override
        public void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               setContentView(R.layout.disp);
               Bundle Name = getIntent().getExtras();
               String userName = Name.getString("value1");
               if(userName != null)
               {
                     TextView txt = (TextView)findViewById(R.id.textView5);
                     txt.setText("Welcome"+" "+userName);
               }
          }
}

disp.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/textView5"
        android:layout_width="167dp"
        android:layout_height="wrap_content"
        android:text="TextView" />
</LinearLayout>





Wednesday, July 18, 2012

How to import webpages in Android?

HtmlImport2Activity.java

package html.import2;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class HtmlImport2Activity extends Activity {
    /** Called when the activity is first created. */
       final Activity activity = this;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
     //   setContentView(R.layout.main);
        getWindow().requestFeature(Window.FEATURE_PROGRESS);
        WebView webview = new WebView(this);
        setContentView(webview);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress) {
        activity.setProgress(progress * 1000);
        }
      });

webview.setWebViewClient(new WebViewClient() {
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        Toast.makeText(activity, "Application has Stoped Working..! " + description, Toast.LENGTH_SHORT).show();
}
});
        webview.loadUrl("http://www.Google.com/");
    }

}

Tuesday, July 17, 2012

Simple JSON Example

After Adding GSON to your project, you can use it as follows.

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
<TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
       />
</LinearLayout>

JSON_structure.java 
package beanmatrix.JSON;
public class JSON_structure {

              public String name;
              public String date;
              public String[] message;
}


JSONexampleActivity.java
package beanmatrix.JSON;
import android.app.Activity;
import com.google.gson.Gson;
import android.os.Bundle;
import android.widget.TextView;


public class JSONexampleActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        try {
              /* Inflate TextView from the layout */
              TextView text = new TextView (this);
              /* JSON data considered as an example. Generally this data is obtained
              from a web service.*/
              String json = " {" + " \"name\": \"BeanMatrix\", " + " \"message\": [\"My JSON Example\",\"Using GSON For Parsing\"]," + " \"date\": \"17-7-2012\" "+ "}";
              Gson gson = new Gson();
              JSON_structure obj= gson.fromJson(json, JSON_structure.class);
              text.setText("Name: "+ obj.name +"\n\n");
              setContentView(text);
              text.append("Date: "+ obj.date +"\n\n");
              for(int i=0;i<obj.message.length;i++)
              {
                     text.append("Message: "+ obj.message[i] +"\n\n");
              }
              }
              catch(Exception ex){ex.printStackTrace();}
              }
}




Monday, July 16, 2012

Downloading and using external Libraries for JSON

There are many libraries available to parse JSON, be carefull while choosing them. GSON is commonly used because they are quicker than the android classes.

1. Download GSON library from
http://code.google.com/p/google-gson/

2. Now add GSON library to your project, follow the given step.

  1. Right click on your project and select properties. Select Java Build Path.Then select Libraries tab.

             2. Now select the option Add external JARs at the right corner.

            3. Browse the path up to downloaded file and press ok.

The External JSON library is ready to use in project.