0
我是android中的新用戶。我想用肥皂webservice工作。我發現非常的例子,但他們不適合我:-(如何在android中使用肥皂web服務工作
工作,這是我的web服務的URL: http://parsnerkh.com/webservice/server.wsdl
和梅索德的名字是:貓
和輸出是:CAT1,CAT2, ...
這是一個測試此WebService用PHP: http://parsnerkh.com/webservice/test-cats.php
請幫我
我用這個代碼:
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 = "urn:AppStore";
private final String URL = "http://parsnerkh.com/webservice/helloserverwsdl.php";
private final String SOAP_ACTION = "action:urn:AppStore/AppStorePortType/CatsRequest";
private final String METHOD_NAME = "Cats";
private String TAG = "PGGURU";
private static String celcius;
private static String fahren;
Button b;
TextView tv;
EditText et;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Celcius Edit Control
et = (EditText) findViewById(R.id.editText1);
//Fahrenheit Text control
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) {
//Check if Celcius text control is not empty
if (et.getText().length() != 0 && et.getText().toString() != "") {
//Get the text control value
celcius = et.getText().toString();
//Create instance for AsyncCallWS
AsyncCallWS task = new AsyncCallWS();
//Call execute
task.execute();
//If text control is empty
} else {
tv.setText("Please enter Celcius");
}
}
});
}
public void getFahrenheit(String celsius) {
//Create request
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
//Property which holds input parameters
/*PropertyInfo celsiusPI = new PropertyInfo();
//Set Name
celsiusPI.setName("Celsius");
//Set Value
celsiusPI.setValue(celsius);
//Set dataType
celsiusPI.setType(double.class);
//Add the property to request object
request.addProperty(celsiusPI);*/
//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 {
//Invole web service
androidHttpTransport.call(SOAP_ACTION, envelope);
//Get the response
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
//Assign it to fahren static variable
fahren = response.toString();
} catch (Exception e) {
Log.e("ads","ad");
e.printStackTrace();
}
}
private class AsyncCallWS extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... params) {
Log.i(TAG, "doInBackground");
getFahrenheit(celcius);
return null;
}
@Override
protected void onPostExecute(Void result) {
Log.i(TAG, "onPostExecute");
tv.setText(fahren + "° F");
}
@Override
protected void onPreExecute() {
Log.i(TAG, "onPreExecute");
tv.setText("Calculating...");
}
@Override
protected void onProgressUpdate(Void... values) {
Log.i(TAG, "onProgressUpdate");
}
}
和我recive這個錯誤:
10-08 22:09:24.536: W/System.err(21468): org.xmlpull.v1.XmlPullParserException: expected: END_TAG {http://schemas.xmlsoap.org/soap/envelope/}Body (position:END_TAG </{http://schemas.xmlsoap.org/soap/envelope/}SOAP-ENV:Fault>@2:209 in [email protected])
10-08 22:09:24.546: W/System.err(21468): at org.kxml2.io.KXmlParser.require(KXmlParser.java:2046)
10-08 22:09:24.546: W/System.err(21468): at org.ksoap2.SoapEnvelope.parse(SoapEnvelope.java:138)
10-08 22:09:24.546: W/System.err(21468): at org.ksoap2.transport.Transport.parseResponse(Transport.java:63)
10-08 22:09:24.556: W/System.err(21468): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:100)
10-08 22:09:24.556: W/System.err(21468): at com.prgguru.android.MainActivity.getFahrenheit(MainActivity.java:85)
10-08 22:09:24.556: W/System.err(21468): at com.prgguru.android.MainActivity$AsyncCallWS.doInBackground(MainActivity.java:101)
10-08 22:09:24.556: W/System.err(21468): at com.prgguru.android.MainActivity$AsyncCallWS.doInBackground(MainActivity.java:1)
10-08 22:09:24.556: W/System.err(21468): at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-08 22:09:24.556: W/System.err(21468): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
10-08 22:09:24.556: W/System.err(21468): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-08 22:09:24.556: W/System.err(21468): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
10-08 22:09:24.556: W/System.err(21468): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
10-08 22:09:24.556: W/System.err(21468): at java.lang.Thread.run(Thread.java:856)
sry。我不能使用它。你能給我一個真實的代碼嗎?我想要它。 pleaseee – user3040697 2014-10-08 16:51:12
你不能在你的項目中使用Ksoap lib或者你無法訪問鏈接? – 2014-10-08 17:07:08
我添加這個庫,但我不能用這個工作。我收到一個錯誤。我不khonw什麼是名稱空間和肥皂行動和...你能推薦一個代碼,與我的web服務工作? – user3040697 2014-10-08 18:24:29