More Related Content
Similar to Connecting to the network
Similar to Connecting to the network (20)
More from Mu Chun Wang (20)
Connecting to the network
- 2. Add Permissions
<?xml version="1.0" encoding="utf-8"?>
<manifest>
......
<uses-permission
android:name="android.permission.INTERNET" />
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE" />
......
</manifest>
at AndroidManifest.xml
2
- 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. Long IO accessing
Activity.runOnUiThread(Runnable)
View.post(Runnable)
View.postDelayed(Runnable, long)
Handler
AsyncTask
4
- 5. AsyncTask
private 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. downloadUrl
private 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
}
- 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
- 13. HttpClient & HttpURLConnection
org.apache.http.cl java.net.HttpURL
ient.HttpClient Connection
2.1, 2.2 a r(close method issue)
2.3 and
r(difficult to maintain) a
higher
13
- 14. prior 2.2 to use HttpURLConnection
private void disableConnectionReuseIfNecessary() {
if (Integer.parseInt(Build.VERSION.SDK) <
Build.VERSION_CODES.FROYO) {
System.setProperty("http.keepAlive", "false");
}
}
14
- 15. 2.3 and higher, gzip-support
getContentLength() is incorrect
● disable gzip-support
● use getInputStream's length
15
- 16. disable gzip-support
HttpURLConnection conn = (HttpURLConnection)
url.openConnection();
conn.setRequestProperty("Accept-Encoding",
"identity");
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.connect();
16
- 17. use getInputStream's length
private 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. 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