0

伙计们,我是 android 编程新手,我尝试使用 KSOAP2 将数据发送到 Web 服务。但我无法发送值或获取数据。在调试模式下,它不会显示错误。我正在使用 android 4.1 模拟器。但我找不到任何结果或错误。有人可以纠正我在程序中的错误吗

在此处输入图像描述

下面给出了我的java程序,

package com.example.webservice;

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

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
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 WebService extends Activity {
       private final String NAMESPACE = "http://tempuri.org/";
       private final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx?wsdl";
       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) {

            SoapObject request= new SoapObject(NAMESPACE, METHOD_NAME);
            PropertyInfo celsuiusPI= new PropertyInfo();
            celsuiusPI.setName("Celsius");
            celsuiusPI.setValue(celsius);
            celsuiusPI.setType(String.class);
            request.addProperty(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);
            envelope.setOutputSoapObject(request);
            HttpTransportSE androidHttpTransport=new HttpTransportSE(URL);
            try{
                androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                androidHttpTransport.debug = true;
                androidHttpTransport.call(SOAP_ACTION, envelope);
                final SoapPrimitive response=(SoapPrimitive)envelope.getResponse();
                Toast.makeText(WebService.this, response.toString(), 20).show();
                Log.i("WebService output", 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");

    }


}
}

上述程序将文本值作为整数发送,并接收摄氏度/华氏度输出。提前感谢程序员,我已经编辑了我的代码,我收到了空值。我怀疑我在代理上,代理服务器是否阻止从 android Emulator 发送/接收数据

4

3 回答 3

0

你应该发送一个字符串值而不是一个整数值,它可以像这样发送

PropertyInfo pi = new PropertyInfo();
    pi.setName("Celsius");  

    pi.setValue("22");
    pi.setType(String.class);
    request.addProperty(pi);(//request is soap object )
于 2012-11-14T05:59:28.610 回答
0

您应该练习此示例链接以了解使用 ksoap 的 Web 服务。 http://android-helper.blogspot.in/2012/04/accessing-webservices-using-soap.html。让我知道你有任何疑问。

于 2012-11-14T07:11:34.730 回答
0

你可以使用这种代码,

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo pi = new PropertyInfo();
pi.setName("Celsius");  
pi.setValue("22");
pi.setType(String.class);
request.addProperty(pi);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
Object result = envelope.getResponse();
于 2012-11-16T02:59:19.123 回答