웹이 웹서버의 어플리케이션을 불러 서비스를 요청하면, 웹 어플리케이션은 서비스 요청에 대한 응답을 합니다.
데이터베이스에 정보를 저장하거나 정보를 추출하여 응답하게 되는 것이지요^^
특정 사이트의 웹 어플리케이션을 호출하고 그 응답으로 반환되는 내용을 보는 것이 웹서버 통신의 기초적인
내용입니다.
데이터 하나하나가 특정한 의미를 갖게 될 때, 정해진 데이터 포맷에 따라 반환되는거죠.
http://google.co.kt 의 웹 웹 어플리케이션에 서비스를 요청하고 서비스 결과를 받게 되는 웹문서 소스코드를
작성해볼 것입니다.
이 글이 도움이 되신다면, 로그인 필요없이 추천버튼 클릭 한번 부탁드립니다^^
프로젝트 네임 : HttpWeb111
패키지 네임 : com.andro
CreatActivity : Three.java
String.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">웹문서 소스</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
</resources>
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- 웹문서 소스 출력 영역 -->
<EditText
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="15dp"
android:id="@+id/webpage_src" />
</LinearLayout>
Three.java
package com.andro;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.EditText;
public class Three extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText et = (EditText)findViewById(R.id.webpage_src);
URL url = null; // Uri => android.net 하위
HttpsURLConnection urlConnection = null;
BufferedInputStream buf = null;
try{
//URL 지정 및 접속
//웹서버 URL 지정
url = new URL("http://www.google.co.kr/");
//URL 접속
urlConnection = (HttpsURLConnection)url.openConnection();
//웸문서 소스 버퍼에 저장
//DB 다운로드
buf = new BufferedInputStream(urlConnection.getInputStream());
//데이터를 버퍼에 기록
BufferedReader br = new BufferedReader(new InputStreamReader(buf,"euc-kr"));
String line = null;
String page = null;
//버퍼의 웹문서 소스를 줄 단위로 읽어들인후(line을 통해)
while((line=br.readLine()) != null){
page = page + line;
}//while end
//page 내용을 화면에 출력한다.
et.setText(page);
}catch(Exception ex){
et.setText(ex.getMessage());
}finally{
//연결해제
urlConnection.disconnect();
}
}//end
}//class END
AndroidMainfest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.andro"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.andro.Three"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
'IT > Android' 카테고리의 다른 글
안드로이드 SQLite DB 활용한 고객등록과 조회 3-2 (0) | 2013.07.19 |
---|---|
안드로이드 SQLite DB 활용한 고객등록과 조회 3-1 (0) | 2013.07.18 |
WebView / 웹 문서 / 웹 뷰 (0) | 2013.07.16 |
Android 고객등록/정보전달 02 (0) | 2013.07.15 |
Android 고객등록/정보전달 01 (0) | 2013.07.12 |