I suffered from last seven days. For this reason I have posted the question, when I have posted at that time I have no idea about WSDL and SOAP . Here is the solve answer by me :
Code:
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class RecetarionDanica extends Activity
{
/** Called when the activity is first created. */
private static String SOAP_ACTION = "http://danica.com.ar/recetario/Registration";
//private static String SOAP_ACTION2 = "http://www.w3schools.com/webservices/CelsiusToFahrenheit";
private static String NAMESPACE = "http://danica.com.ar/recetario";
private static String METHOD_NAME = "Registration";
//private static String METHOD_NAME2 = "CelsiusToFahrenheit";
private static String URL = "http://danica.com.ar/cgi-bin/soapserver.pl";
Button btnReg;
EditText fullName,emailId,password,confirmPwd;
String result="";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.registration_main);
btnReg = (Button)findViewById(R.id.regId);
fullName = (EditText)findViewById(R.id.name);
emailId = (EditText)findViewById(R.id.emailId);
password = (EditText)findViewById(R.id.pwd);
confirmPwd = (EditText)findViewById(R.id.pwd1);
btnReg.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//Initialize soap request + add parameters
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
Log.d("request", request.toString());
//Use this to add parameters
User user = new User();
user.setEmail(""+emailId.getText().toString().trim());
user.setFirstName(""+fullName.getText().toString().trim());
user.setPassword(""+password.getText().toString().trim());
user.setGplusId(" ");//(""+fullName.getText().toString().trim());
user.setTweet(" ");//(""+fullName.getText().toString().trim());
user.setLastName(" ");//(""+fullName.getText().toString().trim());
user.setFacebookId(" ");//(""+fullName.getText().toString().trim());
request.addProperty("user", user);
// request.addProperty("email",emailId.getText().toString().trim());
// request.addProperty("firstName",fullName.getText().toString().trim());
// request.addProperty("password",password.getText().toString().trim());
Log.d("request 2", request.toString());
//Declare the version of the SOAP request
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
//envelope.addMapping(NAMESPACE, "user", new User.getClass());
Log.d("envelope", envelope.toString());
try {
Log.d("TRY Block", "TRY");
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
Log.d("androidHttpTransport", androidHttpTransport.toString());
//this is the actual part that will call the webservice
androidHttpTransport.call(SOAP_ACTION, envelope);
Log.d("---Block---", "Block 2");
SoapObject result = null;
if (envelope.bodyIn instanceof SoapFault) {
String str= ((SoapFault) envelope.bodyIn).faultstring;
Log.i("Fault", str);
} else {
// Get the SoapResult from the envelope body.
result = (SoapObject)envelope.bodyIn;
Log.d("WS", String.valueOf(result));
}
if(result != null)
{
//Get the first property and change the label text
Log.d("If result not null", String.valueOf(result));
}
else
{
Toast.makeText(getApplicationContext(), "No Response",Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
For complexType WSDL
import java.util.Hashtable;
import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
public class User implements KvmSerializable {
private String email;
private String firstName;
private String password;
private String gplusId ;
private String tweeterId ;
private String lastName;
private String facebookId;
public User() {}
public User(String email, String firstName, String password, String gplus, String tweet, String lastName, String facebook) {
this.email = email;
this.firstName = firstName;
this.password = password;
this.gplusId = gplus;
this.tweeterId = tweet;
this.lastName = lastName;
this.facebookId = facebook;
}
public User(String email, String firstName, String password) {
this.email = email;
this.firstName = firstName;
this.password = password;
}
public void setEmail(String email) { this.email = email; }
public void setFirstName(String firstName) { this.firstName = firstName; }
public void setPassword(String password) { this.password = password; }
public void setGplusId(String gplusId) { this.gplusId = gplusId; }
public void setTweet(String tweeterId) { this.tweeterId = tweeterId; }
public void setLastName(String lastName) { this.lastName = lastName; }
public void setFacebookId(String facebookId) { this.facebookId = facebookId; }
public String getEmail() { return email;}
public String getFirstName() { return firstName;}
public String getPassword() { return password;}
public String getGplusId() { return gplusId;}
public String getTweet() { return tweeterId;}
public String getLastName() { return lastName;}
public String getFacebookId() { return facebookId;}
public Object getProperty(int arg0) {
switch(arg0) {
case 0:
return email;
case 1:
return firstName;
case 2:
return password;
case 3:
return gplusId;
case 4:
return tweeterId;
case 5:
return lastName;
case 6:
return facebookId;
}
return null;
}
public int getPropertyCount() {
return 7;
}
public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo propertyInfo) {
switch(index)
{
case 0:
propertyInfo.name = "email";
propertyInfo.type = PropertyInfo.STRING_CLASS;
break;
case 1:
propertyInfo.name = "firstName";
propertyInfo.type = PropertyInfo.STRING_CLASS;
break;
case 2:
propertyInfo.name = "password";
propertyInfo.type = PropertyInfo.STRING_CLASS;
break;
case 3:
propertyInfo.name = "gplusId";
propertyInfo.type = PropertyInfo.STRING_CLASS;
break;
case 4:
propertyInfo.name = "tweeterId";
propertyInfo.type = PropertyInfo.STRING_CLASS;
break;
case 5:
propertyInfo.name = "lastName";
propertyInfo.type = PropertyInfo.STRING_CLASS;
break;
case 6:
propertyInfo.name = "facebookId";
propertyInfo.type = PropertyInfo.STRING_CLASS;
break;
default:
break;
}
}
public void setProperty(int index, Object value) {
switch(index) {
case 0:
this.email = value.toString();
break;
case 1:
this.firstName = value.toString();
break;
case 2:
this.password = value.toString();
break;
case 3:
this.gplusId = value.toString();
break;
case 4:
this.tweeterId = value.toString();
break;
case 5:
this.lastName = value.toString();
break;
case 6:
this.facebookId = value.toString();
break;
default:
break;
}
}
}
UI Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Full Name"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/emailId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Email id"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Password"
android:inputType="textPassword" />
<EditText
android:id="@+id/pwd1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Confirm Password"
android:inputType="textPassword" />
<Button
android:id="@+id/regId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register" />
</LinearLayout>
Don't forget to give permission INTERNET
in AndroidManifest.xml
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…