Download Application Development for Mobile Devices. Exam

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Learning Unit:
Exam:
Project:
IPN-ESCOM
Application Development for Mobile Devices.
Extraordinary.
A Web service, invoking the SOAP protocol, in an Android application.
The delivery of this project is essential therefore a requirement for submission of the special
examination of this learning unit. Please, read text very carefully before start to answer it.
SECTION I.
1. Develop the following steps to construct an Android application to use a Web service
within Android.
2. Create an Android application HelloWorld for using the Web service.
3. Write the code for the main activity file MainActivity.java and include the
startService() and stopService() methods.
4. Create the java file MyService.java containing an implementation of Android service
related methods.
5. In the AndroidManifest.xml file use <service.../> tag and define the service or
services.
6. Insert a LinearLayout and two Buttons in the main.xml.
7. Insert two string tags start_service and stop_service in the strings.xml file.
8. Execute and verify the results in the application and note any changes.
Suggested steps:
a. Include two startService() and stopService() methods to start and stop the
service.
public class MainActivity extends Activity {
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.activity_main);
}
public void startService(View view) {
startService(new Intent(getBaseContext(), MyService.class));
}
public void stopService(View view) {
stopService(new Intent(getBaseContext(), MyService.class));
}
}
b. Next, the MyService.java file implementing the onStartCommand() and onDestroy()
methods associated with Service.
public class MyService extends Service {
public IBinder onBind(Intent arg0) {
return null;
}
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
return START_STICKY;
}
public void onDestroy() {
super.onDestroy();
Toast.makeText(this,"Service Destroyed",Toast.LENGTH_LONG).show();
}
}
Página 1
c. Modify the Manifest.xml file by adding a <service.../> tag.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
:
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<service android:name=".MyService" />
</application>
</manifest>
d. Next, include two buttons in the main.xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
:
<Button android:id="@+id/bStart"
:
android:text="@string/startService"
android:onClick="startService" />
<Button android:id="@+id/bStop"
:
android:text="@string/stopService"
android:onClick="stopService" />
</LinearLayout>
e. Define constants in the strings.xml:
<resources>
:
<string name="start_service">Start Service</string>
<string name="stop_service">Stop Service</string>
:
</resources>
SECTION II.
Construct a simple application to show how a web service is invoked using SOAP protocol in an
Android application. The application will convert Celsius to Fahrenheit degrees. Use SOAP
protocol to send and retrieve information in order to invoke web methods. The application will
require an Internet connection to hit a Web Service hosted in a remote server and to check this is
done before trying to hit Web Service. When a web method is invoked the application gets back the
data within a XML file from the server. The response being received can be parsed and rendered in
the current application if needed.
1. The layout configuration.
Use an Android project with project name WebserviceActivity and insert the package name.
Página 2
2. The XML layout.
<?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="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Celsius to Farenheit" />
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numeric="integer" />
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Convert to Farenheit" />
<TextView
android:id="@+id/tv_result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="24dp" />
</LinearLayout>
3. Download the KSOAP library and add to the lib folder of your Android project.
4. Create the needed objects for the WebserviceActivity class. An example is:
final String NAMESPACE = "http://www.w3schools.com/webservices/";
final String URL="http://www.w3schools.com/webservices/tempconvert.asmx";
final String SOAP_ACTION =
"http://www.w3schools.com/webservices/CelsiusToFahrenheit";
final String METHOD_NAME = "CelsiusToFahrenheit";
String TAG = "PGGURU";
static String celsius, fahren;
Button b;
TextView tv;
EditText et;
onCreate()
5. Add enough code in the onCreate() method, as indicated.
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.main);
et = (EditText) findViewById(R.id.editText1);
tv = (TextView) findViewById(R.id.tv_result);
b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(et.getText().length()!=0 && et.getText().toString()!=""){
celsius = et.getText().toString();
AsyncCallWS task = new AsyncCallWS();
Página 3
task.execute();
} else { tv.setText("Enter Celsius degrees:"); }
}
});
}
6. The AsyncCallWS class.
This class will execute the web service and its separate thread that calls the following methods:
onPreExecute(), doInBackground(), onProgressUpdate(), and onPostExecute().
private class AsyncCallWS extends AsyncTask<String, Void, Void> {
protected Void doInBackground(String params) {
Log.i(TAG, "doInBackground");
getFahrenheit(celsius);
return null;
}
protected void onPostExecute(Void result) {
Log.i(TAG, "onPostExecute");
tv.setText(fahren + "° F");
}
protected void onPreExecute() {
Log.i(TAG, "onPreExecute");
tv.setText("Calculating...");
}
protected void onProgressUpdate(Void values) {
Log.i(TAG, "onProgressUpdate");
}
}
7. Create the method getFahrenheit() inside the WebserviceActivity class to invoke
the web service.
public void getFahrenheit(String celsius) {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo celsiusPI = new PropertyInfo();
celsiusPI.setName("Celsius");
celsiusPI.setValue(celsius);
celsiusPI.setType(double.class);
request.addProperty(celsiusPI);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
fahren = response.toString();
} catch (Exception e) {
e.printStackTrace();
}
}
8. The Internet permission into Manifest.xml.
<uses-permission android:name="android.permission.INTERNET" />
Página 4
9. Executing the service application should be similar to the following image:
Annotations.
a. Report a detailed text and images for the whole application.
b. Include in a Word file: Personal data, Introduction, Development, Conclusions and
Bibliography.
c. Zip the project files into a folder and your name as indicated: NameProject.zip.
Página 5