ȸ¿ø·Î±×ÀÎ
¾Èµå·ÎÀ̵忡¼ À¥¼¹ö¿¡ post·Î ±Û¾²±â [Android]
8³â Àü
¾Èµå·ÎÀ̵å ÄÚµå»ó¿¡¼ °£´ÜÈ÷ id, pass¸¦ post ¹æ½ÄÀ¸·Î ¾²°í,
À¥¼¹ö´Â php¸¦ »ç¿ëÇÏ¿´´Ù.
php¿¡¼ ¹ÞÀº ±ÛÀ» txt·Î ÀúÀåÇÑ ÈÄ, echo·Î °á°ú¸¦ ¾´´Ù.
±×·¯¸é, ¾Èµå·ÎÀ̵å´Â ±× À¥¼¹öÀÇ echo¸¦ ¹Þ¾Æ¼ ¿¡¹Ä ȸ鿡 ÀÛ¼ºÇÏ´Â ÄÚµåÀÌ´Ù.
¾Èµå·ÎÀ̵忡¼ ¿Ã¸° ±ÛÀ» ¹Þ¾Æ³»¼ ¹ÝÀÀÇÏ´Â php ÄÚµå:
php°¡ ¾Èµå·ÎÀ̵åÀÇ ±ÛÀ» ¹Þ¾Ò´ÂÁö ¼¹ö´Ü¿¡¼ÀÇ È®ÀÎÀ» Çϱâ À§ÇØ,
db¿¡ ¿¬°áÇÏÁö ¾Ê°í ÀÏ´Ü °£´ÜÈ÷ ¹ÝÀÀÀÌ ¿À¸é ÆÄÀÏÀ» ÀÛ¼ºÇغ¸¾Ò´Ù.
<?
$id=$_REQUEST[id];
//echo("id: ".$id);
$pass=$_REQUEST[pass];
//echo("pass: ".$pass);
$imgpath = "/home/hosting_users/radstyle/www";
$fp=fopen($xmlpath."./test.txt",'w');
fwrite($fp,$id);
fwrite($fp,"\n");
fwrite($fp,$pass);
fclose($fp);
echo("id is $id, pass is $pass");
if(file_exists($fp)){
if(sizeof($fp)>0){
echo("id is $id, pass is $pass");
}else{
echo("file size is zero");
}
}
?>
À§ Äڵ尡 Àß ¹ÝÀÀÇÏ´ÂÁö À¥¿¡¼ È®ÀÎÇϱâ À§ÇÑ ÄÚµå´Â ¾Æ·¡¿Í °°´Ù.
get ¹æ½ÄÀ¸·Î Å×½ºÆ®ÇÏ·Á¸é,
http://radstyle.net/loginTest.php?id=aaaa&pass=1111 ¸¸À¸·Î µÉ °ÍÀÌ´Ù.
(Ãß°¡·Î php ÆÄÀÏ ¾÷·Îµå Æûµµ ÀÛ¼ºÇغ¼ °Í)
<?
echo("loginTest");
?>
<form action="loginTest.php" method="post" >
<input type=text name=id>
<input type=submit>
</form>
¾Èµå·ÎÀ̵å ÄÚµå(ÆÄÀÏ÷ºÎÇÔ):
package com.bbs.www;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class bbs extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView resultField = ( TextView ) findViewById( R.id.result ) ;
// try {
// downloadUrl("http://radstyle.net/sexfa.php", "ÃÖ¿øÈ¿");
// } catch (UnsupportedEncodingException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
try {
resultField.setText(sendData("wono77", "ÇϾẰ"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private String sendData(String id, String pwd)
throws ClientProtocolException, IOException {
// TODO Auto-generated method stub
HttpPost request = makeHttpPost(id, pwd,
"http://radstyle.net/loginTest.php");
// Get ¹æ½ÄÀÏ°æ¿ì
// HttpPost request = makeHttpGet( id, pwd,
// "http://www.shop-wiz.com/android_post.php" ) ;
HttpClient client = new DefaultHttpClient();
ResponseHandler<String> reshandler = new BasicResponseHandler();
String result = client.execute(request, reshandler);
return result;
}
// Post ¹æ½ÄÀÏ°æ¿ì
private HttpPost makeHttpPost(String user_id, String user_pwd, String url) {
// TODO Auto-generated method stub
HttpPost post = new HttpPost(url);
Vector<NameValuePair> params = new Vector<NameValuePair>();
params.add(new BasicNameValuePair("id", user_id));
params.add(new BasicNameValuePair("pass", user_pwd));
post.setEntity(makeEntity(params));
return post;
}
private HttpEntity makeEntity(Vector<NameValuePair> params) {
HttpEntity result = null;
try {
//result = new UrlEncodedFormEntity( params ) ;
result = new UrlEncodedFormEntity(params, HTTP.UTF_8);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
//´Ù¸¥ ¹æ½Ä
private void downloadUrl(String postURL, String input)
throws UnsupportedEncodingException {
try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(postURL);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", input));
UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,
HTTP.UTF_8);
post.setEntity(ent);
HttpResponse responsePOST = client.execute(post);
HttpEntity resEntity = responsePOST.getEntity();
} catch (Exception e) {
e.printStackTrace();
}
}
}
* ³²Àº ¹®Á¦:
encoding UTF8À» ÇÏ¿© ¼¹ö¿¡ ÇѱÛÀ» Á¦´ë·Î Àü¼ÛµÇ¾úÁö¸¸, °á°ú¸¦ ¹Þ¾Æº¸´Ï ÇѱÛÀÌ ±úÁ³À½.
ÀÌ ¹®Á¦ ÇØ°áÀ» À§Çؼ´Â ¾Æ·¡Ã³·³ ÇÏ¸é µÈ´Ù°í ÇÑ´Ù.
ÂüÁ¶:
http://www.androidpub.com/?_filter=search&mid=android_dev_qna&search_target=title_content&search_keyword=euc-kr&document_srl=549232
text¸¦ ³×Æ®¿öÅ©¸¦ ÅëÇؼ ¾µ ¶§ EUC-KR·Î º¯È¯Çؼ ½î¾ÆÁÖ°í,
OutputStreamWriter writer = new OutputStreamWriter(socket.getOutputStream(), "EUC-KR"));
writer.write(text);
¹ÞÀ»¶§µµ ¾Æ·¡Ã³·³...
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), "EUC-KR")));
String text = reader.readLine();
ÀϹÝÀûÀ¸·Î À¥ÀÇ ±ÛÀ» °¡Á®¿Ã¶§´Â Å×½ºÆ®·Î ÀÛ¼ºÇÑ ÄÚµåó·³ httpPost·Î ±ÛÀ» °¡Á®¿ÀÁö ¾Ê°í,
isStreamÀ¸·Î ¾Æ·¡Ã³·³ °¡Á®¿À¹Ç·Î, ÀÌ ¶§´Â ÇÑ±Û º¯È¯¿¡ ¹®Á¦°¡ ¾øÀ¸¸®¶ó »ý°¢µÈ´Ù.
url¿¡¼ streamÀ» °¡Á®¿ÂÈÄ, post ¹æ½ÄÀ¸·Î redirectedµÈ urlÀ» °¡Á®¿À´ø ¼Ò½ºÀÇ bufferReadÇÏ´ø ºÎºÐ°ú
Á¶ÇÕÇؼ À§ÀÇ ÀÎÄÚµùÀ» Àß ³Ö¾îº¸¸é Çѱ۵µ ÀÐÇôÁöÁö ¾ÊÀ»±î ½Í´Ù.
¾Æ·¡¿¡¼ ½ÎÀÌÁÖ¼Ò°ªÀ» °¡Á®¿Ã¶§´Â ÇѱÛÀÌ ±úÁ³¾ú´Ù.
/**
* url¿¡¼ À̹ÌÁö¸¦ °¡Á®¿Í¼ sdcard¿¡ ÀúÀåÇÏ´Â ÇÔ¼ö
* fullPath=urlPrefix + fileName
*
* @author wono77(2010.04.11)
* @param urlPrefix : "http://radstyle.net/images/cp/"
* @param fileName : a.jpg
*/
public void imageSaveFromURL(String urlPrefix, String fileName) {
String fullImageUrl = urlPrefix + fileName;
// String sdcardPath =
// Environment.getExternalStorageDirectory().toString();//+ "/sdcard/";
String sdcardFullPath = sdcardPath + fileName;
// urlÀ» ÆÄÀÏ¿¡ ÀúÀå
InputStream is = null;
try {
is = getUrlStream(fullImageUrl);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// directory Á¸Àç¿©ºÎ üũ Çѹø
File dir = new File(sdcardPath);
if (!dir.exists()) {
dir.mkdir();
}
if (is != null) {
try {
BufferedInputStream bis = new BufferedInputStream(is);
FileOutputStream fos = new FileOutputStream(new File(sdcardFullPath));
int n = 0;
try {
while ((n = bis.read()) != -1) {
fos.write(n);
}
} catch (IOException e) {
Log.v("a", "write fail");
} finally {
fos.close();
bis.close();
}
} catch (IOException e) {
Log.v("a", "fail");
}
}
}
/**
* url¿¡¼ inputStreamÀ» ¹Þ¾Æ¿À´Â ÇÔ¼ö
*
* @author wono77(2010.04.11)
* @param path
* @return
* @throws Exception
*/
public InputStream getUrlStream(String path) throws Exception {
InputStream is = null;
try {
// 20100222 ÃÖ¿øÈ¿ : timeout ¼ÂÆÃ
int desiredValue = 3000;
URL sourceImageUrl = new URL(path);
URLConnection t_connection = sourceImageUrl.openConnection();
t_connection.setConnectTimeout(desiredValue);
t_connection.setReadTimeout(desiredValue);
is = t_connection.getInputStream();
} catch (Exception e) {
if (is != null) {
is.close();
}
is = null;
}
return is;
}
/**
* urlÀ» ÀÔ·Â¹Þ¾Æ direct µÇ´Â urlÀÇ ³»¿ëÀ» °¡Á®¿À´Â ÇÔ¼ö
*
* @author wono77(2010.08.24)
* @param inputUrl
* @return
* @throws IOException
*/
public String getRedirectedUrlStream(String inputUrl) {
URL url = null;
String inputLine = "";
String response = "";
URLConnection conn = null;
try {
url = new URL(inputUrl);
conn = url.openConnection();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (conn instanceof HttpURLConnection) {
HttpURLConnection httpConn = (HttpURLConnection) conn;
try {
httpConn.setRequestMethod("POST");
httpConn.setRequestProperty("Content-type", "text/xml");
httpConn.setUseCaches(false);
httpConn.setDoInput(true);
httpConn.setDoOutput(true);
String testxml = null;
DataOutputStream out;
out = new DataOutputStream(httpConn.getOutputStream());
out.flush();
out.close();
// This next line throws an IOException if response code is not
// 200 <<======<<
BufferedReader in = null;
in = new BufferedReader(new InputStreamReader(
httpConn.getInputStream()));
while ((inputLine = in.readLine()) != null) {
response = response + inputLine;
}
in.close();
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return response;
} else {
return null;
}
}
* ´õ Çغ¼ ³»¿ë: À̹ÌÁö ¾÷·Îµå
Âü°íÇÒ°Í :
http://www.androidpub.com/?_filter=search&mid=android_dev_qna&search_target=title_content&search_keyword=post&page=4&division=-841993&last_division=-513271&document_srl=664496
* ÂüÁ¶ÇѱÛ:
1. ¾Èµå·ÎÀÌµå ±Û¾²±â (¿Ü±¹±Û)
: http://blog.dahanne.net/2009/08/16/how-to-access-http-resources-from-android/
2. ¾Èµå·ÎÀ̵å À̹ÌÁö ¾÷·Îµå with php
: http://blog.inculab.net/28
3. ÃʷϵÅÁöÀÇ ±Û ¸ðÀ½
: http://absenteye.blog.me/100110049502
4. °¡À帹ÀÌ ÂüÁ¶ÇѱÛ
: http://www.shop-wiz.com/document/android/execise_linkage_web
[¼³¸í]
À̹øÀå¿¡¼´Â À¥°úÀÇ µ¥ÀÌŸ ±³È¯¿¡ ´ëÇØ ¿¬¼ÓÀûÀ¸·Î ´Ù·ç¾î º¸°Ú½À´Ï´Ù.
±× ù¹ø°·Î post/get ¹æ½ÄÀ¸·Î À¥¿¡ µ¥ÀÌŸ¸¦ Àü´ÞÇÏ°í °á°úÄ¡¸¦ ´Ù½Ã ¹Þ´Â °ÍÀ» ó¸®ÇÏ°Ú½À´Ï´Ù.
ÀÏ´Ü ¾îÇÿ¡¼ ºÒ·¯¿Ã php ÆÄÀÏÀ» ÀÛ¼ºÇÏ°Ú½À´Ï´Ù.
ÀÌ phpÆÄÀÏÀº º¯¼ö¸¦ ¹Þ¾Æ¼ Ãâ·ÂÇØÁÖ´Â ÇÁ·Î±×·¥ÀÔ´Ï´Ù.
android_post.php
<?php
$id = $_POST["user_id"] ;
$pwd = $_POST["user_pwd"] ;
echo "your id is ".$id." and your passwd is ".$pwd;
?>
[¼³¸í]
ÀÌÁ¦´Â AndroidManifest.xml¿¡¼ ÀÎÅͳݿ¡ ´ëÇÑ ±ÇÇÑÀÌ ÁÝ´Ï´Ù.
<uses-permission android:name="android.permission.INTERNET" />
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ProjectLinkageWeb"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ProjectLinkageWeb"
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>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
[¼³¸í]
±×¸®°í main.xml ¿¡¼ µÎ°³ÀÇ ÀԷ¹ڽº¿Í ÇϳªÀÇ Ãâ·Â¹Ú½º, ±×¸®°í ¹öÆ°À» »ý¼ºÇÕ´Ï´Ù.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Input Your Id"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/user_id"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Input Your password"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/user_pwd"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/get_result"
android:text="Result Field"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/submitBtn"
android:text="Submit"
/>
</LinearLayout>
[¼³¸í]
±×¸®°í ¾Æ·¡Ã³·³... º°µµÀÇ ¼³¸íÀº µå¸®Áö ¾Ê°Ú½À´Ï´Ù.
¼Ò½º°¡ ¾î·ÆÁö ¾ÊÀ¸´Ï ´«À¸·Î µû¶ó º¸½Ã¸é µÉ°ÍÀÔ´Ï´Ù.
package com.ProjectLinkageWeb;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Vector;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class ProjectLinkageWeb extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById( R.id.submitBtn ) ;
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String user_id = ((EditText) findViewById(R.id.user_id)).getText().toString() ;
String user_pwd = ((EditText) findViewById(R.id.user_pwd)).getText().toString() ;
TextView resultField = ( TextView ) findViewById( R.id.get_result ) ;
try {
//µ¥ÀÌÅ͸¦ À¥¼¹ö¿¡ º¸³»°í ¹Þ¾Æ¿Â °á°ú¸¦ Ãâ·ÂÇÕ´Ï´Ù.
resultField.setText( sendData( user_id, user_pwd ) ) ;
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
private String sendData(String id, String pwd) throws ClientProtocolException, IOException {
// TODO Auto-generated method stub
HttpPost request = makeHttpPost( id, pwd, "http://www.shop-wiz.com/android_post.php" ) ;
// Get ¹æ½ÄÀÏ°æ¿ì
//HttpPost request = makeHttpGet( id, pwd, "http://www.shop-wiz.com/android_post.php" ) ;
HttpClient client = new DefaultHttpClient() ;
ResponseHandler<STRING> reshandler = new BasicResponseHandler() ;
String result = client.execute( request, reshandler ) ;
return result ;
}
//Post ¹æ½ÄÀÏ°æ¿ì
private HttpPost makeHttpPost(String user_id, String user_pwd, String url) {
// TODO Auto-generated method stub
HttpPost request = new HttpPost( url ) ;
Vector<NAMEVALUEPAIR> nameValue = new Vector<NAMEVALUEPAIR>() ;
nameValue.add( new BasicNameValuePair( "user_id", user_id ) ) ;
nameValue.add( new BasicNameValuePair( "user_pwd", user_pwd ) ) ;
request.setEntity( makeEntity(nameValue) ) ;
return request ;
}
//Get ¹æ½ÄÀÏ°æ¿ì
private HttpGet makeHttpGet(String user_id, String user_pwd, String url) {
// TODO Auto-generated method stub
Vector<NAMEVALUEPAIR> nameValue = new Vector<NAMEVALUEPAIR>() ;
nameValue.add( new BasicNameValuePair( "user_id", user_id ) ) ;
nameValue.add( new BasicNameValuePair( "user_pwd", user_pwd ) ) ;
String my_url = url + "?" + URLEncodedUtils.format( nameValue, null) ;
HttpGet request = new HttpGet( my_url ) ;
return request ;
}
private HttpEntity makeEntity( Vector<NAMEVALUEPAIR> nameValue ) {
HttpEntity result = null ;
try {
result = new UrlEncodedFormEntity( nameValue ) ;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result ;
}
}
ÀÌ ÇÁ·Î±×·¥À» ½ÇÇàÇÒ¶§´Â ¹Ýµå½Ã ÀÌŬ¸³½º»ó¿¡¼ ÀÎÅͳÝÀÌ µÇ´ÂÁö È®ÀÎÇØ¾ß ÇÕ´Ï´Ù.
̵̧ : 1345
̵̧
¸ñ·Ï