Cosc 4730 Brief return Sockets And HttpClient And AsyncTask.

Slides:



Advertisements
Similar presentations
Programming with Android: Network Operations
Advertisements

USING ANDROID WITH THE INTERNET. Slide 2 Network Prerequisites The following must be included so as to allow the device to connect to the network The.
Threads, AsyncTasks & Handlers.  Android implements Java threads & concurrency classes  Conceptual view  Parallel computations running in a process.
Inter Process Communication:  It is an essential aspect of process management. By allowing processes to communicate with each other: 1.We can synchronize.
Android App to control an IRC Bot. Background What is IRC and what does an IRC Bot do? IRC is Internet Relay Chat. It is a place that you can connect.
Programming with Android: Network Operations Luca Bedogni Marco Di Felice Dipartimento di Scienze dell’Informazione Università di Bologna.
Chien-Chung Shen Manifest and Activity Chien-Chung Shen
1 Android: Event Handler Blocking, Android Inter-Thread, Process Communications 10/11/2012 Y. Richard Yang.
HTTP and Threads. Download some code I’ve created an Android Project which gives examples of everything covered in this lecture. Download code here.here.
Intro to Android Programming George Nychis Srinivasan Seshan.
Networking Nasrullah. Input stream Most clients will use input streams that read data from the file system (FileInputStream), the network (getInputStream()/getInputStream()),
By: Joel Rodriguez.  International student from Mexico  Delicias, Chihuahua Mexico  Spanish  Sports and Music.
UI Design Patterns & Best Practices Mike Wolfson July 22, 2010.
CS378 - Mobile Computing Web - WebView and Web Services.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Concurrency in Android with.
Networking: Part 2 (Accessing the Internet). The UI Thread When an application is launched, the system creates a “main” UI thread responsible for handling.
Cosc 4730 Brief return Sockets And HttpClient (and with AsyncTask) DownloadManager.
1 Announcements Homework #2 due Feb 7 at 1:30pm Submit the entire Eclipse project in Blackboard Please fill out the when2meets when your Project Manager.
1 Mobile Software Development Framework: Android 2/28/2011 Y. Richard Yang.
Networking Android Club Networking AsyncTask HttpUrlConnection JSON Parser.
CS378 - Mobile Computing Web - WebView and Web Services.
HTTP and Threads. Download some code I’ve created an Android Project which gives examples of everything covered in this lecture. Download code here.here.
Asynchronous Tasks with Android Anthony Dahanne, Ing Jr identi.ca/twitter blog : Android Montreal, le 01/09/2010.
HTTP and Threads. Getting Data from the Web Believe it or not, Android apps are able to pull data from the web. Developers can download bitmaps and text.
Writing Zippy Android Apps Brad Fitzpatrick May 20th, 2010 Live Wave:
Working in the Background Radan Ganchev Astea Solutions.
Introduction to Socket Programming in Android Jules White Bradley Dept. of Electrical and Computer Engineering Virginia Tech
MAKANI ANDROID APPLICATION Prepared by: Asma’ Hamayel Alaa Shaheen.
CS378 - Mobile Computing Responsiveness. An App Idea From Nifty Assignments Draw a picture use randomness Pick an equation at random Operators in the.
Android networking 1. Network programming with Android If your Android is connected to a WIFI, you can connect to servers using the usual Java API, like.
ALAA M. ALSALEHI SOFTWARE ENGINEER AT IUG Multithreading in Android.
Android Threads. Threads Android will show an “ANR” error if a View does not return from handling an event within 5 seconds Or, if some code running in.
1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang.
HW#9 Clues CSCI 571 Fall, HW#9 Prototype
네트워크 전송 1/2 UNIT 29 로봇 SW 콘텐츠 교육원 조용수. 학습 목표 Android Network 통신 2.
Recap of Part 1 Terminology Windows FormsAndroidMVP FormActivityView? ControlViewView? ?ServiceModel? Activities Views / ViewGroups Intents Services.
Address Book App 1 Fall 2014 CS7020: Game Design and Development.
David Sutton MOBILE INTERNET. OUTLINE  This week’s app – an RSS feed reader  Mobile internet considerations  Threads (recap)  RSS feeds  Using AsyncTask.
Lecture 6: Process and Threads Topics: Process, Threads, Worker Thread, Async Task Date: Mar 1, 2016.
CHAPTER 6 Threads, Handlers, and Programmatic Movement.
HUJI Post PC Workshop 5 Files, Threading, and Web Services Sasha Goldshtein
USING ANDROID WITH THE INTERNET. Slide 2 Lecture Summary Getting network permissions Working with the HTTP protocol Sending HTTP requests Getting results.
Multithreading Chapter 6. Objectives Understand the benefits of multithreading on Android Understand multi-threading fundamentals Know the Thread class.
H W #9 Clues CSCI 571 Spring, H W# 9 P rototype YouTube Link :
Small talk with the UI thread
UI Design Patterns & Best Practices
Asynchronous Task (AsyncTask) in Android
Reactive Android Development
Concurrency in Android
Cosc 5/4730 REST services.
WebView and Web Services
CS240: Advanced Programming Concepts
CSE 486/586 Distributed Systems Android Programming --- 2
Notifications and Services
Lecture 6: Process, Thread, Task
Android Introduction Camera.
Mobile Software Development Framework: Android
CIS 470 Mobile App Development
Many thanks to Jun Bum Lim for his help with this tutorial.
CS371m - Mobile Computing Responsiveness.
CS323 Android Topics Network Basics for an Android App
Mobile Computing With Android ACST 4550 Android Database Storage
Android Topics Asynchronous Callsbacks
Android Topics Threads and the MessageQueue
Android Developer Fundamentals V2
Threads, Handlers, and AsyncTasks
CIS 470 Mobile App Development
Using threads for long running tasks.
Lecture 6: Process, Thread, Task
Android Threads Dimitar Ivanov Mobile Applications with Android
Presentation transcript:

cosc 4730 Brief return Sockets And HttpClient And AsyncTask

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.

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.

AndroidManifest.xml <manifest xmlns:android=" package="com.cosc4755.TCPclient" android:versionCode="1" android:versionName="1.0"> <activity android:name=".TCPclient"

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

References Android dev site of course – CN/reference/android/net/package- summary.html Socket programming tutorial. t325-s30.html

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

HttpClient This is a modified version of Apache’s HttpClient – – Used a lot with the J2EE space

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.

Example get HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet(); request.setURI(new URI(" HttpResponse response = client.execute(request); To add parameters to a get HttpGet method = new HttpGet( " HttpResponse response = client.execute(method);

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

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.

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

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

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

Q A &