php에서 xml로 출력해 앱에서 접근해 파싱까지는 성공했습니다.
그러나 문제는 앱에서 php로 sql 쿼리를 날려서 처리하게 하고 처리 결과를 앱으로 다시 읽어야 하는데
이 과정에 대해서 찾아봣는데 잘 안보이더라고요.
제 php 소스입니다.
<?php
//require_once 'login.php';
$db_hostname = 'localhost';
$db_database = 'publications';
$db_username = 'jim';
$db_password = '222';
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
if(!$db_server) die("unable to connect to mysql : " . mysql_error());
mysql_select_db($db_database) or die("unable to select db: " . mysql_error());
$query = "SELECT * FROM classics";
$result = mysql_query($query);
if(!$result) die ("DB access failed : " . mysql_error());
$rows = mysql_num_rows($result);
echo "<?xml version='1.0' encoding='utf-8'?>";
echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" .
"\"http://www.w3.org/TR/xhtml11/DTD/xhtml11-strict.dtd\">" .
"<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">";
$query_test = "select * from classics";
$query_result = mysql_query($query_test);
$tempcount = 0;
while ($result = mysql_fetch_object($query_result))
{
echo "<record>";
echo "<item ";
echo "author='$result->author'";
echo "title='$result->title'";
echo "category='$result->category'";
echo "year='$result->year'";
echo "isbn='$result->isbn'>";
echo ++$tempcount."</item>";
echo "</record>";
}
mysql_close($db_server);
function get_post($var)
{
return mysql_real_escape_string($_POST[$var]);
}
?>
대충 내용은 DB를 읽어서 xml 로 출력하게 하였습니다. 이렇게해서 앱에서 접근하였고요. (PullParser 를 사용했습니다.)
위에서도 적었듯이
1) 앱에서 쿼리를 어떻게 날리며 php 에서 어떻해야 받아서 처리하고 결과를 xml 로 출력할 수 있을까요?
2) 1)에서 출력된 결과를 기존의 파서를 써서 접근이 충분히 가능한지요?
고수님들으 아낌없는 조언 부탁드립니다.
참고로 앱에서 파서는 아래와 같이 썼습니다.
boolean initem = false;
String ItemName = "";
try
{
url = new URL("http://192.168.0.35/index.php");
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser parser = factory.newPullParser();
parser.setInput(url.openStream(), "utf-8");
int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT)
{
switch (eventType)
{
case XmlPullParser.START_TAG:
if (parser.getName().equals("item"))
{
aAuthor = parser.getAttributeValue(null, "author");
aTitle = parser.getAttributeValue(null, "title");
aCategory = parser.getAttributeValue(null, "category");
aYear = parser.getAttributeValue(null, "year");
aIsbn = parser.getAttributeValue(null, "isbn");
initem = true;
ItemName += aAuthor + " " + aTitle + " " + aCategory + " " + aYear + " " + aIsbn + " \n\n";
}
break;
case XmlPullParser.TEXT:
if (initem)
{
ItemName = ItemName + " " + parser.getText() + "\n\n";
initem = false;
}
break;
}
eventType = parser.next();
}
mResult.setText(ItemName);
}
catch (Exception e)
{
;
}다음은 겟으로 받아서 xml로 바꾸는 php 소스인데
공개해봅니다..
<?
require_once('../config/dbconn.php');
$queryString= $_GET['query'];
//사용자가 원하는 값, 예를들어 사용자가 aaa 라는 글자를 원하면 get으로 받고
// 안드로이드에서 뿌리는 주소는 http://홈페이지 주소/이거이름.php?query=aaa 가 되겠네요.
echo "<?xml version='1.0' encoding='utf-8'?>";
$query_search_list = "select * from products where pro_title like '%".$queryString."%'";
//레코드에 특정 칼럼에 aaa가 들어가는걸 선택한 뒤
$query_search_result = mysql_query($query_search_list);
$total = mysql_num_rows($query_search_result);
//뿌린다, total 변수는 aaa가 들어가는게 몇개인지 count
if ($total == 0) {
echo "nodata";
} else {
echo "<record><total count='$total'></total></record>";
while ($result = mysql_fetch_object($query_search_result)) {
echo "<record>";
echo "<item ";
echo "title='$result->pro_title'";
echo "img1='$result->pro_img1'";
echo "writer='$result->pro_writer'";
echo "price='$result->pro_price'";
echo "publish='$result->pro_publish'>";
echo $result->pro_no."</item>";
echo "</record>";
}
}
?>
해당 aaa 값은 안드로이드상 에서는
예를들어 사용자가 edittext 에서 aaa를 입력했을 경우나...
뭐 기타 등등 다양하죠...
검색 결과 뿌려주는걸 구현하는데 사용됐던 소스입니다...
책 제목 검색하는...참고하세요
만약 글 쓰기 같은걸 날리는걸 원하시면
이걸 응용 하셔도 되지만 그 부분은 post 로 날리는걸 추천해드립니다.
post 방식은 c2dm 인가 하는걸 찾아보세요.




우선 php 페이지를 하나 더 (비슷하게) 만드십시요.
검색이나 그와 비슷한걸 구현하시는거 같은데.
$_GET 변수로 해서 , 보통 홈페이지라고 치면 값을 form 으로 get 날리는거죠
그렇 그 주소는
http://homepageaddress.com/phpname.php?get밸류값=값
이런식으로 날라가죠,
인터넷에서 많이 보셨을겁니다. 홈페이지 주소 보면 말이죠, 복잡하게 막 써있죠, 그게 대부분 get으로 날리는 겁니다.
그러면 xml 파싱해오실때 위에 예제로 말씀드린 주소값으로 날리면 되는겁니다.
보안상 좋지는 않지는 않아서 POST를 이용하는 방법도 있는데,
get으로 하셔도 되긴합니다.