package
com.example.iamapainter;
import
java.io.InputStream;
import
java.net.URL;
import
java.net.URLEncoder;
import
org.xmlpull.v1.XmlPullParser;
import
org.xmlpull.v1.XmlPullParserFactory;
import
android.app.Activity;
import
android.content.Intent;
import
android.os.Bundle;
import
android.os.StrictMode;
import
android.util.Log;
import
android.view.View;
import
android.widget.Button;
import
android.widget.EditText;
import
android.widget.Toast;
public
class
joinActivity
extends
Activity {
Button joinOk;
EditText edtname, edtid, edtpass;
private
static
final
String SERVER_ADDRESS =
"http://아이피:8080"
;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.join);
StrictMode.ThreadPolicy policy =
new
StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
joinOk = (Button) findViewById(R.id.joinOk);
edtname = (EditText) findViewById(R.id.edtname);
edtid = (EditText) findViewById(R.id.edtid);
edtpass = (EditText) findViewById(R.id.edtpass);
joinOk.setOnClickListener(
new
View.OnClickListener() {
public
void
onClick(View v){
if
(edtname.getText().toString().equals(
""
) || edtid.getText().toString().equals(
""
) || edtpass.getText().toString().equals(
""
)) {
Toast.makeText(joinActivity.
this
,
"입력오류입니다"
, Toast.LENGTH_SHORT).show();
return
;
}
runOnUiThread(
new
Runnable() {
public
void
run() {
String name = edtname.getText().toString();
String id = edtid.getText().toString();
String password = edtpass.getText().toString();
try
{
URL url =
new
URL(SERVER_ADDRESS +
"/sos.php?"
+
"name="
+ URLEncoder.encode(name,
"UTF-8"
)
+
"&id="
+ URLEncoder.encode(id,
"UTF-8"
)
+
"&password="
+ URLEncoder.encode(password,
"UTF-8"
));
url.openStream();
String result = getXmlData(
"insertresult.xml"
,
"result"
);
if
(result.equals(
"1"
)){
Toast.makeText(joinActivity.
this
,
"가입완료! 로그인하세요"
, Toast.LENGTH_SHORT).show();
edtname.setText(
""
);
edtid.setText(
""
);
edtpass.setText(
""
);
startActivity(
new
Intent(joinActivity.
this
, loginActivity.
class
));
finish();
}
else
Toast.makeText(joinActivity.
this
,
"회권 가입 실패(ID 중복일 수 있습니다.)"
,Toast.LENGTH_SHORT).show();
}
catch
(Exception e){
Log.e(
"Error"
, e.getMessage());
}
}
});
}
});
}
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;
}
}