• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Connecting to the network
 

Connecting to the network

on

  • 1,477 views

 

Statistics

Views

Total Views
1,477
Views on SlideShare
1,477
Embed Views
0

Actions

Likes
1
Downloads
13
Comments
1

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

11 of 1 previous next

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • can you tell me how to use HTTPS as a url and parse an xml file using SAX Parser
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Connecting to the network Connecting to the network Presentation Transcript

    • Connecting to the NetworkKewang
    • Add Permissions<?xml version="1.0" encoding="utf-8"?><manifest> ...... <uses-permissionandroid:name="android.permission.INTERNET" /> <uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE" /> ......</manifest> at AndroidManifest.xml 2
    • isConnected?private boolean isConnected() { ConnectivityManager connMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo =connMgr.getActiveNetworkInfo(); if (networkInfo != null && networkInfo.isConnected()) { return true; } else { return false; }} 3
    • Long IO accessing Activity.runOnUiThread(Runnable) View.post(Runnable) View.postDelayed(Runnable, long) Handler AsyncTask 4
    • AsyncTaskprivate class ProcessingTask extends AsyncTask<String,Void, String> { @Override protected String doInBackground(String... params) { try { return downloadUrl(params[0]); } catch (IOException e) { return "無法讀取網頁"; worker thread } } @Override protected void onPostExecute(String result) { txtContent.setText(result); } main thread} 5
    • downloadUrlprivate String downloadUrl(String myurl) { int len = 500; URL url = new URL(myurl); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setReadTimeout(10000); conn.setConnectTimeout(15000); conn.connect(); int statusCode = conn.getResponseCode(); return streamToString(conn.getInputStream(), len); 6}
    • streamToStringprivate String streamToString(InputStream stream, intlen) { Reader reader = new InputStreamReader(stream, "UTF-8"); char[] buffer = new char[len]; reader.read(buffer); return new String(buffer);} 7
    • streamToImageprivate Bitmap streamToImage(InputStream stream) { return BitmapFactory.decodeStream(stream);} GET http://example.com/mitake.png 8
    • Where is My IP?private String getIP() { for (Enumeration<NetworkInterface> en = NetworkInterface .getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inet = enumIpAddr.nextElement(); if (!inet.isLoopbackAddress() && inet instanceof Inet4Address) { return inet.getHostAddress().toString(); } } } return "";} 9
    • HttpClient & HttpURLConnection ● HttpClient ● stable ● few bugs ● large size ● HttpURLConnection ● easy to use ● gzip-support 10
    • no gzip-support 11
    • gzip-supported 12
    • HttpClient & HttpURLConnection org.apache.http.cl java.net.HttpURL ient.HttpClient Connection2.1, 2.2 a r(close method issue)2.3 and r(difficult to maintain) ahigher 13
    • prior 2.2 to use HttpURLConnectionprivate void disableConnectionReuseIfNecessary() { if (Integer.parseInt(Build.VERSION.SDK) < Build.VERSION_CODES.FROYO) { System.setProperty("http.keepAlive", "false"); }} 14
    • 2.3 and higher, gzip-support getContentLength() is incorrect ● disable gzip-support ● use getInputStreams length 15
    • disable gzip-supportHttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setRequestProperty("Accept-Encoding","identity");conn.setReadTimeout(10000);conn.setConnectTimeout(15000);conn.connect(); 16
    • use getInputStreams lengthprivate String streamToString(InputStream stream) { Reader reader = new InputStreamReader(stream, "UTF-8"); StringBuffer sb = new StringBuffer(); int length = 0; char c; while ((c = (char) reader.read()) != 65535) { length++; sb.append(c); } return sb.toString();} 17
    • References● http://developer.android.com/training/basics/network-ops/connecting.html● http://developer.android.com/reference/java/net/HttpURLConnection.html● http://developer.android.com/reference/java/net/URLConnection.html● http://developer.android.com/reference/java/net/package-summary.html● http://developer.android.com/reference/android/net/ConnectivityManager.html● http://developer.android.com/reference/android/net/NetworkInfo.html● http://android-developers.blogspot.tw/2011/09/androids-http-clients.html● http://android-developers.blogspot.tw/2009/05/painless-threading.html● http://www.droidnova.com/get-the-ip-address-of-your-device,304.html● https://developers.google.com/speed/articles/gzip 18
    • 19