2010.10.31———Android 04


2010.10.31———Android 04
内容1
*********************************************
ネットワークからのデータの取得
*********************************************
1、web権限
<!--   internet   -->
<uses-permission android:name="android.permission.INTERNET"/>

2、指定されたパスの下の内容またはファイルのバイナリデータを取得する
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//       5 
conn.setConnectTimeout(5000);
// get      
conn.setRequestMethod("GET");
//
if(conn.getResponseCode() == 200){
	//          
	InputStream is = conn.getInputStream();
	ByteArrayOutputStream bos = new ByteArayOutputStream();
	byte[] buffer = new byte[1024];
	int len = 0;
	while((len = is.read(buffer)) !=-1);{
		bos.write(buffer,0,len);
	}
	return bos.toByteArray();
	
}

3、バイナリデータから指定内容またはファイルに変換
A、ネットから画像を取得する
if(data!=null){
	//          ,    
	Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
	//      ImageView   
	image.setImageBitmap(bitmap);
						
}

B、ネットワークからhtmlソースを取得する
htmlソースはテキストに相当します
if(data!=null){
	String htmlText = new String(data,"UTF-8");
	html.setText(htmlText);
}else{
	Toast.makeText(MainActivity.this, R.string.htmlError, 1).show();
}

スクロールする内容をScrollViewに入れます
スクロールバーの効果:

スクロール表示の内容