Connecting to the network

1,592 views

Published on

Published in: Technology, Design
1 Comment
1 Like
Statistics
Notes
  • can you tell me how to use HTTPS as a url and parse an xml file using SAX Parser
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
1,592
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
17
Comments
1
Likes
1
Embeds 0
No embeds

No notes for slide

Connecting to the network

  1. 1. Connecting to the NetworkKewang
  2. 2. 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
  3. 3. 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
  4. 4. Long IO accessing Activity.runOnUiThread(Runnable) View.post(Runnable) View.postDelayed(Runnable, long) Handler AsyncTask 4
  5. 5. 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
  6. 6. 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}
  7. 7. 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
  8. 8. streamToImageprivate Bitmap streamToImage(InputStream stream) { return BitmapFactory.decodeStream(stream);} GET http://example.com/mitake.png 8
  9. 9. 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
  10. 10. HttpClient & HttpURLConnection ● HttpClient ● stable ● few bugs ● large size ● HttpURLConnection ● easy to use ● gzip-support 10
  11. 11. no gzip-support 11
  12. 12. gzip-supported 12
  13. 13. 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
  14. 14. prior 2.2 to use HttpURLConnectionprivate void disableConnectionReuseIfNecessary() { if (Integer.parseInt(Build.VERSION.SDK) < Build.VERSION_CODES.FROYO) { System.setProperty("http.keepAlive", "false"); }} 14
  15. 15. 2.3 and higher, gzip-support getContentLength() is incorrect ● disable gzip-support ● use getInputStreams length 15
  16. 16. disable gzip-supportHttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setRequestProperty("Accept-Encoding","identity");conn.setReadTimeout(10000);conn.setConnectTimeout(15000);conn.connect(); 16
  17. 17. 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
  18. 18. 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. 19. 19

×