kSOAP2 请求 returns 空对象

kSOAP2 request returns null object

我是Android新手,请耐心等待。我正在尝试使用来自 http://www.webservicex.net/globalweather.asmx?WSDL 的 kSOAP2 Web 服务,到目前为止我的请求已返回空响应。我的请求有问题还是我遗漏了什么?代码如下:

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


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

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Button sendBtn;
    EditText editCityName, editCountryName;
    String tempCity, tempCountry;
    SoapObject testy;

    SoapObject request;
    final String METHOD_NAME1 = "GetWeather";
    final String SOAP_ACTION1 = "http://www.webserviceX.NET/GetWeather";
    final String NAMESPACE = "http://www.webserviceX.NET";
    final String  SOAP_URL = "http://www.webservicex.net/globalweather.asmx";

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sendBtn = (Button) findViewById(R.id.button);
        editCityName = (EditText) findViewById(R.id.editCityName);
        editCountryName = (EditText) findViewById(R.id.editCountryName);
        sendBtn.setOnClickListener(this);
        Log.e("create", "test123");
    }

    @Override
    public void onClick(View v) {
        tempCity = editCityName.getText().toString();
        tempCountry = editCountryName.getText().toString();
        testyTask task = new testyTask();
        task.execute();
        Log.e("test", "task started");
    }

    private class testyTask extends AsyncTask <String, Void, String>
    {
        @Override
        protected String doInBackground(String... params)
        {
            request = new SoapObject(NAMESPACE, METHOD_NAME1);
            request.addProperty("CityName", tempCity);
            request.addProperty("CountryName", tempCountry);
            Log.e("request1", request.getProperty("CityName").toString());
            Log.e("request2", request.getProperty("CountryName").toString());

            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
            envelope.dotNet = true;
            envelope.setOutputSoapObject(request);

            HttpTransportSE httpTransport = new HttpTransportSE(SOAP_URL);
            Log.d("transport", httpTransport.toString());
            httpTransport.debug = true;
            try {

                httpTransport.call(SOAP_ACTION1, envelope);
                testy = (SoapObject) envelope.bodyIn;


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

            //Log.d("dump Request: " , httpTransport.requestDump);
            String xml = httpTransport.responseDump;
            Log.e("dump response: " , xml); //Error here
            //Log.e("weather", testy.toString());

            return "blah";
        }

        @Override
        protected void onPostExecute(String aVoid)
        {

            Log.e("end test", testy.toString());
            super.onPostExecute(aVoid);
            Toast.makeText(getApplicationContext(), testy.toString(), Toast.LENGTH_LONG).show();
        }

    }
}

首先,使用最新版本的kSoap2。下面是可以下载最新版本的 link。目前最新版本为3.6.1。 https://oss.sonatype.org/content/repositories/ksoap2-android-releases/com/google/code/ksoap2-android/ksoap2-android-assembly/

我使用了您发布的相同代码
MainActivity.class

public class MainActivity extends AppCompatActivity {
    SoapObject request;
    SoapObject testy;
    final String METHOD_NAME1 = "GetWeather";
    final String SOAP_ACTION1 = "http://www.webserviceX.NET/GetWeather";
    final String NAMESPACE = "http://www.webserviceX.NET";
    final String SOAP_URL = "http://www.webservicex.net/globalweather.asmx";

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

        new testyTask().execute();

    }

    private class testyTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... params) {
            request = new SoapObject(NAMESPACE, METHOD_NAME1);
            request.addProperty("CityName", "Delhi");
            request.addProperty("CountryName", "India");
            Log.e("request1", request.getProperty("CityName").toString());
            Log.e("request2", request.getProperty("CountryName").toString());

            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
            envelope.dotNet = true;
            envelope.setOutputSoapObject(request);

            HttpTransportSE httpTransport = new HttpTransportSE(SOAP_URL);
            Log.d("transport", httpTransport.toString());
            httpTransport.debug = true;
            try {

                httpTransport.call(SOAP_ACTION1, envelope);
                testy = (SoapObject) envelope.bodyIn;


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

            //Log.d("dump Request: " , httpTransport.requestDump);
            String xml = httpTransport.responseDump;
            Log.e("dump response: ", xml); //Error here
            //Log.e("weather", testy.toString());

            return "blah";
        }

        @Override
        protected void onPostExecute(String aVoid) {

            Log.e("end test", testy.toString());
            super.onPostExecute(aVoid);
            Toast.makeText(getApplicationContext(), testy.toString(), Toast.LENGTH_LONG).show();
        }

    }
}

我得到的回复是

GetWeatherResponse{GetWeatherResult=<?xml version="1.0" encoding="utf-16"?>
        <CurrentWeather>
        <Location>New Delhi / Palam, India (VIDP) 28-34N 077-07E 233M</Location>
        <Time>May 26, 2016 - 12:00 PM EDT / 2016.05.26 1600 UTC</Time>
        <Wind> Calm:0</Wind>
        <Visibility> 1 mile(s):0</Visibility>
        <SkyConditions> mostly clear</SkyConditions>
        <Temperature> 96 F (36 C)</Temperature>
        <DewPoint> 68 F (20 C)</DewPoint>
        <RelativeHumidity> 39%</RelativeHumidity>
        <Pressure> 29.53 in. Hg (1000 hPa)</Pressure>
        <Status>Success</Status>
        </CurrentWeather>; }

注意: 确保您在清单文件中具有 Internet 权限。