从Android应用程序调用asp.net Web Service

时间:2014-05-08 13:35:05

标签: android asp.net web-services

我尝试调用asp.net Web Service的add方法,该方法将两个整数作为参数并返回它们的总和。 Web服务已部署并在本地IIS服务器中运行。

但是在Android模拟器中执行apk时,它会抛出以下错误。

05-08 18:14:53.668:E / AndroidRuntime(662):致命异常:Thread-81

05-08 18:14:53.668:E / AndroidRuntime(662):java.lang.NoClassDefFoundError:org.ksoap2.serialization.SoapObject

05-08 18:14:53.668:E / AndroidRuntime(662):at com.example.adddemo.CallSoap.Add(CallSoap.java:23)

05-08 18:14:53.668:E / AndroidRuntime(662):at com.example.adddemo.Caller.run(Caller.java:14)

我在这里缺少什么?我已经导入了ksoap2库。如果我在发送请求时遗漏了任何需要额外添加的内容,或者我的本地IIS有任何问题,请告诉我。

我的完整代码如下:

activity_main.xml有两个编辑文本和一个按钮。在按钮上单击ActivityMain.java中的方法应该被调用。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.adddemo.MainActivity$PlaceholderFragment" >

<EditText
    android:id="@+id/textParam1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:hint="@string/param1"/>

<EditText
    android:id="@+id/textParam2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@id/textParam1"
    android:layout_below="@id/textParam1"
    android:layout_marginTop="27dp"
    android:hint="@string/param2"/>

<Button
    android:id="@+id/btnAdd"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textParam2"
    android:layout_below="@+id/textParam2"
    android:layout_marginLeft="89dp"
    android:layout_marginTop="46dp"
    android:text="@string/Button"
    android:onClick="onClick" />

</RelativeLayout>

ActivityMain.java代码如下:

package com.example.adddemo;

import android.app.AlertDialog;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


public class MainActivity extends ActionBarActivity 
{

public static String rslt = "";

@Override
protected void onCreate(Bundle savedInstanceState) 
{   
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button btn = (Button)findViewById(R.id.btnAdd);

}

public void onClick(View view)
{
    final  AlertDialog ad=new AlertDialog.Builder(this).create();

    try
    { 
        EditText ed1=(EditText)findViewById(R.id.textParam1);
        EditText ed2=(EditText)findViewById(R.id.textParam2); 
        int value1=Integer.parseInt(ed1.getText().toString());
        int value2=Integer.parseInt(ed2.getText().toString());
        rslt="START";

        Caller call = new Caller(); 
        call.param1=value1;
        call.param2=value2; 
        //call.ad=ad;
        call.join(); 
        call.start();
        while(rslt=="START") 
        {
            try 
            {
                Thread.sleep(10); 
            }
            catch(Exception ex) 
            {
                rslt = ex.getMessage();
            }
        }
        ad.setTitle("RESULT OF ADD of "+value1+" and "+value2);
        ad.setMessage(rslt); 
    }
    catch(Exception ex) 
    {
        ad.setTitle("Error!"); ad.setMessage(ex.toString());
    }
    ad.show();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) 
{

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

Caller.java类代码如下:
它将调用CallSoap类,它将负责调用asp.net webservice

package com.example.adddemo;

public class Caller extends Thread
{
public CallSoap cs;
public int param1, param2; 

public void run()
{
    try
    {
        cs = new CallSoap();
        String resp=cs.Add(param1, param2);
        MainActivity.rslt = resp;
    }
    catch(Exception ex)
    {
        MainActivity.rslt=ex.toString();
    }    
}
}

CallSoap类代码如下 它将处理创建http请求,将数据绑定到soap信封并使用http请求传递它

package com.example.adddemo;
import org.ksoap2.SoapEnvelope; 
import org.ksoap2.serialization.PropertyInfo; 
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

public class CallSoap 
{
public final String SOAP_ACTION = "http://tempuri.org/Add";
public  final String OPERATION_NAME = "Add"; 
public  final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";
public  final String SOAP_ADDRESS = "http://192.168.8.1/AddDemo.asmx";
    //where 192.168.8.1 is my system ip
    //AddDemo.asmx is my webservice 

public CallSoap()
{

}

public String Add(int param1, int param2)
{
    SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);
    PropertyInfo pi=new PropertyInfo();

    pi.setName("param1");
    pi.setValue(param1);
    pi.setType(Integer.class);
    request.addProperty(pi);
    pi=new PropertyInfo();
    pi.setName("param2");
    pi.setValue(param2);
    pi.setType(Integer.class);
    request.addProperty(pi);

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

    envelope.setOutputSoapObject(request);

    HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
    Object response=null;
    try
    { 
        httpTransport.call(SOAP_ACTION, envelope);
        response = envelope.getResponse();
    }
    catch (Exception exception)
    {
        response=exception.toString();
    }
    return response.toString();
}

}

2 个答案:

答案 0 :(得分:0)

来自eclipse的

选择文件 - &gt;新的 - &gt;其他 - &gt;从现有代码的android项目然后选择ksoap2源代码

然后去ksoap2属性 - &gt; android然后检查是库它必须是真的

然后在您的项目属性中 - &gt; android在库部分添加ksoap2

答案 1 :(得分:0)

嘿所有我弄清楚为什么引起异常。上面的错误说 java.lang.NoClassDefFoundError:org.ksoap2.serialization.SoapObject。 找不到所需的Ksoap2依赖类。

解决方案:
1.)在eclipse goto项目中 - &gt;属性 - &gt; java构建路径 - &gt;订购并导出并检查Ksoap2 jar复选框。这将使其在运行时可用于项目。

对于上述例外情况,这对我很有用。