2012-12-10 68 views
0

我想從android傳遞一個值到aspx web服務。但是,Web服務不採取在下面的代碼中傳遞的32。Request.addProperty(「SayHello」,「32」);不通過32?

Request.addProperty("SayHello", "32"); 

任何幫助將不勝感激。

我的完整的Android代碼如下

package com.example.fp1_webservicedropdown; 

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.TextView; 

import org.ksoap2.*; 
import org.ksoap2.serialization.SoapObject; 
import org.ksoap2.serialization.SoapPrimitive; 
import org.ksoap2.serialization.SoapSerializationEnvelope; 
import org.ksoap2.transport.*; 

public class MainActivity extends Activity { 
    TextView result; 

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

     result = (TextView) findViewById(R.id.textView2); 

     final String NAMESPACE = "http://sample.com/"; 
     final String METHOD_NAME = "SayHello"; 
     final String SOAP_ACTION = "http://sample.com/SayHello"; 
     final String URL = "http://myLoclalIP/HellowWorld/Service1.asmx"; 

     SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME); 
     Request.addProperty("SayHello", "32"); 

     SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
     soapEnvelope.dotNet = true; 
     soapEnvelope.setOutputSoapObject(Request); 

     AndroidHttpTransport aht = new AndroidHttpTransport(URL); 

     try { 
      aht.call(SOAP_ACTION, soapEnvelope); 
      SoapPrimitive resultString = (SoapPrimitive) soapEnvelope.getResponse(); 
      result.setText("The web service returned " + resultString); 
     } 

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

我的web服務代碼如下:

[WebService(Namespace = "http://sample.com/")] 
    public class Service1 : System.Web.Services.WebService 
    { 
     [WebMethod] 
     public String SayHello(int a) 
     { 
      int passingValue = 8 + a; 
      String aTemp=passingValue.ToString(); 
      return aTemp; 
     } 


    } 
+0

印在服務器端有什麼價值?我的意思是你有任何的logcat或控制檯打印value.Kindly共享 – curious

+0

@curious我已經加入上面的web方法代碼。因此,它只能打印8. – Kasanova

回答

0

沒有在文檔的請求類中的任何方法,方法addProperty。雖然您可以將URI構造爲字符串並將其傳遞給構造函數,或使用setDestinationUri(uri)方法。

例如,

uri = "http://www.mydomain.com/index?SayHello=32" 
my_request.setDestinationUri(uri) 

進行更多的控制也有自己設置的HTTP標頭的方法。

+0

我的完整Android代碼看起來像上面編輯 – Kasanova

+0

您正在使用SOAP,好吧。 webservice方法看起來可能是錯誤的? – adarsh

+0

哦,我需要做什麼改變的Web服務方法,以便我可以添加傳遞值8? – Kasanova

0

這是示例...

package com.mehul.Web_Service; 

import org.ksoap2.SoapEnvelope; 
import org.ksoap2.serialization.PropertyInfo; 
import org.ksoap2.serialization.SoapObject; 
import org.ksoap2.serialization.SoapSerializationEnvelope; 
import org.ksoap2.transport.HttpTransportSE; 

import android.R.string; 
import android.app.Activity; 
import android.content.Intent; 
import android.graphics.Typeface; 
import android.net.Uri; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 


public class Web_ServiceActivity extends Activity 
{ 
    //SoapObject request; 
    Button ok,btnimage; 
    EditText eprice; 
    TextView tprice; 


    private final String NAMESPACE = "http://www.webserviceX.NET/"; 
    private final String URL = "http://www.webservicex.net/stockquote.asmx"; 
    private final String SOAP_ACTION = "http://www.webserviceX.NET/GetQuote"; 
    private final String METHOD_NAME = "GetQuote"; 

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

     ok = (Button)findViewById(R.id.btnok); 
     eprice = (EditText)findViewById(R.id.edtprice); 
     tprice = (TextView)findViewById(R.id.txtprice); 
     Typeface typface=Typeface.createFromAsset(getAssets(),"fonts/Gretoon.ttf"); 
     tprice.setTypeface(typface); 

     ok.setOnClickListener(new OnClickListener() 
     { 

      public void onClick(View v) 
      { 
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
        PropertyInfo weightProp =new PropertyInfo(); 
        weightProp.setName("symbol"); 

        weightProp.setValue(eprice.getText().toString()); 

        weightProp.setType(string.class); 
        request.addProperty(weightProp); 


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

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


         //tv.setText(weight+" "+fromUnit+" equal "+response.toString()+ " "+toUnit); 

         String str = obj1.toString(); 
         Log.d("My Price", ""+str); 
         int start = str.indexOf("<Last>")+("<Last>").length(); 
         int end = str.indexOf("</Last>"); 

         tprice.setText(""+str.substring(start,end)+" Price"); 
         Toast.makeText(getApplicationContext(), "Set Price Successfully .. "+str.substring(start,end), Toast.LENGTH_SHORT).show(); 


         request = null; 

        } 
        catch (Exception e) 
        { 
         //e.printStackTrace(); 
         Log.d("Exception Generated", ""+e.getMessage()); 
        } 

      } 
     }); 

     //String weight = "ITC"; 
     //String fromUnit = "Grams"; 
     //String toUnit = "Kilograms"; 



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

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



    } 


} 
+0

仍然返回8 bro – Kasanova

+0

您傳遞字符串,並在方法採取整數? –

+0

你能告訴我的web方法添加傳遞的數字與8 – Kasanova