Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cosc 4730 Brief return Sockets And HttpClient And AsyncTask.

Similar presentations


Presentation on theme: "Cosc 4730 Brief return Sockets And HttpClient And AsyncTask."— Presentation transcript:

1 cosc 4730 Brief return Sockets And HttpClient And AsyncTask

2 Networking Android networking is built on standard Java SE – So use the same network code you learned earlier. – See the source code example, Android TCPclient and TCPserv for examples that run on the Android platform.

3 Permissions! Android app require a permission line in the AndroidManifest.xml – Otherwise it fails to work. – The line must be in specific place as well. Putting in the work spot, causes it to be ignored.

4 AndroidManifest.xml <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.cosc4755.TCPclient" android:versionCode="1" android:versionName="1.0"> <activity android:name=".TCPclient" android:label="@string/app_name">

5 simulator For server code, you need to tell the simulator to accept the port number In android-sdk-windows\tools directory, run the following dos command – adb forward tcp:3012 tcp:3012 assuming you are using port 3012

6 References Android dev site of course – http://developer.android.com/intl/zh- CN/reference/android/net/package- summary.html Socket programming tutorial. http://www.anddev.org/socket_programming- t325-s30.html

7 Main thread and network. Networking can take some time and should not be done on the main thread – Ie it can lock up the drawing. As of v11 (honeycomb) – It will force close if you attempt networking on the main thread. It must be done in a thread – Or a AsyncTask

8 HttpClient This is a modified version of Apache’s HttpClient – http://hc.apache.org/httpclient-3.x/ http://hc.apache.org/httpclient-3.x/ – Used a lot with the J2EE space

9 Use Create an HttpClient Instantiate a new HTTP method – PostMethod or GetMethod Set HTTP parameter names/values Execute the HTTP call using the HttpClient Process the HTTP response.

10 Example get HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet(); request.setURI(new URI("http://www.uwyo.edu/")); HttpResponse response = client.execute(request); To add parameters to a get HttpGet method = new HttpGet( "http://www.com/do.php?key=valueGoesHere"); HttpResponse response = client.execute(method);

11 Example Post HttpClient client = new DefaultHttpClient(); HttpPost request = new HttpPost("http://somewebsite/do.php"); List postParameters = new ArrayList (); postParameters.add(new BasicNameValuePair("one", "valueGoesHere")); UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters); request.setEntity(formEntity); HttpResponse response = client.execute(request);

12 AsyncTask This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers. – AsyncTasks should ideally be used for short operations (a few seconds at the most.) otherwise you should use threads and handlers.

13 AsyncTask (2) An asynchronous task is defined by 3 generic types, called Params, Progress and Result, and 4 steps, called onPreExecute, doInBackground, onProgressUpdate and onPostExecute. – doInBackground runs in the background. – onProgressUpdate and onPostExecute are executed on the main/UI thread – Meaning they can also update the widgets. The return value from doInBackground is called as parameter to onPostExecute publishProgress (called in doInBackground) invokes the onProgressUpdate

14 AsyncTask Example private class DownloadFilesTask extends AsyncTask { protected Long doInBackground(URL... urls) { int count = urls.length; long totalSize = 0; for (int i = 0; i < count; i++) { totalSize += Downloader.downloadFile(urls[i]); publishProgress((int) ((i / (float) count) * 100)); // Escape early if cancel() is called if (isCancelled()) break; } return totalSize; } //background thread protected void onProgressUpdate(Integer... progress) { setProgressPercent(progress[0]); } //UI thread protected void onPostExecute(Long result) { showDialog("Downloaded " + result + " bytes"); } //UI thread } Once created, a task is executed very simply: new DownloadFilesTask().execute(url1, url2, url3); URL is pamaters to doInBackground Integer is the value for publishProgress and onProgressUpdate And Long is the return value and parameter to onPostExecute The call, uses URL to create the “list” used in doInBackground

15 Rest of the examples See the hand page pages for the rest of the source code for the examples. HttpClientDemo.zip uses threads HttpClientDemo2.zip uses AsyncTask

16 Q A &


Download ppt "Cosc 4730 Brief return Sockets And HttpClient And AsyncTask."

Similar presentations


Ads by Google