在android中使用SOAP本地Web服务

时间:2016-05-06 05:02:51

标签: java android xml web-services soap

我正在尝试使用kso​​ap2库在Android模拟器中使用本地Web服务中的数据,我使用在线Web服务http://www.webservicex.net/New/Home/ServiceDetail/17测试相同的应用程序并且它有效, webService已经在Csharp上制作了,它有一个从DB返回一个列的方法,调用neptuno(西班牙冒险作品),当执行不显示数据并且片刻之后显示编程的错误消息时,我更改了localhost for我的本地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,问题是用方法调用,我已经在manifest上添加了权限,而布局只是一个textview for error message和edittext multiline for show data

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");
        }
    }
  }
}

1 个答案:

答案 0 :(得分:1)

  

在我的情况下使用以下功能这是正常的登录肥皂呼叫你需要在你的清单中添加互联网权限

  /*
* 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;

    }  
}
  

以下是Requestdetail类的结构

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;
}

}