001.
package
com.example.testdata;
002.
003.
import
java.io.IOException;
004.
005.
import
org.json.JSONException;
006.
import
org.json.JSONObject;
007.
import
org.ksoap2.SoapEnvelope;
008.
import
org.ksoap2.serialization.SoapObject;
009.
import
org.ksoap2.serialization.SoapSerializationEnvelope;
010.
import
org.ksoap2.transport.HttpTransportSE;
011.
import
org.xmlpull.v1.XmlPullParserException;
012.
013.
import
android.os.Bundle;
014.
import
android.os.StrictMode;
015.
import
android.annotation.SuppressLint;
016.
import
android.app.Activity;
017.
import
android.app.AlertDialog;
018.
import
android.view.View;
019.
import
android.view.Menu;
020.
import
android.widget.Button;
021.
import
android.widget.EditText;
022.
import
android.widget.Toast;
023.
024.
public
class
MainActivity
extends
Activity {
025.
026.
030.
private
final
String METHOD_NAME =
"insertMember"
;
031.
032.
@SuppressLint
(
"NewApi"
)
033.
@Override
034.
public
void
onCreate(Bundle savedInstanceState) {
035.
super
.onCreate(savedInstanceState);
036.
setContentView(R.layout.activity_main);
037.
038.
039.
if
(android.os.Build.VERSION.SDK_INT >
9
) {
040.
StrictMode.ThreadPolicy policy =
new
StrictMode.ThreadPolicy.Builder().permitAll().build();
041.
StrictMode.setThreadPolicy(policy);
042.
}
043.
044.
045.
final
Button btnSave = (Button) findViewById(R.id.btnSave);
046.
047.
btnSave.setOnClickListener(
new
View.OnClickListener() {
048.
public
void
onClick(View v) {
049.
if
(SaveData())
050.
{
051.
052.
}
053.
}
054.
});
055.
056.
}
057.
058.
public
boolean
SaveData()
059.
{
060.
061.
062.
final
EditText txtUsername = (EditText)findViewById(R.id.txtUsername);
063.
final
EditText txtPassword = (EditText)findViewById(R.id.txtPassword);
064.
final
EditText txtConPassword = (EditText)findViewById(R.id.txtConPassword);
065.
final
EditText txtName = (EditText)findViewById(R.id.txtName);
066.
final
EditText txtEmail = (EditText)findViewById(R.id.txtEmail);
067.
final
EditText txtTel = (EditText)findViewById(R.id.txtTel);
068.
069.
070.
071.
final
AlertDialog.Builder ad =
new
AlertDialog.Builder(
this
);
072.
073.
ad.setTitle(
"Error! "
);
074.
ad.setIcon(android.R.drawable.btn_star_big_on);
075.
ad.setPositiveButton(
"Close"
,
null
);
076.
077.
078.
if
(txtUsername.getText().length() ==
0
)
079.
{
080.
ad.setMessage(
"Please input [Username] "
);
081.
ad.show();
082.
txtUsername.requestFocus();
083.
return
false
;
084.
}
085.
086.
if
(txtPassword.getText().length() ==
0
|| txtConPassword.getText().length() ==
0
)
087.
{
088.
ad.setMessage(
"Please input [Password/Confirm Password] "
);
089.
ad.show();
090.
txtPassword.requestFocus();
091.
return
false
;
092.
}
093.
094.
if
(!txtPassword.getText().toString().equals(txtConPassword.getText().toString()))
095.
{
096.
ad.setMessage(
"Password and Confirm Password Not Match! "
);
097.
ad.show();
098.
txtConPassword.requestFocus();
099.
return
false
;
100.
}
101.
102.
if
(txtName.getText().length() ==
0
)
103.
{
104.
ad.setMessage(
"Please input [Name] "
);
105.
ad.show();
106.
txtName.requestFocus();
107.
return
false
;
108.
}
109.
110.
if
(txtEmail.getText().length() ==
0
)
111.
{
112.
ad.setMessage(
"Please input [Email] "
);
113.
ad.show();
114.
txtEmail.requestFocus();
115.
return
false
;
116.
}
117.
118.
if
(txtTel.getText().length() ==
0
)
119.
{
120.
ad.setMessage(
"Please input [Tel] "
);
121.
ad.show();
122.
txtTel.requestFocus();
123.
return
false
;
124.
}
125.
126.
127.
SoapObject request =
new
SoapObject(NAMESPACE, METHOD_NAME);
128.
request.addProperty(
"strUsername"
, txtUsername.getText().toString());
129.
request.addProperty(
"strPassword"
, txtPassword.getText().toString());
130.
request.addProperty(
"strName"
, txtName.getText().toString());
131.
request.addProperty(
"strEmail"
, txtEmail.getText().toString());
132.
request.addProperty(
"strTel"
, txtTel.getText().toString());
133.
134.
SoapSerializationEnvelope envelope =
new
SoapSerializationEnvelope(
135.
SoapEnvelope.VER11);
136.
137.
envelope.setOutputSoapObject(request);
138.
139.
HttpTransportSE androidHttpTransport =
new
HttpTransportSE(URL);
140.
String resultServer =
null
;
141.
142.
try
{
143.
androidHttpTransport.call(SOAP_ACTION, envelope);
144.
SoapObject result = (SoapObject) envelope.bodyIn;
145.
resultServer = result.getProperty(
0
).toString();
146.
}
catch
(IOException e) {
147.
148.
e.printStackTrace();
149.
}
catch
(XmlPullParserException e) {
150.
151.
e.printStackTrace();
152.
}
153.
154.
155.
156.
157.
158.
159.
160.
161.
162.
163.
String strStatusID =
"0"
;
164.
String strError =
"Unknow Status!"
;
165.
166.
JSONObject c;
167.
try
{
168.
c =
new
JSONObject(resultServer);
169.
strStatusID = c.getString(
"StatusID"
);
170.
strError = c.getString(
"Error"
);
171.
}
catch
(JSONException e) {
172.
173.
e.printStackTrace();
174.
}
175.
176.
177.
if
(strStatusID.equals(
"0"
))
178.
{
179.
ad.setMessage(strError);
180.
ad.show();
181.
}
182.
else
183.
{
184.
Toast.makeText(MainActivity.
this
,
"Save Data Successfully"
, Toast.LENGTH_SHORT).show();
185.
txtUsername.setText(
""
);
186.
txtPassword.setText(
""
);
187.
txtConPassword.setText(
""
);
188.
txtName.setText(
""
);
189.
txtEmail.setText(
""
);
190.
txtTel.setText(
""
);
191.
}
192.
193.
194.
return
true
;
195.
}
196.
197.
198.
@Override
199.
public
boolean
onCreateOptionsMenu(Menu menu) {
200.
getMenuInflater().inflate(R.menu.main, menu);
201.
return
true
;
202.
}
203.
204.
}