繁体   English   中英

使用kso​​ap2在Android中调用.net Web服务

[英]calling .net web service with android using ksoap2

我是android编程的新手(第一个项目),我需要帮助使我的代码正常工作...好的,所以我有一种方法可以调用webservice并从那里获取字符串。 它正在avd上工作(2.2版)

这是代码:

public void NewCall(int Position)
{
    String SOAP_ACTION = "http://tempuri.org/";
    String NAMESPACE = "http://tempuri.org/";
    String METHOD_NAME = "";
    String URL = "http://test.com/Service1.asmx?WSDL";

    if(Position == 0)
    {
        SOAP_ACTION += "Method1";
        METHOD_NAME = "Method1";
    }
    else if(Position == 1)
    {
        SOAP_ACTION += "Method2";
        METHOD_NAME = "Method2";
    }
    else if(Position == 2)
    {
        SOAP_ACTION += "Method3";
        METHOD_NAME = "Method3";
    }
    else if(Position == 3)
    {
        SOAP_ACTION += "Method4";
        METHOD_NAME = "Method4";
    }

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

    envelope.setOutputSoapObject(request);
    envelope.dotNet = true;

    try 
    {
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        androidHttpTransport.call(SOAP_ACTION, envelope);
        SoapObject result = (SoapObject)envelope.bodyIn;

        if(result != null)
        {
            String textRez = result.getProperty(0).toString();

            addRow(textRez); //method for inserting new row in the database

        }
    } 
    catch (Exception e) 
    {
        e.printStackTrace();
    }
}

但是,正如我读到的那样,您无法在android 3.0+的“主线程”中执行此操作,而作为一种解决方案,我看到可以使用AsyncClass 关于这一点,我试图将发现的示例与代码结合起来,但是它还没有给我结果。我认为我需要将Position解析为AssyncClass的Integer,然后执行doInBackground,然后我需要以字符串形式获取结果。 我试图编写类似的内容,但没有成功。

因此,如果你们中的某人可以帮助/指导我解决这个问题,我将不胜感激。

谢谢 。

编辑:这就是我尝试使用AsyncClass的方法:

private class wsGet extends AsyncTask<String, Void, String>
{

    @Override
    protected String doInBackground(String... params) {
    int Position = Integer.parseInt(params[0]); 
    String SOAP_ACTION = "http://tempuri.org/";
    String NAMESPACE = "http://tempuri.org/";
    String METHOD_NAME = "";
    String URL = "http://test.com/Service1.asmx?WSDL";


    if(Position == 0)
    {
        SOAP_ACTION += "Method1";
        METHOD_NAME = "Method1";
    }
    else if(Position == 1)
    {
        SOAP_ACTION += "Method2";
        METHOD_NAME = "Method2";
    }
    else if(Position == 2)
    {
        SOAP_ACTION += "Method3";
        METHOD_NAME = "Method3";
    }
    else if(Position == 3)
    {
        SOAP_ACTION += "Method4";
        METHOD_NAME = "Method4";
    }

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

        envelope.setOutputSoapObject(request);
        envelope.dotNet = true;

        try 
        {
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            androidHttpTransport.call(SOAP_ACTION, envelope);
            SoapObject result = (SoapObject)envelope.bodyIn;

            if(result != null)
            {
                tekstRez = result.getProperty(0).toString();

            }
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }

        return tekstRez;
    }

然后,我尝试使用以下方法进行调用:

public void NewTenders(int Pos)
{
    String Position = String.valueOf(Pos);

    String[] params= {Position}; //to pass to doInBackground

    //Pass your args array and the current activity to the AsyncTask    

    String tekst = "";
    try {
        tekst = new wsGet().execute(params).get();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }addRow(tekst);

不过,它对我没有用...

从android 3.0+开始,您无法从主线程调用Web服务。 将整个代码写在DoInBackgroundAsyncClass

AsyncClass无法将值返回给调用类。 return值将值传递给onPostExecute 在AsyncClass中使用以下功能

protected void onPostExecute(String result) 
{
//from here you have to pass your value to your main class
}

或者,您可以仅将这一行添加到您正在调用网络服务的活动中

    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }

就在setContentView()之后,这应该可以解决问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM