java - 在 android 中使用 SOAP 本地 Web 服务

标签 java android xml web-services soap

我正在尝试使用 ksoap2 库在 Android 模拟器中使用来自本地 Web 服务的数据,我使用在线 Web 服务测试相同的应用程序 http://www.webservicex.net/New/Home/ServiceDetail/17它有效, WebService 已在 Csharp 上实现,它有一个方法从数据库返回一列,调用 neptuno(西类牙冒险作品),执行时不显示数据,片刻后显示编程的错误消息,我将 localhost 更改为我的本地IP

[WebService(Namespace = "http://testapi.idat/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]

public class Service : System.Web.Services.WebService
{
SqlConnection cn = new SqlConnection("data source=.;initial catalog = neptuno; integrated security=true");

public Service () {
    //InitializeComponent(); 
}

[WebMethod]
public string HelloWorld() {
    return "Hello World";
}

[WebMethod]
public DataSet mostrar(){

    DataSet ds = new DataSet();
    cn.Open();
    String sql = "select idProducto from productos";
    SqlDataAdapter da = new SqlDataAdapter(sql,cn);
    da.Fill(ds);
    return ds;    
}    
}

服务正常工作并返回 IDProduct 列,问题是使用方法调用,我已经在 list 上添加了权限,布局只是一个用于错误消息的 TextView 和用于显示数据的 edittext 多行

package com.example.webservice;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;
import android.os.AsyncTask;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

public class MainActivity extends Activity {

EditText resultMultiline;
TextView message;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    resultMultiline = (EditText)findViewById(R.id.editText1);
    message = (TextView) findViewById(R.id.textView);
    CallWebservice webservice = new CallWebservice();
    webservice.execute();
}

public class CallWebservice extends AsyncTask<Integer,Integer,Boolean>{

    String resultText = "";

    protected Boolean doInBackground(Integer... params){

        boolean result = true;

        final String SOAP_ACTION = "http://testapi.idat/mostrar";
        final String NAMESPACE = "http://testapi.idat/";
        final String METHOD_NAME = "mostrar";
        final String URL = "http://192.168.1.45:51582/Webservice/Service.asmx?WSDL";

        HttpTransportSE transport = new HttpTransportSE(URL);
        SoapObject request = new SoapObject(NAMESPACE,METHOD_NAME);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);

        try {
            transport.call(SOAP_ACTION,envelope);
            SoapPrimitive respSoap = (SoapPrimitive)envelope.getResponse();

            resultText = respSoap.toString();

        }catch (Exception e){
            result = false;
            Log.d("Debug", e.getMessage().toString());
        }
        return  result;
    }

    protected void onPostExecute(Boolean result){
        if (result){
            resultMultiline.setText(resultText);
            Log.d("Debug","Web service works");
        }else{
            message.setText("ERROR");
        }
    }
  }
}

最佳答案

Use the Following function it is working in my case this is normal Login soap call you need to add internet permission in your menifest

  /*
* Vishal Mokal 05-MAR-2015
* soapCALL() function returns String Array.
* Make A sope call and returns response. 
* if responce is success full then user will get responce array 
* first element status = 1 if syccessfull 0 = if any exception of faliur 
* second element = response srtring if successful or error message.
* */
public String[] soapCALL(RequestDetails requestDetails, String wsdlUserName, String wsdlPassword , String headerName) {

    String url = requestDetails.getUrl().toString();
    String nameSpace = requestDetails.getNameSpace().toString();
    String methodName = requestDetails.getMethodName().toString();
    String soapAction = requestDetails.getSoapAction().toString();

    String[] responses = new String[2];

    Element[] header = new Element[1];

   // header[0] = new Element().createElement(nameSpace, "AuthenticationHeader");
    header[0] = new Element().createElement(nameSpace, headerName);

    try {
        Element UserName = new Element().createElement(nameSpace, "UserName");
        UserName.addChild(Node.TEXT, wsdlUserName);
        header[0].addChild(Node.ELEMENT, UserName);
        Element Password = new Element().createElement(nameSpace, "Password");
        Password.addChild(Node.TEXT, wsdlPassword);
        header[0].addChild(Node.ELEMENT, Password);
        SoapObject request = requestDetails.getSoapObject();

        SoapSerializationEnvelope soapSerilizationEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        soapSerilizationEnvelope.dotNet = true;
        soapSerilizationEnvelope.headerOut = header;
        soapSerilizationEnvelope.setOutputSoapObject(request);
        Object env = soapSerilizationEnvelope.bodyOut;
        HttpTransportSE httptransport = new HttpTransportSE(url);
        httptransport.call(soapAction, soapSerilizationEnvelope);
        SoapPrimitive response = (SoapPrimitive) soapSerilizationEnvelope.getResponse();

        responses[0] = "1";
        responses[1] = response.toString();


        Log.d("Respons", response.toString());
        return responses;


    } 

    catch (SocketTimeoutException e)
    {
        responses[0] = "0";
        responses[1] = "Sorry!Unable To Connect Server Please Check Your Internet Connection Or Try After Some Time.";
        return responses;

    }
    catch (SocketException e)
    {
        responses[0] = "0";
        responses[1] = "Sorry!Unable To Connect Server Please Try After Some Time.";
        return responses;

    }        
    catch (Exception e) {
        responses[0] = "0";
        responses[1] = "Sorry!Unable To Connect Server Please Try After Some Time.";
        return responses;

    }  
}

Following is the structure for the Requestdetail class

public class RequestDetails {

private String nameSpace;
private String url;
private String methodName;
private String SoapAction;
private SoapObject soapObject;

public RequestDetails(String nameSpace, String url, String methodName, String soapAction) {
    this.nameSpace = nameSpace;
    this.url = url;
    this.methodName = methodName;
    SoapAction = soapAction;
}

public String getNameSpace() {
    return nameSpace;
}

public String getUrl() {
    return url;
}

public String getMethodName() {
    return methodName;
}

public String getSoapAction() {
    return SoapAction;
}


public SoapObject getSoapObject() {
    return soapObject;
}

public void setSoapObject(SoapObject soapObject) {
    this.soapObject = soapObject;
}

}

关于java - 在 android 中使用 SOAP 本地 Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37064581/

相关文章:

java - 布隆过滤器用于在 O(n) 内从整数流中删除重复项

Java从paintComponent()外部绘制形状到bufferedImage?

android - 如何阻止 webview 中的任何广告,例如浏览器中的 adblock 或 adblock plus?

android - 如何完全过滤频率范围android

android - TabListener、ActionBarSherlock Fragment 和 ListFragment、PageAdapter 的混合?一团糟

java - 在一个对象上设置背景颜色会更改另一个对象

java - 为什么 JAR 文件中不包含依赖项?

java - map 应用程序在模拟器上强制退出

java - 当涉及命名空间时从 SAX 属性获取值

java - Android 按钮不起作用?