簡體   English   中英

調用Web服務並通過SOAP方法在Android / Eclipse上返回結果

[英]Call the web service and return the result on Android/Eclipse by SOAP method

目前,我正在通過SOAP方法使用android的web服務。在這里,我需要調用webservice並在android / eclipse.ie上返回結果。我需要從edittext獲取輸入並返回適當的值。

請找到我的代碼以供參考。

網絡方法

public class GetName {
public String GetName(String a){
return(a);
}  }

注意:為避免主活動線程產生套接字操作異常,我編寫了一個單獨的類並隔離了與soap相關的功能。

調用者

public class Caller  extends Thread 
{

public AlertDialog ad;
public CallSoap cs;
public int a;

public void run()
{
    try
    {
    cs=new CallSoap();  
    String resp=cs.Call(a);
    MySOAPCallActivity.rslt=resp;
    }catch(Exception ex)
    {
        MySOAPCallActivity.rslt=ex.toString();  
    }
    }    }

呼叫SOAP

public class CallSoap 
{
public final String SOAP_ACTION = "http://tempuri.org/GetName";

public  final String OPERATION_NAME = "GetName";

public  final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";

public  final String SOAP_ADDRESS = "http://122.248.240.105:234/Service1.asmx?WSDL";
public CallSoap()
{

}

public String Call(int a)
{

    SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);
    PropertyInfo pi=new PropertyInfo();
    pi.setName("a");
    pi.setValue(a);
    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();
    }
 } 

MySOAPCallActivity

public class MySOAPCallActivity extends Activity 
    {
public static String rslt="";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button b1=(Button)findViewById(R.id.btn_getvalues);
    final  AlertDialog ad=new AlertDialog.Builder(this).create();

     b1.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {

        try
            {

            EditText ed1=(EditText)findViewById(R.id.et_1);

            String ed1str = ed1.getText().toString().trim();
            int a = Integer.parseInt(ed1str);

            rslt="START";           
            Caller c=new Caller();
            c.a=a;
            c.ad=ad;
            c.join();
            c.start();
            while(rslt=="START")
            {
                try
                {
                    Thread.sleep(5);

                }catch(Exception ex)
                {

                }
            }
           ad.setTitle("");
            ad.setMessage(rslt);

            }catch(Exception ex)
            {
                ad.setTitle("Error!");
                ad.setMessage(ex.toString());

            }
           ad.show();   
        }
    });
    }    }

輸出畫面

在此處輸入圖片說明

注意:如果輸入vlaue “ 0005”,則必須在瀏覽器中返回字符串“ Vivek”

但是在我的輸出屏幕上,它顯示為“找不到名稱”。我想根據用戶輸入返回適當的值。請讓我知道如何修補源。

感謝您的寶貴時間!

請利用我的經驗。 我認為它將為您提供幫助。 在執行相同任務時,我遇到很多麻煩。 但是最后我做了博客中給出的事情。 http://www.insightforfuture.blogspot.com/search/label/Android

暫無
暫無

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

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