我需要在android設備中調用肥皂web服務。我一直在閱讀大量的文章在stackoverflow和其他網頁上,看視頻......但我已經嘗試過所有的東西,我不能讓它在我的android設備上工作,我不能在模擬器上測試,因爲我的電腦無法處理它們中的任何一個,所以我不知道錯誤是在代碼上還是它是我的Android設備的問題。在android中調用soap web服務
佈局xml只是一個EditText,一個Button和一個TextView。
在這個環節你可以看到請求XML我需要發送到web服務(我應該使用SOAP 1.1或SOAP 1.2?) http://www.webservicex.net/globalweather.asmx?op=GetCitiesByCountry
這是我實際的代碼,我試過很多其他方法,而且他們都沒有爲我工作。任何幫助? (URL,命名空間,soap_action和METHOD_NAME值是好,是不是?)
package com.example.doazdoas.webservice_prueba;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.os.AsyncTask;
import android.widget.Toast;
import static android.content.ContentValues.TAG;
public class MainActivity extends Activity{
private TextView textResult;
private Button buttonSend;
String NAMESPACE = "http://www.webserviceX.NET/";
String METHOD_NAME = "GetCitiesByCountry";
String SOAP_ACTION = NAMESPACE + METHOD_NAME;
String URL = "http://www.webservicex.net/globalweather.asmx?WSDL";
private Object resultsRequestSOAP = null;
HttpTransportSE androidHttpTransport;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textResult = (TextView)findViewById(R.id.textResultado);
buttonSend = (Button)findViewById(R.id.buttonEnviar);
//setContentView(tv);
}
public void onClickEnviar(View view){
AsyncCallWS task = new AsyncCallWS();
task.execute();
}
private class AsyncCallWS extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
Log.i(TAG, "onPreExecute");
}
@Override
protected Void doInBackground(Void... params) {
Log.i(TAG, "doInBackground");
sendRequest();
return null;
}
@Override
protected void onPostExecute(Void result) {
Log.i(TAG, "onPostExecute");
Log.d("dump Request: " ,androidHttpTransport.requestDump);
Log.d("dump response: " ,androidHttpTransport.responseDump);
}
}
public void sendRequest(){
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
//SoapObject
request.addProperty("@CountryName", "SPAIN");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
androidHttpTransport = new HttpTransportSE(URL);
try
{
androidHttpTransport.call(SOAP_ACTION, envelope);
resultsRequestSOAP = envelope.getResponse();
String[] results = (String[]) resultsRequestSOAP;
textResult.setText(results[0]);
}
catch (Exception aE)
{
aE.printStackTrace();
}
}
}
嘗試在'NAMESPACE'結尾刪除'/',並更改'SOAP_ACTION = NAMESPACE +「/」+ METHOD_NAME;' – DaveNOTDavid