Networking.  Many applications provide & use data & services via the Internet  Android includes multiple networking support classes, e.g.,  java.net.

Slides:



Advertisements
Similar presentations
Programming with Android: Network Operations
Advertisements

J0 1 Marco Ronchetti - Web architectures – Laurea Specialistica in Informatica – Università di Trento Java XML parsing.
Technische universität dortmund Service Computing Service Computing Prof. Dr. Ramin Yahyapour IT & Medien Centrum 22. Oktober 2009.
SOCKET PROGRAMMING WITH MOBILE SOCKET CLIENT DEARTMENT OF COMPUTER SCIENCE IOWA STATE UNIVERSITY.
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.
XML Parsers By Chongbing Liu. XML Parsers  What is a XML parser?  DOM and SAX parser API  Xerces-J parsers overview  Work with XML parsers (example)
1 SAX and more… CS , Spring 2008/9. 2 SAX Parser SAX = Simple API for XML XML is read sequentially When a parsing event happens, the parser invokes.
1 The Simple API for XML (SAX) Part I ©Copyright These slides are based on material from the upcoming book, “XML and Bioinformatics” (Springer-
Xerces The Apache XML Project Yvonne Yao. Introduction Set of libraries that provides functionalities to parse XML documents Set of libraries that provides.
21-Jun-15 SAX (Abbreviated). 2 XML Parsers SAX and DOM are standards for XML parsers-- program APIs to read and interpret XML files DOM is a W3C standard.
Client/Server example. Server import java.io.*; import java.net.*; import java.util.*; public class PrimeServer { private ServerSocket sSoc; public static.
Recommendation: Play the game and attempt to answer the questions yourself without looking at the answers. You’ll learn much less if you just look at the.
26-Jun-15 SAX. SAX and DOM SAX and DOM are standards for XML parsers--program APIs to read and interpret XML files DOM is a W3C standard SAX is an ad-hoc.
Generic Connection Framework Connection FileConnectionSocketConnectionHTTPConnection InputConnection OutputConnection StreamConnection.
Programming with Android: Network Operations Luca Bedogni Marco Di Felice Dipartimento di Scienze dell’Informazione Università di Bologna.
17 Apr 2002 XML Programming: SAX Andy Clark. SAX Design Premise Generic method of creating XML parser, parsing documents, and receiving document information.
CSE 341, S. Tanimoto Java networking- 1 Java Networking Motivation Network Layers Using Sockets A Tiny Server Applets URLs Downloading Images, MediaTracker.
Web Proxy Server. Proxy Server Introduction Returns status and error messages. Handles http CGI requests. –For more information about CGI please refer.
CSE 6331 © Leonidas Fegaras XML Tools1 XML Tools Leonidas Fegaras.
Io package as Java’s basic I/O system continue’d.
17 Apr 2002 XML Programming: JAXP Andy Clark. Java API for XML Processing Standard Java API for loading, creating, accessing, and transforming XML documents.
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.
Networking Nasrullah. Input stream Most clients will use input streams that read data from the file system (FileInputStream), the network (getInputStream()/getInputStream()),
CS378 - Mobile Computing Web - WebView and Web Services.
SDPL 2003Notes 3: XML Processor Interfaces1 3. XML Processor APIs n How can applications manipulate structured documents? –An overview of document parser.
Mobile Programming Lecture 14 Communicating via the Internet.
CSE 6331 © Leonidas Fegaras XML Tools1 XML Tools.
SDPL 2002Notes 3: XML Processor Interfaces1 3. XML Processor APIs n How can applications manipulate structured documents? –An overview of document parser.
SDPL 20113: XML APIs and SAX1 3. XML Processor APIs n How can (Java) applications manipulate structured (XML) documents? –An overview of XML processor.
Networking Android Club Networking AsyncTask HttpUrlConnection JSON Parser.
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.
SAX. What is SAX SAX 1.0 was released on May 11, SAX is a common, event-based API for parsing XML documents Primarily a Java API but there implementations.
Beginning XML 4th Edition. Chapter 12: Simple API for XML (SAX)
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.
SDPL Streaming API for XML1 3.4 Streaming API for XML (StAX) n Could we process XML documents more conveniently than with SAX, and yet more efficiently?
Serialization. Serialization is the process of converting an object into an intermediate format that can be stored (e.g. in a file or transmitted across.
Lecture 9 Network programming. Manipulating URLs URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on the Internet.
Web Design & Development 1 Lec - 21 Umair Javed. Web Design & Development 2 Socket Programming.
Introduction to Socket Programming in Android Jules White Bradley Dept. of Electrical and Computer Engineering Virginia Tech
Networks Sockets and Streams. TCP/IP in action server ports …65535 lower port numbers ( ) are reserved port echo7 time13 ftp20 telnet23.
1 XML and Web App 1. Objectives 2  Building Database-Driven Applications  Building Data Exchange Applications  Workshops.
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.
UNIT 30 네트워크 전송 2 로봇 SW 콘텐츠 교육원 조용수.
CSE 6331 © Leonidas Fegaras XML Tools1 XML Tools.
Server - Client Communication Getting data from server.
© Marty Hall, Larry Brown Web core programming 1 Simple API for XML SAX.
XML and SAX (A quick overview) ● What is XML? ● What are SAX and DOM? ● Using SAX.
네트워크 전송 1/2 UNIT 29 로봇 SW 콘텐츠 교육원 조용수. 학습 목표 Android Network 통신 2.
SDPL 20063: XML Processor Interfaces1 3. XML Processor APIs n How can (Java) applications manipulate structured (XML) documents? –An overview of XML processor.
1 Lecture 9: Network programming. 2 Manipulating URLs URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on.
Simple API for XML (SAX) Aug’10 – Dec ’10. Introduction to SAX Simple API for XML or SAX was developed as a standardized way to parse an XML document.
Java Server Sockets ServerSocket : Object to listen for client connection requests Throws IOException accept() method to take the client connection. Returns.
7-Mar-16 Simple API XML.  SAX and DOM are standards for XML parsers-- program APIs to read and interpret XML files  DOM is a W3C standard  SAX is an.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Java API for distributed computing.
SDPL 2001Notes 3: XML Processor Interfaces1 3. XML Processor APIs n How applications can manipulate structured documents? –An overview of document parser.
David Sutton MOBILE INTERNET. OUTLINE  This week’s app – an RSS feed reader  Mobile internet considerations  Threads (recap)  RSS feeds  Using AsyncTask.
XML & JSON. Background XML and JSON are to standard, textual data formats for representing arbitrary data – XML stands for “eXtensible Markup Language”
1 Introduction SAX. Objectives 2  Simple API for XML  Parsing an XML Document  Parsing Contents  Parsing Attributes  Processing Instructions  Skipped.
Java API for XML Processing
Simple API for XML SAX. Agenda l Introduction to SAX l Installation and setup l Steps for SAX parsing l Defining a content handler l Examples Printing.
USING ANDROID WITH THE INTERNET. Slide 2 Lecture Summary Getting network permissions Working with the HTTP protocol Sending HTTP requests Getting results.
Messaging and Networking. Objectives Send SMS messages using the Messaging app Send SMS messages programmatically from within your app Receive and consume.
1 Framework Presentation Project Participants: Karun Biyani Manish Mehta Pradeep Vincent CSE870 Advanced Software Engineering, Spring 2001 Instructor:
CS499 – Mobile Application Development Fall 2013 Networking & HTTP.
CSE 341, S. Tanimoto Java networking-
null, true, and false are also reserved.
Java API for XML Processing
Android Developer Fundamentals V2
Streams and Readers The stream hierarchy is for reading bytes, the reader/writer hierarchy is for reading characters Unicode characters can be used internationally,
SAX2 29-Jul-19.
Presentation transcript:

Networking

 Many applications provide & use data & services via the Internet  Android includes multiple networking support classes, e.g.,  java.net – (Socket, URL)  org.apache - (HttpRequest, HttpResponse)  android.net – (URI, AndroidHttpClient,AudioStream)

 Typically need permission to access the Internet 

public class NetworkingSocketsActivity extends Activity { TextView mTextView = null; public void onCreate(Bundle savedInstanceState) { … // assuming webserver on , listening on port 80 new HttpGet().execute(" "); } private void onFinishGetRequest(String result) { mTextView.setText(result); }

private class HttpGet extends AsyncTask { protected String doInBackground(String... params) { Socket socket = null; StringBuffer data = new StringBuffer(); try { socket = new Socket(params[ 0 ], 80 ); PrintWriter pw = new PrintWriter( new OutputStreamWriter(socket.getOutputStream()),true); pw.println("GET /json_example.txt"); …

BufferedReader br = new BufferedReader( new InputStreamReader( socket.getInputStream())); String rawData; while ((rawData = br.readLine()) != null) { data.append(rawData); } } catch … // close socket … return data.toString(); } protected void onPostExecute(String result) { onFinishGetRequest(result); } …

public class NetworkingURLActivity extends Activity { TextView mTextView = null; public void onCreate(Bundle savedInstanceState) { new HttpGetTask().execute( " } …

private class HttpGetTask extends AsyncTask { protected String doInBackground(String... params) { StringBuffer data = new StringBuffer(); BufferedReader br = null; try { HttpURLConnection conn = (HttpURLConnection) new URL(params[ 0 ]).openConnection(); // read & process response } // close outputStream return data.toString(); } …

private class HttpGetTask extends AsyncTask { protected String doInBackground(String... params) { AndroidHttpClient client = AndroidHttpClient.newInstance(""); HttpGet request = new HttpGet(params[ 0 ]); ResponseHandler responseHandler = new BasicResponseHandler(); try { return client.execute(request, responseHandler); } catch … return null; } …

 Several popular formats including  JSON  XML

 Javascript Object Notation   Intended to be a lightweight data interchange format  Data packaged in two types of structures:  Maps of key/value pairs  Ordered lists of values

 &south=- 9.9&east=-22.4&west=55.2&username=demo  Produces {"earthquakes”: [ {"eqid":"c0001xgp","magnitude":8.8,"lng": ,"src":"us", "datetime":" :46:23","depth":24.4,"lat":38.322}, {"eqid":"2007hear","magnitude":8.4,"lng": ,"src":"us", "datetime":" :10:26","depth":30,"lat": }, … {"eqid":"2010xkbv","magnitude":7.5,"lng": ,"src":"us", "datetime":" :26:50","depth":35,"lat":7.7477} ] }

class HttpGetTask extends AsyncTask > { protected List doInBackground(String... params) { AndroidHttpClient client = AndroidHttpClient.newInstance(""); HttpGet request = new HttpGet(params[ 0 ]); JSONResponseHandler responseHandler = new JSONResponseHandler(); try { return client.execute(request, responseHandler); } catch … return null; } …

class JSONResponseHandler implements ResponseHandler > { public List handleResponse(HttpResponse response) throws ClientProtocolException, IOException { List result = new ArrayList (); String JSONResponse = new BasicResponseHandler().handleResponse(response); try { JSONObject object = (JSONObject) new JSONTokener(JSONResponse).nextValue(); JSONArray earthquakes = object.getJSONArray("earthquakes"); for (int i = 0; i < earthquakes.length(); i++) { JSONObject tmp = (JSONObject) earthquakes.get(i); result.add("mag:" + tmp.get("magnitude") + " lat:” + tmp.getString("lat") + " lng:" + tmp.get("lng")); } } catch (JSONException e) { … } return result; …

 eXtensible Markup Language  See  XML documents  made up of storage units called entities  Entities can contain markup, which encodes a description of the document's storage layout and logical structure

 Several types of parsers available  SAX – streaming with application callbacks  Pull – Application iterates over XML entries  DOM – Coverts document into a tree of nodes

 south=- 9.9&east=-22.4&west=55.2& username=demo  Produces us c0001xgp :46: …

… private class XMLResponseHandler implements ResponseHandler > { public List handleResponse(HttpResponse response) throws ClientProtocolException, IOException { try { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); XMLContentHandler handler = new XMLContentHandler(); xr.setContentHandler(handler); xr.parse(new InputSource(response.getEntity().getContent())); return handler.getData(); } catch … return null; } }…

private class XMLContentHandler extends DefaultHandler { String lat = null, lng = null, mag=null; boolean parsingLat = false, parsingLng = false, parsingMag=false; List results = new ArrayList (); public void startElement(String uri, String localName, String qName,Attributes attributes) throws SAXException { if (localName.equals("lat")) { parsingLat = true; } else if (localName.equals("lng")) { parsingLng = true; } else if (localName.equals("magnitude")) { parsingMag = true; }

public void characters(char[] ch, int start, int length) throws SAXException { if (parsingLat) { lat = new String(ch, start, length).trim(); } else if … } public void endElement(String uri, String localName, String qName) { if (localName.equals("lat")) { parsingLat = false; } else if … } else if (localName.equals("earthquake")) { results.add("lat:" + lat + " lng: " + lng + " mag:" + mag); lat = null; lng = null; mag = null; } public List getData() { return results; } …