我正在尝试使用.Net webservice。我收到以下异常。
例外: org.xmlpull.v1.XmlPullParserException:expected:START_TAG {http://schemas.xmlsoap.org/soap/envelope/} Envelope(位置:START_TAG @ 2:7 in java.io.InputStreamReader@40d66380)
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import com.example.webserviceactivity.R;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private final String NAMESPACE = "http://tempuri.org/";
private final String URL = "http://10.0.2.2:2351/Service1.asmx";
private final String SOAP_ACTION = "http://tempuri.org/HelloWorld";
private final String METHOD_NAME = "HelloWorld";
private String TAG = "Result";
private static String c;
Button b;
TextView tv;
EditText et;
EditText et2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.tv_result);
//Button to trigger web service invocation
b = (Button) findViewById(R.id.button1);
//Button Click Listener
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//Create instance for AsyncCallWS
AsyncCallWS task = new AsyncCallWS();
//Call execute
task.execute();
//If text control is empty
}
});
}
public void getSum() {
Log.i(TAG, "1");
//Create request
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
//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);
Log.i(TAG, "5");
try {
//Invole web service
androidHttpTransport.call(SOAP_ACTION, envelope);
//Get the response
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
//Assign it to Sum static variable
c = response.toString();
Log.i(TAG, "6");
} catch (Exception e) {
Log.i(TAG, e.toString());
c=e.toString();
}
}
private class AsyncCallWS extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... params) {
Log.i(TAG, "doInBackground");
getSum();
return null;
}
@Override
protected void onPostExecute(Void result) {
Log.i(TAG, "onPostExecute");
tv.setText(c + " ! ");
}
@Override
protected void onPreExecute() {
Log.i(TAG, "onPreExecute");
tv.setText("Calculating...");
}
@Override
protected void onProgressUpdate(Void... values) {
Log.i(TAG, "onProgressUpdate");
}
}
}
答案 0 :(得分:0)
下载最新的jar(ksoap2-android-assembly-3.2.0-jar-with-dependencies.jar)
这是代码
SoapObject request = new SoapObject(Constants.NAMESPACE, Constants.METHOD_NAME_GETPROFILE);
SoapObject userObject=new SoapObject(Constants.NAMESPACE, "user");
userObject.addProperty("userName", username);
userObject.addProperty("password", password);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
// envelope.dotNet = true;
//envelope.avoidExceptionForUnknownProperty=true;
request.addProperty("",userObject);
envelope.setOutputSoapObject(request);
envelope.implicitTypes = true;
envelope.dotNet = true;
HttpTransportSE transport = new HttpTransportSE(Constants.URL);
transport.debug=true;
transport.call(Constants.SOAP_ACTION_GETPROFILE, envelope);
//SoapObject object=(SoapObject)envelope.bodyIn;
System.out.println("Request :"+transport.requestDump);
System.out.println("Response :"+transport.responseDump);
System.out.println("TEST "+envelope.bodyIn);