 |
|
|  |
|
 |
8³â Àü |
´ÙÀ½ ÇÁ·Î±×·¥Àº lesson01.php ¿Í ÀڷḦ ÁÖ°í¹Þ´Â ¿¹Á¦ÀÌ´Ù.
½ÇÇà °á°ú : ÆäÀÌÁö1·Î Àü¼ÛÇÏ°í ÆäÀÌÁö2·Î °á°ú ¹ÞÀ½
main.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableLayout
android:id="@+id/page01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1">
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ID :"/>
<EditText
android:id="@+id/edit_Id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PWord : "/>
<EditText
android:id="@+id/edit_pword"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:password="true"/>
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="°³³ä : "/>
<EditText
android:id="@+id/edit_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="´ëÃ¥ : "/>
<EditText
android:id="@+id/edit_subject"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:lines="4"/>
</TableRow>
<View
android:layout_height="2dip"
android:background="#AAAAAA"/>
<TableRow>
<Button
android:text=" Àü ¼Û "
android:id="@+id/button_submit"
android:layout_column="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</TableRow>
</TableLayout>
<LinearLayout
android:id="@+id/page02"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text_result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
Manifest.xml¿¡ ´ÙÀ½°ú °°Àº Æ۹̼ÇÀ» Áà¾ß ÇÑ´Ù.
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
project
package com.http_post;
import java.io.*;
import java.net.*;
import android.app.*;
import android.os.*;
import android.util.*;
import android.view.*;
import android.widget.*;
public class MainActivity extends TabActivity {
// Àü¿ªº¯¼ö¸¦ ¼±¾ðÇÑ´Ù
TabHost mTabHost = null;
String myId, myPWord, myTitle, mySubject, myResult;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTabHost = getTabHost(); // Tab ¸¸µé±â
mTabHost.addTab(mTabHost.newTabSpec("tab_1").setIndicator("¼¹ö·Î Àü¼Û").setContent(R.id.page01));
mTabHost.addTab(mTabHost.newTabSpec("tab_2").setIndicator("¼¹ö¿¡¼ ¹ÞÀ½").setContent(R.id.page02));
findViewById(R.id.button_submit).setOnClickListener(buttonClick);
}
//------------------------------
// button Click
//------------------------------
Button.OnClickListener buttonClick = new Button.OnClickListener() {
public void onClick(View v) {
// »ç¿ëÀÚ°¡ ÀÔ·ÂÇÑ ³»¿ëÀ» Àü¿ªº¯¼ö¿¡ ÀúÀåÇÑ´Ù
myId = ((EditText)(findViewById(R.id.edit_Id))).getText().toString();
myPWord = ((EditText)(findViewById(R.id.edit_pword))).getText().toString();
myTitle = ((EditText)(findViewById(R.id.edit_title))).getText().toString();
mySubject = ((EditText)(findViewById(R.id.edit_subject))).getText().toString();
HttpPostData(); // ¼¹ö¿Í ÀÚ·á ÁÖ°í¹Þ±â
}
};
//------------------------------
// Http Post·Î ÁÖ°í ¹Þ±â
//------------------------------
public void HttpPostData() {
try {
//--------------------------
// URL ¼³Á¤ÇÏ°í Á¢¼ÓÇϱâ
//--------------------------
URL url = new URL("lesson01.php"); // URL ¼³Á¤
HttpURLConnection http = (HttpURLConnection) url.openConnection(); // Á¢¼Ó
//--------------------------
// Àü¼Û ¸ðµå ¼³Á¤ - ±âº»ÀûÀÎ ¼³Á¤ÀÌ´Ù
//--------------------------
http.setDefaultUseCaches(false);
http.setDoInput(true); // ¼¹ö¿¡¼ Àб⠸ðµå ÁöÁ¤
http.setDoOutput(true); // ¼¹ö·Î ¾²±â ¸ðµå ÁöÁ¤
http.setRequestMethod("POST"); // Àü¼Û ¹æ½ÄÀº POST
// ¼¹ö¿¡°Ô À¥¿¡¼ <Form>À¸·Î °ªÀÌ ³Ñ¾î¿Â °Í°ú °°Àº ¹æ½ÄÀ¸·Î ó¸®Ç϶ó´Â °É ¾Ë·ÁÁØ´Ù
http.setRequestProperty("content-type", "application/x-www-form-urlencoded");
//--------------------------
// ¼¹ö·Î °ª Àü¼Û
//--------------------------
StringBuffer buffer = new StringBuffer();
buffer.append("id").append("=").append(myId).append("&"); // php º¯¼ö¿¡ °ª ´ëÀÔ
buffer.append("pword").append("=").append(myPWord).append("&"); // php º¯¼ö ¾Õ¿¡ '$' ºÙÀÌÁö ¾Ê´Â´Ù
buffer.append("title").append("=").append(myTitle).append("&"); // º¯¼ö ±¸ºÐÀº '&' »ç¿ë
buffer.append("subject").append("=").append(mySubject);
OutputStreamWriter outStream = new OutputStreamWriter(http.getOutputStream(), "EUC-KR");
PrintWriter writer = new PrintWriter(outStream);
writer.write(buffer.toString());
writer.flush();
//--------------------------
// ¼¹ö¿¡¼ Àü¼Û¹Þ±â
//--------------------------
InputStreamReader tmp = new InputStreamReader(http.getInputStream(), "EUC-KR");
BufferedReader reader = new BufferedReader(tmp);
StringBuilder builder = new StringBuilder();
String str;
while ((str = reader.readLine()) != null) { // ¼¹ö¿¡¼ ¶óÀδÜÀ§·Î º¸³»ÁÙ °ÍÀ̹ǷΠ¶óÀδÜÀ§·Î Àд´Ù
builder.append(str + "\n"); // View¿¡ Ç¥½ÃÇϱâ À§ÇØ ¶óÀÎ ±¸ºÐÀÚ Ãß°¡
}
myResult = builder.toString(); // Àü¼Û°á°ú¸¦ Àü¿ª º¯¼ö¿¡ ÀúÀå
((TextView)(findViewById(R.id.text_result))).setText(myResult);
Toast.makeText(MainActivity.this, "Àü¼Û ÈÄ °á°ú ¹ÞÀ½", 0).show();
} catch (MalformedURLException e) {
//
} catch (IOException e) {
//
} // try
} // HttpPostData
} // Activity
lesson01.php
<?
// º¯¼ö ³»¿ë È®ÀÎ
if ($id == "") $id = "³Ê´Â IDµµ ¾ø³Ä?";
if ($pword == "") $pword = "ĥĥ¸Â°Ô ºñ¹Ð¹øÈ£µµ Àؾî¸Ô°í ´Ù´Ï³×...";
if ($title == "") $title = "Áõ¸» °³³ä¾ø´Â »ç¶÷Àϼ¼...";
if ($subject == "") $subject = "¿¡±¸~~ ´ëÃ¥ÀÌ ¾ø³×...";
// º¯¼ö ³»¿ë Ãâ·Â
echo ("
´Ô²²¼ PHP·Î Àü¼ÛÇÑ ³»¿ëÀÔ´Ï´Ù
-----------------------------------------------------------------\r\n
»ç¿ëÀÚ ID : $id \r\n
ºñ¹Ð¹øÈ£ : $pword \r\n
°³³ä : $title \r\n
´ëÃ¥ : $subject \r\n
-----------------------------------------------------------------\r\n
ÃàÇϵ帳´Ï´Ù. lesson01.php ¸¦ Á¤»èÀûÀ¸·Î È£ÃâÇϼ̽À´Ï´Ù!
");
?>
|
|
̵̧ : 285 |
̵̧
¸ñ·Ï
|
|
|  |
|