001.
package
com.myapp;
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.content.Intent;
019.
import
android.view.View;
020.
import
android.view.Menu;
021.
import
android.widget.Button;
022.
import
android.widget.EditText;
023.
import
android.widget.TextView;
024.
import
android.widget.Toast;
025.
026.
public
class
UpdateActivity
extends
Activity {
027.
028.
032.
private
final
String METHOD_NAME1 =
"resultMember"
;
033.
034.
038.
private
final
String METHOD_NAME2 =
"updateMember"
;
039.
040.
@SuppressLint
(
"NewApi"
)
041.
@Override
042.
public
void
onCreate(Bundle savedInstanceState) {
043.
super
.onCreate(savedInstanceState);
044.
setContentView(R.layout.activity_update);
045.
046.
047.
if
(android.os.Build.VERSION.SDK_INT >
9
) {
048.
StrictMode.ThreadPolicy policy =
new
StrictMode.ThreadPolicy.Builder().permitAll().build();
049.
StrictMode.setThreadPolicy(policy);
050.
}
051.
052.
showInfo();
053.
054.
055.
Button btnSave = (Button) findViewById(R.id.btnSave);
056.
057.
btnSave.setOnClickListener(
new
View.OnClickListener() {
058.
public
void
onClick(View v) {
059.
if
(SaveData())
060.
{
061.
062.
Intent newActivity =
new
Intent(UpdateActivity.
this
,MainActivity.
class
);
063.
startActivity(newActivity);
064.
}
065.
}
066.
});
067.
068.
069.
070.
final
Button btnCancel = (Button) findViewById(R.id.btnCancel);
071.
072.
btnCancel.setOnClickListener(
new
View.OnClickListener() {
073.
public
void
onClick(View v) {
074.
Intent newActivity =
new
Intent(UpdateActivity.
this
,MainActivity.
class
);
075.
startActivity(newActivity);
076.
}
077.
});
078.
079.
}
080.
081.
public
void
showInfo()
082.
{
083.
084.
final
TextView tMemberID = (TextView)findViewById(R.id.txtMemberID);
085.
final
TextView tUsername = (TextView)findViewById(R.id.txtUsername);
086.
final
TextView tPassword = (TextView)findViewById(R.id.txtPassword);
087.
final
TextView tConPassword = (TextView)findViewById(R.id.txtConPassword);
088.
final
TextView tName = (TextView)findViewById(R.id.txtName);
089.
final
TextView tEmail = (TextView)findViewById(R.id.txtEmail);
090.
final
TextView tTel = (TextView)findViewById(R.id.txtTel);
091.
092.
Button btnSave = (Button) findViewById(R.id.btnSave);
093.
Button btnCancel = (Button) findViewById(R.id.btnCancel);
094.
095.
096.
Intent intent= getIntent();
097.
final
String MemberID = intent.getStringExtra(
"MemberID"
);
098.
099.
SoapObject request =
new
SoapObject(NAMESPACE1, METHOD_NAME1);
100.
request.addProperty(
"strMemberID"
, MemberID);
101.
102.
SoapSerializationEnvelope envelope =
new
SoapSerializationEnvelope(
103.
SoapEnvelope.VER11);
104.
105.
envelope.setOutputSoapObject(request);
106.
107.
HttpTransportSE androidHttpTransport =
new
HttpTransportSE(URL1);
108.
String resultServer =
null
;
109.
110.
111.
112.
113.
114.
115.
try
{
116.
androidHttpTransport.call(SOAP_ACTION1, envelope);
117.
SoapObject result = (SoapObject) envelope.bodyIn;
118.
resultServer = result.getProperty(
0
).toString();
119.
}
catch
(IOException e) {
120.
121.
e.printStackTrace();
122.
}
catch
(XmlPullParserException e) {
123.
124.
e.printStackTrace();
125.
}
126.
127.
String strMemberID =
""
;
128.
String strUsername =
""
;
129.
String strPassword =
""
;
130.
String strName =
""
;
131.
String strEmail =
""
;
132.
String strTel =
""
;
133.
134.
JSONObject c;
135.
try
{
136.
c =
new
JSONObject(resultServer);
137.
strMemberID = c.getString(
"MemberID"
);
138.
strUsername = c.getString(
"Username"
);
139.
strPassword = c.getString(
"Password"
);
140.
strName = c.getString(
"Name"
);
141.
strEmail = c.getString(
"Email"
);
142.
strTel = c.getString(
"Tel"
);
143.
144.
if
(!strMemberID.equals(
""
))
145.
{
146.
tMemberID.setText(strMemberID);
147.
tUsername.setText(strUsername);
148.
tPassword.setText(strPassword);
149.
tConPassword.setText(strPassword);
150.
tName.setText(strName);
151.
tEmail.setText(strEmail);
152.
tTel.setText(strTel);
153.
}
154.
else
155.
{
156.
tMemberID.setText(
"-"
);
157.
tUsername.setText(
"-"
);
158.
tName.setText(
"-"
);
159.
tEmail.setText(
"-"
);
160.
tTel.setText(
"-"
);
161.
btnSave.setEnabled(
false
);
162.
btnCancel.requestFocus();
163.
}
164.
165.
}
catch
(JSONException e) {
166.
167.
e.printStackTrace();
168.
}
169.
170.
}
171.
172.
173.
public
boolean
SaveData()
174.
{
175.
176.
177.
final
TextView txtMemberID = (TextView)findViewById(R.id.txtMemberID);
178.
final
EditText txtPassword = (EditText)findViewById(R.id.txtPassword);
179.
final
EditText txtConPassword = (EditText)findViewById(R.id.txtConPassword);
180.
final
EditText txtName = (EditText)findViewById(R.id.txtName);
181.
final
EditText txtEmail = (EditText)findViewById(R.id.txtEmail);
182.
final
EditText txtTel = (EditText)findViewById(R.id.txtTel);
183.
184.
185.
186.
final
AlertDialog.Builder ad =
new
AlertDialog.Builder(
this
);
187.
188.
ad.setTitle(
"Error! "
);
189.
ad.setIcon(android.R.drawable.btn_star_big_on);
190.
ad.setPositiveButton(
"Close"
,
null
);
191.
192.
193.
if
(txtPassword.getText().length() ==
0
|| txtConPassword.getText().length() ==
0
)
194.
{
195.
ad.setMessage(
"Please input [Password/Confirm Password] "
);
196.
ad.show();
197.
txtPassword.requestFocus();
198.
return
false
;
199.
}
200.
201.
if
(!txtPassword.getText().toString().equals(txtConPassword.getText().toString()))
202.
{
203.
ad.setMessage(
"Password and Confirm Password Not Match! "
);
204.
ad.show();
205.
txtConPassword.requestFocus();
206.
return
false
;
207.
}
208.
209.
if
(txtName.getText().length() ==
0
)
210.
{
211.
ad.setMessage(
"Please input [Name] "
);
212.
ad.show();
213.
txtName.requestFocus();
214.
return
false
;
215.
}
216.
217.
if
(txtEmail.getText().length() ==
0
)
218.
{
219.
ad.setMessage(
"Please input [Email] "
);
220.
ad.show();
221.
txtEmail.requestFocus();
222.
return
false
;
223.
}
224.
225.
if
(txtTel.getText().length() ==
0
)
226.
{
227.
ad.setMessage(
"Please input [Tel] "
);
228.
ad.show();
229.
txtTel.requestFocus();
230.
return
false
;
231.
}
232.
233.
234.
SoapObject request =
new
SoapObject(NAMESPACE2, METHOD_NAME2);
235.
request.addProperty(
"strMemberID"
, txtMemberID.getText().toString());
236.
request.addProperty(
"strPassword"
, txtPassword.getText().toString());
237.
request.addProperty(
"strName"
, txtName.getText().toString());
238.
request.addProperty(
"strEmail"
, txtEmail.getText().toString());
239.
request.addProperty(
"strTel"
, txtTel.getText().toString());
240.
241.
SoapSerializationEnvelope envelope =
new
SoapSerializationEnvelope(
242.
SoapEnvelope.VER11);
243.
244.
envelope.setOutputSoapObject(request);
245.
246.
HttpTransportSE androidHttpTransport =
new
HttpTransportSE(URL2);
247.
String resultServer =
null
;
248.
249.
try
{
250.
androidHttpTransport.call(SOAP_ACTION2, envelope);
251.
SoapObject result = (SoapObject) envelope.bodyIn;
252.
resultServer = result.getProperty(
0
).toString();
253.
}
catch
(IOException e) {
254.
255.
e.printStackTrace();
256.
}
catch
(XmlPullParserException e) {
257.
258.
e.printStackTrace();
259.
}
260.
261.
262.
263.
264.
265.
266.
267.
268.
269.
270.
271.
String strStatusID =
"0"
;
272.
String strError =
"Unknow Status!"
;
273.
274.
JSONObject c;
275.
try
{
276.
c =
new
JSONObject(resultServer);
277.
strStatusID = c.getString(
"StatusID"
);
278.
strError = c.getString(
"Error"
);
279.
}
catch
(JSONException e) {
280.
281.
e.printStackTrace();
282.
}
283.
284.
285.
if
(strStatusID.equals(
"0"
))
286.
{
287.
ad.setMessage(strError);
288.
ad.show();
289.
return
false
;
290.
}
291.
else
292.
{
293.
Toast.makeText(UpdateActivity.
this
,
"Update Data Successfully"
, Toast.LENGTH_SHORT).show();
294.
}
295.
296.
297.
return
true
;
298.
}
299.
300.
301.
@Override
302.
public
boolean
onCreateOptionsMenu(Menu menu) {
303.
getMenuInflater().inflate(R.menu.activity_main, menu);
304.
return
true
;
305.
}
306.
307.
}