2012-11-20 118 views
0

我編寫了一個用於與Web服務進行通信並獲取響應值的程序。但是,當我調試程序我在符合requestDump = NULL結束androidHttpTransport.call(SOAP_ACTION, envelope);能有人告訴我錯誤的原因,我可以爲這個Android Soap Web服務代理服務器後面的錯誤

public class WebService extends Activity { 
     private final String NAMESPACE = "http://tempuri.org/"; 
     private final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx"; 
     private final String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit"; 
     private final String METHOD_NAME = "CelsiusToFahrenheit"; 
    String celsius; 
    Button b; 
    TextView tv; 
    EditText et; 
    String res,resultval; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_web_service); 
     et=(EditText)findViewById(R.id.editText1); 

     tv=(TextView)findViewById(R.id.Result); 
     b=(Button)findViewById(R.id.button1); 
     b.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
      //String result=getFarenheit(et.getText().toString()); 
      //tv.setText(result+"°F"); 
      new service().execute(); 
      } 
     }); 
    } 
    private class service extends AsyncTask<Void, Void, String>{ 

     @Override 
     protected String doInBackground(Void... arg0) { 
      celsius=et.getText().toString(); 
      SoapObject request= new SoapObject(NAMESPACE, METHOD_NAME); 
      PropertyInfo celsuiusPI= new PropertyInfo(); 
      celsuiusPI.setName("Celsius"); 
      celsuiusPI.setValue(celsius); 
      celsuiusPI.setType(String.class); 
      request.addProperty("XMLMarks",celsuiusPI); 
      SoapSerializationEnvelope envelope=new SoapSerializationEnvelope (SoapEnvelope.VER11); 
      envelope.dotNet=true; 
      envelope.implicitTypes = true; 
      envelope.enc = SoapSerializationEnvelope.ENC2003; 
      envelope.xsd = SoapEnvelope.XSD; 
      envelope.xsi = SoapEnvelope.XSI; 
      envelope.setOutputSoapObject(request); 
      envelope.setAddAdornments(false); 
      SoapPrimitive response; 

      HttpTransportSE androidHttpTransport=new HttpTransportSE(URL); 
      try{ 
       androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); 
       androidHttpTransport.debug = true; 
       androidHttpTransport.call(SOAP_ACTION, envelope); 
       String dump= androidHttpTransport.requestDump.toString(); 
       response=(SoapPrimitive)envelope.getResponse(); 
       Toast.makeText(WebService.this, response.toString(), 20).show(); 
       Log.i("WebService output", response.toString()); 
       System.out.println("WebService Response"+response.toString()); 
       Object res= response.toString(); 
       resultval=(String) res; 
      } 
      catch(Exception e){ 
       e.printStackTrace(); 
      } 

      return res; 

     } 
     protected void onPostExecute(String h){ 
      String result=h; 

       tv.setText(result+"°F"); 

    } 


} 
} 
+0

你與androidHttpTransport.setXmlVersionTag做( 「<?XML版本= \」 1.0 \ 「編碼= \ 」UTF-8 \「?>」);沒有必要這樣做。 SOAP只是迴應你。你不必從xml結構中解析它。 –

回答

1

做什麼就用這個新更換服務的AsyncTask和看到結果:

代碼:

private class service extends AsyncTask<Void, Void, String> { 

    @Override 
    protected String doInBackground(Void... arg0) { 
     System.out.println("In DoIn Background"); 

     // Initialize soap request + add parameters 
     SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 

     // Use this to add parameters 
     request.addProperty("Celsius", txtCel.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 
       // txtFar.setText(result.getProperty(0).toString()); 
       res = result.getProperty(0).toString(); 
      } else { 
       Toast.makeText(getApplicationContext(), "No Response", 
         Toast.LENGTH_LONG).show(); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return res; 

    } 

    protected void onPostExecute(String h) { 
     String result = h; 

     tv.setText(result + "°F"); 

    } 

} 
+0

仍然我得到null'F結果事情是亞姆在代理requestDump爲null。如何將代理添加到HTTPTransportSE –

+0

你剛剛複製粘貼上面的代碼,並運行項目或已經做了更多的應用程序?因爲我可以成功地在這裏運行它。 –

+0

所以希望你能得到答案。你也可以加入它。所以它可以幫助別人。 –