簡體   English   中英

從Android應用程序調用asp.net Web服務

[英]Invoking asp.net Web Service from Android application

我試圖調用asp.net Web服務的add方法,該方法將兩個整數作為參數並返回它們的總和。 該Web服務已部署並正在本地IIS服務器中運行。

但是在android模擬器中執行apk時,它拋出了以下錯誤。

我在這里想念什么? 我已經導入了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 Web服務

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

}

從eclipse中選擇文件->新建->其他->從現有代碼中選擇android項目,然后選擇ksoap2源代碼

然后去ksoap2屬性-> android然后檢查是庫它必須為true

然后在您的項目屬性-> android的庫部分中添加ksoap2

嘿,我弄清楚了導致異常的上面的錯誤說 它找不到所需的Ksoap2依賴類。

解:
1.)在eclipse goto項目->屬性-> Java構建路徑->命令和導出中,並選中Ksoap2 jar復選框。 這將使其在運行時可用於項目。

對於上述例外情況,這對我來說非常有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM