2011-11-06 19 views
0

我需要从android进行一些简单的web服务调用。 我知道服务器的IP地址 - 我知道我需要调用什么方法。 服务器基于.net平台 - 我需要调用的方法将返回给我简单的字符串,它会告诉我什么是服务器Web服务版本。我怎样才能使简单的调用从android的网络Web服务?

我不知道如何打这个电话。

感谢您的任何帮助。

回答

2

答案取决于这是什么样的web服务...... SOAP(XML)webservice需要一些XML功能,最简单的选择是使用一个库(请参阅下面的KSOAP2)... REST webservice可能工作纯HTTP(或许再加JSON)......

对于一些示例源代码/演练/库/关于如何从Android调用web服务的所有提到的选项DOC看到:

+1

版本0.0.4脱离与教程: http://wiki.javaforum.hu/display/ANDROIDSOAP/2012/04/16/Version+0.0.4+released 的http://维基。 javaforum.hu/display/ANDROIDSOAP/Step+by+step+tutorial –

1
public class SOAPActivity extends Activity { 
    private final String NAMESPACE = "http://www.webserviceX.NET/"; 
     private final String URL = "http://www.webservicex.net/ConvertWeight.asmx"; 
     private final String SOAP_ACTION = "http://www.webserviceX.NET/ConvertWeight"; 
     private final String METHOD_NAME = "ConvertWeight"; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     SoapObject soapObject=new SoapObject(NAMESPACE, METHOD_NAME); 

     String weight = "700"; 
     String fromUnit = "Kilograms"; 
     String toUnit = "Grams"; 

     PropertyInfo weightProp =new PropertyInfo(); 
     weightProp.setName("Weight"); 
     weightProp.setValue(weight); 
     weightProp.setType(double.class); 
     soapObject.addProperty(weightProp); 

     PropertyInfo fromProp =new PropertyInfo(); 
     fromProp.setName("FromUnit"); 
     fromProp.setValue(fromUnit); 
     fromProp.setType(String.class); 
     soapObject.addProperty(fromProp); 

     PropertyInfo toProp =new PropertyInfo(); 
     toProp.setName("ToUnit"); 
     toProp.setValue(toUnit); 
     toProp.setType(String.class); 
     soapObject.addProperty(toProp); 

     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
     envelope.dotNet = true; 
     envelope.setOutputSoapObject(soapObject); 
     HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 

     try { 
      androidHttpTransport.call(SOAP_ACTION, envelope); 
      SoapPrimitive response = (SoapPrimitive)envelope.getResponse(); 
      Log.i("myApp", response.toString()); 

      TextView tv = new TextView(this); 
      tv.setText(weight+" "+fromUnit+" equal "+response.toString()+ " "+toUnit); 
      setContentView(tv); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    } 

这是SOAP的Web服务代码。

相关问题