我的Java代码:
import java.net.URI;
import java.util.Vector;
import org.xmlrpc.android.XMLRPCClient;
import org.xmlrpc.android.XMLRPCException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
String txtName, txtEmail, txtPassword, txtConfPassword;
TextView nameView, emailView, passView, confPassView;
private XMLRPCClient client;
private URI uri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class RegistrarionTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
// TODO Auto-generated method stub
/*
* XmlRpcClient client = new XmlRpcClient(
* "http://www.danica.com.ar/recetario/recetario.wsdl");
* Vector<String> vector = new Vector<String>();
* vector.addElement(txtName); vector.addElement(txtEmail);
* vector.addElement(txtPassword); Object result = null; try {
* result = client.execute("Registration", vector);
*
* } catch (Exception e) { // TODO Auto-generated catch block
* e.printStackTrace(); }
*/
uri = URI
.create("http://www.danica.com.ar/recetario/recetario.wsdl");
client = new XMLRPCClient(uri);
String result = null;
try {
// result = (String) client.call("Registration");
result = (String) client.call("Registration", txtName,
txtEmail, txtPassword);
} catch (XMLRPCException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(String result) {
if (result != null) {
}
}
}
public void onRegisterClick(View v) {
nameView = (TextView) findViewById(R.id.txtName);
nameView.requestFocus();
emailView = (TextView) findViewById(R.id.txtEmail);
passView = (TextView) findViewById(R.id.txtPass);
confPassView = (TextView) findViewById(R.id.txtConfPass);
txtName = nameView.getText().toString().trim();
txtEmail = emailView.getText().toString().trim();
txtPassword = passView.getText().toString().trim();
txtConfPassword = confPassView.getText().toString().trim();
if (txtName.equals("")) {
nameView.setError("This field is required!");
nameView.requestFocus();
return;
}
if (txtEmail.equals("")) {
emailView.setError("This field is required!");
emailView.requestFocus();
return;
}
if (!txtEmail.contains("@")) {
emailView.setError("Not a valid Email!");
emailView.requestFocus();
return;
}
if (txtPassword.equals("")) {
passView.setError("This field is required!");
passView.requestFocus();
return;
}
if (!txtPassword.equals(txtConfPassword)) {
confPassView.setError("Not a matching password!");
confPassView.requestFocus();
return;
}
new RegistrarionTask().execute();
}
}
和XML布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="22dp"
android:ems="10"
android:hint="Name"
android:inputType="textPersonName" />
<EditText
android:id="@+id/txtEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Email"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/txtPass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword" />
<EditText
android:id="@+id/txtConfPass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Confirm Password"
android:inputType="textPassword" />
<Button
android:id="@+id/btnRegister"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="54dp"
android:layout_marginTop="44dp"
android:onClick="onRegisterClick"
android:text="Register" />
但是当我运行这段代码时,它会给出异常:
03-07 19:30:59.537: W/System.err(1016): org.xmlpull.v1.XmlPullParserException: expected: START_TAG {null}methodResponse (position:START_TAG <definitions name='Recetario' targetNamespace='http://danica.com.ar/recetario' xmlns:tns='http://danica.com.ar/recetario' xmlns:xsd1='http://danica.com.ar/recetarioNamespace' xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap-enc='http://schemas.xmlsoap.org/soap/encoding/' xmlns='http://schemas.xmlsoap.org/wsdl/'>@9:43 in java.io.InputStreamReader@4122e1f8)
03-07 19:30:59.537: W/System.err(1016): at org.kxml2.io.KXmlParser.require(KXmlParser.java:2046)
03-07 19:30:59.537: W/System.err(1016): at org.xmlrpc.android.XMLRPCClient.callEx(XMLRPCClient.java:200)
03-07 19:30:59.537: W/System.err(1016): at org.xmlrpc.android.XMLRPCClient.call(XMLRPCClient.java:314)
03-07 19:30:59.537: W/System.err(1016): at com.example.kousikchatterjee.MainActivity$RegistrarionTask.doInBackground(MainActivity.java:59)
03-07 19:30:59.537: W/System.err(1016): at com.example.kousikchatterjee.MainActivity$RegistrarionTask.doInBackground(MainActivity.java:1)
03-07 19:30:59.547: W/System.err(1016): at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-07 19:30:59.547: W/System.err(1016): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-07 19:30:59.547: W/System.err(1016): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-07 19:30:59.547: W/System.err(1016): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
03-07 19:30:59.547: W/System.err(1016): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-07 19:30:59.547: W/System.err(1016): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-07 19:30:59.547: W/System.err(1016): at java.lang.Thread.run(Thread.java:856)
03-07 19:30:59.557: W/System.err(1016): org.xmlrpc.android.XMLRPCException: org.xmlpull.v1.XmlPullParserException: expected: START_TAG {null}methodResponse (position:START_TAG <definitions name='Recetario' targetNamespace='http://danica.com.ar/recetario' xmlns:tns='http://danica.com.ar/recetario' xmlns:xsd1='http://danica.com.ar/recetarioNamespace' xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap-enc='http://schemas.xmlsoap.org/soap/encoding/' xmlns='http://schemas.xmlsoap.org/wsdl/'>@9:43 in java.io.InputStreamReader@4122e1f8)
03-07 19:30:59.557: W/System.err(1016): at org.xmlrpc.android.XMLRPCClient.callEx(XMLRPCClient.java:237)
03-07 19:30:59.557: W/System.err(1016): at org.xmlrpc.android.XMLRPCClient.call(XMLRPCClient.java:314)
03-07 19:30:59.579: W/System.err(1016): at com.example.kousikchatterjee.MainActivity$RegistrarionTask.doInBackground(MainActivity.java:59)
03-07 19:30:59.579: W/System.err(1016): at com.example.kousikchatterjee.MainActivity$RegistrarionTask.doInBackground(MainActivity.java:1)
03-07 19:30:59.579: W/System.err(1016): at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-07 19:30:59.579: W/System.err(1016): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-07 19:30:59.587: W/System.err(1016): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-07 19:30:59.587: W/System.err(1016): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
03-07 19:30:59.587: W/System.err(1016): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-07 19:30:59.597: W/System.err(1016): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-07 19:30:59.597: W/System.err(1016): at java.lang.Thread.run(Thread.java:856)
03-07 19:30:59.607: W/System.err(1016): Caused by: org.xmlpull.v1.XmlPullParserException: expected: START_TAG {null}methodResponse (position:START_TAG <definitions name='Recetario' targetNamespace='http://danica.com.ar/recetario' xmlns:tns='http://danica.com.ar/recetario' xmlns:xsd1='http://danica.com.ar/recetarioNamespace' xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap-enc='http://schemas.xmlsoap.org/soap/encoding/' xmlns='http://schemas.xmlsoap.org/wsdl/'>@9:43 in java.io.InputStreamReader@4122e1f8)
03-07 19:30:59.607: W/System.err(1016): at org.kxml2.io.KXmlParser.require(KXmlParser.java:2046)
03-07 19:30:59.617: W/System.err(1016): at org.xmlrpc.android.XMLRPCClient.callEx(XMLRPCClient.java:200)
03-07 19:30:59.617: W/System.err(1016): ... 10 more
请根据我的代码向我提供解决方案。提前谢谢。
答案 0 :(得分:0)
过去七天我受苦了。出于这个原因,我发布了这个问题,当时我发布的帖子我不知道WSDL和SOAP。以下是我的解决答案:
代码:
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();
}
}
});
}
}
对于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;
}
}
}
用户界面布局
<?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>
不要忘记在INTERNET
中授予AndroidManifest.xml
权限。