SOAP Web サービスを使用してAndroidとASP.NETの間で通信する Android アプリケーションを作成しています。そのために KSoap2 を使用しています。そして、「登録コード」とともにいくつかのユーザー情報を取得するアカウントを登録し、WebサービスからASPへの接続に使用される接続Webサービスを使用します。
私の ASP.NET IIS は正常に動作しています。
値がWebサービスに渡されていることを知ってください。.netで値を送信し、AndroidのSQLサーバーに挿入することが可能です。
登録コード
public class Register extends Activity {
EditText edit_username;
EditText edit_displayName;
EditText edit_email;
EditText edit_password;
EditText edit_confirmPassword;
Button btn_register, btn_login;
Button btn_reset;
String user;
String disp, pass, email, conpass;
boolean RegisterStatus;
static boolean errored = false;
ProgressBar webservicePG;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
edit_username = (EditText) findViewById(R.id.edit_username);
edit_displayName = (EditText) findViewById(R.id.edit_name);
edit_email = (EditText) findViewById(R.id.edit_email);
edit_password = (EditText) findViewById(R.id.edit_password);
edit_confirmPassword = (EditText) findViewById(R.id.edit_confirmPassword);
btn_register = (Button) findViewById(R.id.btn_register);
btn_login = (Button) findViewById(R.id.reg_back_btn);
btn_reset = (Button) findViewById(R.id.btn_reset);
btn_reset.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
edit_username.setText("");
edit_displayName.setText("");
edit_email.setText("");
edit_password.setText("");
edit_confirmPassword.setText("");
}
});
btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i = new Intent(getApplicationContext(), Login.class);
startActivity(i);
}
});
btn_register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (edit_username.getText().length() != 0
&& edit_username.getText().toString() != "") {
if (edit_displayName.getText().length() != 0
&& edit_displayName.getText().toString() != "") {
if (edit_email.getText().length() != 0
&& edit_email.getText().toString() != "") {
if (edit_password.getText().length() != 0
&& edit_password.getText().toString() != "") {
if (edit_confirmPassword.getText().length() != 0
&& edit_confirmPassword.getText()
.toString() != "") {
user = edit_username.getText().toString();
disp = edit_displayName.getText()
.toString();
pass = edit_password.getText().toString();
email = edit_email.getText().toString();
conpass = edit_confirmPassword.getText()
.toString();
// statusTV.setText("");
// Create instance for AsyncCallWS
AsyncCallWS task = new AsyncCallWS();
// Call execute
task.execute();
}
// If Password text control is empty
else {
// statusTV.setText("Please enter Password");
Toast.makeText(getApplicationContext(),
"Please enter Conform Password",
Toast.LENGTH_LONG).show();
edit_confirmPassword
.setError("Cannot Be Empty");
}
// If Username text control is empty
} else {
// statusTV.setText("Please enter Username");
Toast.makeText(getApplicationContext(),
"Please enter Password",
Toast.LENGTH_LONG).show();
edit_password.setError("Cannot Be Empty");
}
} else {
// statusTV.setText("Please enter Username");
Toast.makeText(getApplicationContext(),
"Please enter Email Address",
Toast.LENGTH_LONG).show();
edit_email.setError("Cannot Be Empty");
}
} else {
// statusTV.setText("Please enter Username");
Toast.makeText(getApplicationContext(),
"Please enter Dispalayname", Toast.LENGTH_LONG)
.show();
edit_displayName.setError("Cannot Be Empty");
}
} else {
// statusTV.setText("Please enter Username");
Toast.makeText(getApplicationContext(),
"Please enter Username", Toast.LENGTH_LONG).show();
edit_username.setError("Cannot Be Empty");
}
}
});
}
private class AsyncCallWS extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... params) {
// Call Web Method
RegisterStatus = WebService.invokeLoginWS(user, disp, email, pass,
conpass, "AuthenticateUser");
return null;
}
@Override
// Once WebService returns response
protected void onPostExecute(Void result) {
// Make Progress Bar invisible
webservicePG.setVisibility(View.INVISIBLE);
Intent intObj = new Intent(Register.this, Login.class);
// Error status is false
if (!errored) {
// Based on Boolean value returned from WebService
if (RegisterStatus) {
// Navigate to Home Screen
startActivity(intObj);
} else {
// Set Error message
Toast.makeText(getApplicationContext(),
"Register Failed, try again", Toast.LENGTH_LONG)
.show();
}
// Error status is true
} else {
// /statusTV.setText("Error occured in invoking webservice");
Toast.makeText(getApplicationContext(),
"Error occured in invoking webservice",
Toast.LENGTH_LONG).show();
}
// Re-initialize Error Status to False
errored = false;
}
@Override
// Make Progress Bar visible
protected void onPreExecute() {
webservicePG.setVisibility(View.VISIBLE);
}
@Override
protected void onProgressUpdate(Void... values) {
}
}
}
Web サービス コード
public class WebService {
// Namespace of the Webservice - can be found in WSDL
private static String NAMESPACE = "http://tempuri.org/";
// Webservice URL - WSDL File location
private static String URL = "http://192.168.3.3/LoginCheck/Service.asmx";// Make
// sure
// you
// changed
// IP
// address
// SOAP Action URI again Namespace + Web method name
private static String SOAP_ACTION = "http://tempuri.org/";
public static boolean invokeLoginWS(String userName, String dispalyname,
String Email, String password, String conpassWord,
String webMethName) {
boolean loginStatus = false;
// Create request
SoapObject request = new SoapObject(NAMESPACE, webMethName);
// Property which holds input parameters
PropertyInfo unamePI = new PropertyInfo();
PropertyInfo passPI = new PropertyInfo();
// Set Username
unamePI.setName("username");
// Set Value
unamePI.setValue(userName);
// Set dataType
unamePI.setType(String.class);
// Add the property to request object
request.addProperty(unamePI);
passPI.setName("dispalyname");
// Set dataType
passPI.setValue(dispalyname);
// Set dataType
passPI.setType(String.class);// Set Password
passPI.setName("Email");
// Set dataType
passPI.setValue(Email);
// Set dataType
passPI.setType(String.class);
// Add the property to request object
request.addProperty(passPI);
// Set Password
passPI.setName("password");
// Set dataType
passPI.setValue(password);
// Set dataType
passPI.setType(String.class);
// Add the property to request object
request.addProperty(passPI);
// Set dataType
// Set Password
passPI.setName("conpassWord");
passPI.setValue(conpassWord);
// Set dataType
passPI.setType(String.class);
// Add the property to request object
request.addProperty(passPI);
// Create envelope
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
// Set output SOAP object
envelope.setOutputSoapObject(request);
// Create HTTP call object
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
// Invoke web service
androidHttpTransport.call(SOAP_ACTION + webMethName, envelope);
// Get the response
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
// Assign it to boolean variable variable
loginStatus = Boolean.parseBoolean(response.toString());
} catch (Exception e) {
// Assign Error Status true in static variable 'errored'
Login.errored = true;
e.printStackTrace();
}
// Return booleam to calling object
return loginStatus;
}
}