2012-06-12 109 views
-1

我需要通過soap方法從android/eclipse中使用web服務。android/eclipse中的web服務

即,我必須提供一個輸入並根據用戶輸入顯示來自Web服務的適當結果。如何操作?

Java類

public class Demo_webserviceActivity extends Activity 
{ 
/** Called when the activity is first created. */ 

    private static String NAMESPACE = "http://tempuri.org/"; 
    private static String METHOD_NAME = "GetName"; 
    private static String SOAP_ACTION = "http://tempuri.org/GetName"; 
    private static String URL = "http://122.248.240.105:234/Service1.asmx"; 

    Button btnFar; 
    EditText txtFar,txtCel; 


    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    btnFar = (Button)findViewById(R.id.btnFar); 

    txtFar = (EditText)findViewById(R.id.txtFar); 
    txtCel = (EditText)findViewById(R.id.txtCel); 

    btnFar.setOnClickListener(new View.OnClickListener() 
    { 

    public void onClick(View v) 
    { 
    //Initialize soap request + add parameters 
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);  

    //Use this to add parameters 
    request.addProperty("Fahrenheit",txtFar.getText().toString()); 

    //Declare the version of the SOAP request 
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 

    envelope.setOutputSoapObject(request); 
    envelope.dotNet = true; 

    try 
    { 
     HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 

     //this is the actual part that will call the webservice 
     androidHttpTransport.call(SOAP_ACTION, envelope); 

     // Get the SoapResult from the envelope body. 
     SoapObject result = (SoapObject)envelope.bodyIn; 

     if(result != null) 
     { 
      //Get the first property and change the label text 
      txtCel.setText(result.getProperty(0).toString()); 
     } 
     else 
     { 
      Toast.makeText(getApplicationContext(), "No Response",Toast.LENGTH_SHORT).show(); 
     } 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
     } 
    } 
     }); 
    } 
} 

Web方法

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

logcat的

06-12 17:40:00.322: W/InputManagerService(59): Starting input on non-focused client [email protected] (uid=10040 pid=345) 
06-12 17:40:00.352: W/IInputConnectionWrapper(345): showStatusIcon on inactive InputConnection 
06-12 17:40:07.292: D/AndroidRuntime(352): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<< 
06-12 17:40:07.292: D/AndroidRuntime(352): CheckJNI is ON 
06-12 17:40:07.477: D/AndroidRuntime(352): --- registering native functions --- 
06-12 17:40:08.062: D/AndroidRuntime(352): Shutting down VM 
06-12 17:40:08.062: D/dalvikvm(352): Debugger has detached; object registry had 1 entries 
06-12 17:40:08.102: I/AndroidRuntime(352): NOTE: attach of thread 'Binder Thread #3' failed 
06-12 17:40:08.502: D/AndroidRuntime(360): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<< 
06-12 17:40:08.502: D/AndroidRuntime(360): CheckJNI is ON 
06-12 17:40:08.633: D/AndroidRuntime(360): --- registering native functions --- 
06-12 17:40:09.152: I/ActivityManager(59): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.demo.webser/.Demo_webserviceActivity } 
06-12 17:40:09.222: D/AndroidRuntime(360): Shutting down VM 
06-12 17:40:09.222: D/dalvikvm(360): Debugger has detached; object registry had 1 entries 
06-12 17:40:09.252: I/AndroidRuntime(360): NOTE: attach of thread 'Binder Thread #3' failed 

非常感謝

+1

[讓我爲你做?](http://www.google.co.in/search?sugexp=chrome,mod=13&sourceid=chrome&ie=UTF-8&q=android+soap+web+service) –

+0

+1 Samir和-1 @Rohith不搜索 –

+0

只需一分鐘我會用我到目前爲止所做的編輯我的問題 – Rohith

回答

4

您應該使用SoapPrimitive results = (SoapPrimitive)envelope.getResponse();而不是SoapObject result = (SoapObject)envelope.bodyIn;因爲你的web服務返回stringobject

+0

如果我按照你的答案。這些行顯示錯誤txtCel.setText(result.getProperty(0).toString());我能做些什麼 – Rohith

+0

刪除getProperty(0)並嘗試result.toString() – himanshu

+0

它顯示與「名稱未找到」相同的結果...我認爲可能是web方法的問題,我的web方法源是否正確? – Rohith

1

你會在正確的方向,即你在android寫的代碼是正確的。只要確保肥皂行動,方法,URl和命名空間你寫的是正確的。 如果您對這個或任何其他疑問有任何疑問,可以給我寫信。

這裏基本的Android教程來訪問Web服務的Android basic ksoap android tutorial

,並在Java應用創建Web服務本教程How to create java based web service

+0

我結帳Soap操作,方法名稱,網址和命名空間...每一件事情都很好... – Rohith

+0

我覺得我的問題是在網絡方法....請參考網絡方法的來源 – Rohith

+0

你給出的代碼是write.Just請確保你正確部署該服務。請按照我給出的鏈接我的答案檢查Web服務。並且我已經創建Web服務使用克,鏈接和它的工作。如果你仍然有問題,然後寫信給我。 –