调用Web服务并通过SOAP方法在Android / Eclipse上返回结果

时间:2012-06-01 07:49:39

标签: java android eclipse web-services

目前我通过SOAP方法从android消费web服务。我需要调用webservice并在android / eclipse.ie上返回结果。我需要从edittext获取输入并返回相应的值。

请找我的代码以供参考。

网络方法

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

注意:为了避免来自主活动线程的套接字操作异常,我编写了一个单独的类并隔离了与soap相关的函数。

Caller.java

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

CallSOAP

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

输出屏幕

enter image description here

注意:如果我输入了“0005”,则必须在浏览器中返回字符串“Vivek”

但在我的输出屏幕中显示为“找不到姓名”。我想根据用户输入返回适当的值。请让我知道如何修改我的来源。

感谢您宝贵的时间!..

1 个答案:

答案 0 :(得分:1)

请使用我的经验。我认为它会对你有所帮助。在完成同样的任务时我遇到了很多麻烦。但最后我做了博客中给出的事情。 http://www.insightforfuture.blogspot.com/search/label/Android