在安卓中调用肥皂网络服务



我需要在安卓设备中调用肥皂网络服务。我一直在阅读堆栈溢出和其他页面上的很多文章,观看视频......但是我已经尝试了每一个,我无法让它在我的 android 设备中工作,我无法在模拟器上进行测试,因为我的计算机无法处理其中任何一个,所以我不知道错误是否在代码上,或者它是否是我的 android 设备的问题。

布局 xml 只是一个编辑文本、一个按钮和一个文本视图。

在此链接中,您可以看到我需要发送到 Web 服务的请求 xml(我应该使用 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 ();
}
}
}

你可以在doInBackGround中做任何与UI相关的操作,所以把它们移入onPostExecutemethnod。

因为doInBackGround不是 UI 线程。请仔细阅读异步任务文档。无论您从doInBackGround返回的数据是什么,它都会被视为onPostExecute的输入。

因此,请按如下方式更改您的代码,

private class AsyncCallWS extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
Log.i(TAG, "onPreExecute");
}
@Override
protected String[] doInBackground(Void... params) {
Log.i(TAG, "doInBackground");
String[] data = sendRequest();
return data;
}
@Override
protected void onPostExecute(String[] result) {
Log.i(TAG, "onPostExecute");
if(result != null && result.length > 0){
textResult.setText( results[0]);
}
}
}

private String[] 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;
}
catch (Exception aE)
{
aE.printStackTrace ();
}
}

您是否考虑过在不使用库的情况下发出 SOAP 请求?我之前遇到了同样的问题,它让我发现库会让你的工作更加困难,尤其是在更改请求结构时。这是您在不使用库的情况下发出 SOAP 请求的方式: 首先,您需要知道如何使用SOAP UI,这是一个Windows应用程序。您可以在此处导入 wsdl 文件,如果语法正确,那么您将获得一个屏幕,显示 Web 服务的请求正文。您可以输入测试值,您将获得响应结构。此链接将指导您如何使用肥皂UI https://www.soapui.org/soap-and-wsdl/working-with-wsdls.html

现在进入安卓代码:

我们将创建一个名为runTask的类,该类扩展了异步任务并使用http发送请求正文并获取请求响应:

private class runTask extends AsyncTask<String, String, String> {
private String response;
String string = "your string parameter"
String SOAP_ACTION = "your soap action here";
String stringUrl = "http://your_url_here";
//if you experience a problem with url remove the '?wsdl' ending

@Override
protected String doInBackground(String... params) {
try {
//paste your request structure here as the String body(copy it exactly as it is in soap ui)
//assuming that this is your request body

String body = "<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">" +
"<soap:Body>"+
"<GetCitiesByCountryResponse xmlns="http://www.webserviceX.NET">"+
"<GetCitiesByCountryResult>"+string+"</GetCitiesByCountryResult>"+
"</GetCitiesByCountryResponse>"+
"</soap:Body>"+
"</soap:Envelope>";

try {
URL url = new URL(stringUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDefaultUseCaches(false);
conn.setRequestProperty("Accept", "text/xml");
conn.setRequestProperty("SOAPAction", SOAP_ACTION);
//push the request to the server address
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(body);
wr.flush();
//get the server response
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder builder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {

builder.append(line);
response = builder.toString();//this is the response, parse it in onPostExecute
}

} catch (Exception e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}

} catch (Exception e) {
e.printStackTrace();
}
return response;
}
/**
* @see AsyncTask#onPostExecute(Object)
*/
@Override
protected void onPostExecute(String result) {

try {
Toast.makeText(this,"Response "+ result,Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
}

现在只需执行类并观察魔术发生:

runTask runner = new runTask();
runner.execute();

您可以使用 DOM 或 SAX 解析器解析响应以获取所需的值。 请随时要求进一步澄清。

相关内容

  • 没有找到相关文章

最新更新