|
標準APIによるネットワークの制御
- Android搭載のJavaライブラリは、CLDC(Connected Limited Device Configuration)やMIDP(Mobile Information Device Profile)などのサブセットではない
- java.netパッケージにあるJavaAPIをそのまま使用可能
- android.netパッケージにあるLocalSocketクラスは、UNIXドメインを使用したローカル用のソケット通信
http接続の概要
- AndroidでHTTP通信を行う場合、
- java.netにあるHttpURLConnectionクラス
- org.apache.httpパッケージ
を使用する
- 使用するAPI
- URLが参照するリモートオブジェクトへの接続を示すURLConnectionオブジェクトを返す
Class | java.net.URL |
---|
Method | URLConnection openConnection() throws IOException |
---|
- 接続からの入力を受け取る入力ストリームを返す
Class | java.net.HttpURLConnection |
---|
Method | InputStream getInputStream() throws IOException |
---|
- 接続に書き込みを行う出力ストリームを返す
Class | java.net.HttpURLConnection |
---|
Method | OutputStream getOutputStream() throws IOException |
---|
- 引数で渡されたHTTP要求を実行する
Class | org.apache.http.impl.client.DefaultHttpClient |
---|
Method | HttpResponse execute(HttpUriRequest request) throws IOException, ClientProtocolException |
---|
- http接続を使ったデータのダウンロード
- サーバーへHTTP接続し、GETメソッドでデータをダウンロードして配列に格納するコードを例示する。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
-
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
|
|
-
|
|
!
-
-
-
-
|
!
!
-
-
|
!
!
!
|
|
-
|
|
!
-
-
!
| import java.io.*;
import java.net.*;
String APIURL = "http://sample.net/...";
byte[] RESULT = null;
try {
URL url = new URL(APIURL);
HttpURLConnection http = (HttpURLConnection)url.openConnection();
http.setRequestMethod("GET");
http.connect();
InputStream in = null;
ByteArrayOutputSteam out = null;
long recved = 0;
try {
in = http.getInputStream();
out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
recved += len;
}
} catch (IOException ie) {
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ie) { }
}
if (out != null) {
try {
out.close();
} catch (IOException ie) { }
}
}
http.disconnect();
if (recved > 0) {
RESULT = out.toByteArray();
}
} catch (MalformedURLException e) {
} catch (IOException e) {
}
|
通信回線の制御
- 携帯電話等で実行されるプログラムを書く際には、ネットワークの状態や回線品質を考慮する必要がある。
- 使用するAPI
- アクティブなネットワークの状態を取得
Class | android.net.ConnectivityManager |
---|
Method | NetworkInfo getActiveNetworkInfo() |
---|
- ネットワークのステータス取得
Class | android.net.NetworkInfo |
---|
Method | NetworkInfo.State getState() |
---|
- ネットワークの種類を取得
Class | android.net.NetworkInfo |
---|
Method | int getType() |
---|
- ローミングサービスによるネットワーク接続か否かを取得
Class | android.telephony.TelephonyManager |
---|
Method | boolean isNetworkRoaming() |
---|
- ネットワーク接続の種類を取得
Class | android.telephony.TelephonyManager |
---|
Method | int getNetworkType() |
---|
- ネットワークへの接続状態を取得
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
-
|
-
|
-
|
-
|
-
|
-
|
-
|
!
| import android.net.ConnectivityManager;
import android.net.NetworkInfo;
ConnectivityManager cm = (ConnectivityManager)getSystemService(
getContext().CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
NetworkInfo.State state = info.getState();
if (state == NetworkInfo.State.UNKNOWN) {
} else if (state == NetworkInfo.State.CONNECTED) {
} else if (state == NetworkInfo.State.CONNECTING) {
} else if (state == NetworkInfo.State.DISCONNECTING) {
} else if (state == NetworkInfo.State.DISCONNECTED) {
} else if (state == NetworkInfo.State.SUSPENDED) {
} else {
}
|
- 回線の品質を取得
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
-
|
-
|
|
|
|
-
|
!
|
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
!
!
| import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
ConnectivityManager cm = (ConnectivityManager)getSystemService(
getContext().CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
if (info.getType() == ConnectivityManager.TYPE_WIFI) {
} else if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
if (tm.isNetworkRoaming()) {
}
switch (tm.getNetworkType()) {
default:
case TelephonyManager.NETWORK_TYPE_UNKNOWN:
break;
case TelephonyManager.NETWORK_TYPE_GPRS:
break;
case TelephonyManager.NETWORK_TYPE_EDGE:
case TelephonyManager.NETWORK_TYPE_UMTS:
case TelephonyManager.NETWORK_TYPE_HSDPA:
case TelephonyManager.NETWORK_TYPE_CDMA:
case TelephonyManager.NETWORK_TYPE_EVDO_0:
case TelephonyManager.NETWORK_TYPE_EVDO_A:
case TelephonyManager.NETWORK_TYPE_1xRTT:
break;
case TelephonyManager.NETWORK_TYPE_HSUPA:
case TelephonyManager.NETWORK_TYPE_HSPA:
break;
}
}
|
ネットワーク接続に関するPermission
- ネットワークAPIを使用するには、以下の2つのPermissionが必要。
- android.permission.INTERNET
- android.permission.ACCESS_NETWORK_STATE
- ネットワーク接続の状態を取得するために必要なPermissionは、以下の2つが必要。
- android.permission.READ_PHONE_STATE
- android.permission.ACCESS_NETWORK_STATE
- 以上のPermissionをAndroidManifest.xmlに追加する必要がある。
|
|