2012-11-20 104 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

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