2011-11-06 63 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服務代碼。