package
gong.mille.books;
import
java.io.InputStream;
import
java.net.URL;
import
java.net.URLEncoder;
import
java.util.ArrayList;
import
org.xmlpull.v1.XmlPullParser;
import
org.xmlpull.v1.XmlPullParserFactory;
import
android.app.Activity;
import
android.app.ProgressDialog;
import
android.os.Bundle;
import
android.os.Handler;
import
android.util.Log;
import
android.view.View;
import
android.widget.ArrayAdapter;
import
android.widget.Button;
import
android.widget.EditText;
import
android.widget.ListView;
import
android.widget.Toast;
public
class
Android_BookActivity
extends
Activity {
private
final
String SERVER_ADDRESS =
"http://127.0.0.1/Books"
; //서버 주소(php파일이 저장되어있는 경로까지, 절대로
127.0
.
0.1
이나 localhost를 쓰면 안된다!! 그러므로 아이피는 자기 아이피로.....)
EditText edtname;
EditText edtprice;
Button btninsert;
Button btnsearch;
ListView list;
ArrayList<string> data;
ArrayAdapter<string> adapter;
/** Called when the activity is first created. */
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
edtname = (EditText )findViewById(R.id.editText1);
edtprice = (EditText )findViewById(R.id.editText2);
btninsert = (Button )findViewById(R.id.button1);
btnsearch = (Button )findViewById(R.id.button2);
list = (ListView )findViewById(R.id.listView1);
data =
new
ArrayList<string>();
adapter =
new
ArrayAdapter<string>(
this
,
android.R.layout.simple_list_item_1, data);
list.setAdapter(adapter);
btninsert.setOnClickListener(
new
View.OnClickListener() {
public
void
onClick(View v) {
if
( edtname.getText().toString().equals(
""
) ||
edtprice.getText().toString().equals(
""
) ) {
Toast.makeText(Android_BookActivity.
this
,
"이름이나 가격을 입력하세요"
, Toast.LENGTH_SHORT).show();
return
;
}
runOnUiThread(
new
Runnable() {
public
void
run() {
String name = edtname.getText().toString();
String price = edtprice.getText().toString();
try
{
URL url =
new
URL(SERVER_ADDRESS +
"/insert.php?"
+
"name="
+ URLEncoder.encode(name,
"UTF-8"
)
+
"&price="
+ URLEncoder.encode(price,
"UTF-8"
));
url.openStream();
String result = getXmlData(
"insertresult.xml"
,
"result"
);
if
(result.equals(
"1"
)) {
Toast.makeText(Android_BookActivity.
this
,
"DB insert 성공"
, Toast.LENGTH_SHORT).show();
edtname.setText(
""
);
edtprice.setText(
""
);
}
else
Toast.makeText(Android_BookActivity.
this
,
"DB insert 실패"
, Toast.LENGTH_SHORT).show();
}
catch
(Exception e) {
Log.e(
"Error"
, e.getMessage());
}
}
});
}
});
btnsearch.setOnClickListener(
new
View.OnClickListener() {
public
void
onClick(View arg0) {
final
Handler handler =
new
Handler();
runOnUiThread(
new
Runnable() {
public
void
run() {
final
ProgressDialog dialog = ProgressDialog.show(
Android_BookActivity.
this
,
"불러오는중....."
,
"잠시만 기다려주세요."
);
handler.post(
new
Runnable() {
public
void
run() {
try
{
data.clear();
URL url =
new
URL(SERVER_ADDRESS +
"/search.php"
);
url.openStream();
ArrayList<string> namelist = getXmlDataList(
"searchresult.xml"
,
"name"
);
ArrayList<string> pricelist = getXmlDataList(
"searchresult.xml"
,
"price"
);
if
(namelist.isEmpty())
data.add(
"아무것도 검색되지 않았습니다."
);
else
{
for
(
int
i =
0
; i < namelist.size(); i++) {
String str = namelist.get(i) +
" - "
+ pricelist.get(i);
data.add(str);
}
}
}
catch
(Exception e) {
Log.e(
"Error"
, e.getMessage());
}
finally
{
dialog.dismiss();
adapter.notifyDataSetChanged();
}
}
});
}
});
}
});
}
private
String getXmlData(String filename, String str) {
String rss = SERVER_ADDRESS +
"/"
;
String ret =
""
;
try
{
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(
true
);
XmlPullParser xpp = factory.newPullParser();
URL server =
new
URL(rss + filename);
InputStream is = server.openStream();
xpp.setInput(is,
"UTF-8"
);
int
eventType = xpp.getEventType();
while
(eventType != XmlPullParser.END_DOCUMENT) {
if
(eventType == XmlPullParser.START_TAG) {
if
(xpp.getName().equals(str)) {
ret = xpp.nextText();
}
}
eventType = xpp.next();
}
}
catch
(Exception e) {
Log.e(
"Error"
, e.getMessage());
}
return
ret;
}
private
ArrayList<string> getXmlDataList(String filename, String str) {
String rss = SERVER_ADDRESS +
"/"
;
ArrayList<string> ret =
new
ArrayList<string>();
try
{
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(
true
);
XmlPullParser xpp = factory.newPullParser();
URL server =
new
URL(rss + filename);
InputStream is = server.openStream();
xpp.setInput(is,
"UTF-8"
);
int
eventType = xpp.getEventType();
while
(eventType != XmlPullParser.END_DOCUMENT) {
if
(eventType == XmlPullParser.START_TAG) {
if
(xpp.getName().equals(str)) {
ret.add(xpp.nextText());
}
}
eventType = xpp.next();
}
}
catch
(Exception e) {
Log.e(
"Error"
, e.getMessage());
}
return
ret;
}
}
</string></string></string></string></string></string></string></string></string></string>