1

目前我正在通过 SOAP 方法使用来自 android 的 Web 服务。这里我需要调用 Web 服务并在 android/eclipse.ie 上返回结果。我需要从 edittext 获取输入并返回适当的值。

请找到我的代码以供参考。

网络方法

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

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

调用者.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();  
    }
    }    }

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

输出屏幕

在此处输入图像描述

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

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

感谢您宝贵的时间!..

4

1 回答 1

1

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

于 2012-06-11T11:19:58.790 回答