【问题标题】:Invoking asp.net Web Service from Android application从 Android 应用程序调用 asp.net Web 服务
【发布时间】:2014-05-08 13:35:04
【问题描述】:

我正在尝试调用 asp.net Web 服务的 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 网络服务

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

}

【问题讨论】:

    标签: android asp.net web-services


    【解决方案1】:

    从 eclipse 中选择文件 -> 新建 -> 其他 -> 从现有代码中选择 android 项目,然后选择 ksoap2 源代码

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

    然后在你的项目属性 -> android 库部分添加 ksoap2

    【讨论】:

      【解决方案2】:

      嘿,我知道为什么会导致异常。上面的错误说 java.lang.NoClassDefFoundError: org.ksoap2.serialization.SoapObject. 找不到所需的 Ksoap2 依赖类。

      解决方案:
      1.) 在 Eclipse 中转到项目 -> 属性 -> java 构建路径 -> 订购和导出并选中 Ksoap2 jar 复选框。这将使其在运行时可用于项目。

      对于上述例外情况,这对我来说非常有效。

      【讨论】:

        猜你喜欢
        • 2014-02-24
        • 1970-01-01
        • 2018-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-01
        • 2011-04-04
        • 1970-01-01
        相关资源
        最近更新 更多