0

http://www.webserviceX.NET/CurrencyConvertor.asmxの Web サービスを使用して通貨コンバーターを作成しようとしています。 問題に対する答えをどこでも探しましたが、他のユーザーと同じ問題を解決してもうまくいきませんでした。私たちのために、私たちは何をすべきかわかりません。問題は、結果が null のままであることです...これが私たちのコードです:

package com.example.ws;

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.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

    TextView tvResult;
    Button btnConvert;

    final String NAME_SPACE = "http://www.webserviceX.NET/";
    String URL = "http://www.webserviceX.NET/CurrencyConvertor.asmx";
    final String METHOD_NAME = "ConversionRate";
    final String SOAP_ACTION = "http://www.webserviceX.NET/ConversionRate";

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

        tvResult = (TextView) findViewById(R.id.tvPretvoreniIznos);

        btnConvert = (Button) findViewById(R.id.btnPretvori);

        btnConvert.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                SoapObject request = new SoapObject(NAME_SPACE, METHOD_NAME);

                PropertyInfo propInfo = new PropertyInfo(); 
                propInfo.name = "FromCurrency"; 
                propInfo.type = PropertyInfo.STRING_CLASS;
                propInfo.setValue("USD");

                PropertyInfo propInfo2 = new PropertyInfo();
                propInfo2.name = "ToCurrency";
                propInfo2.type = PropertyInfo.STRING_CLASS;
                propInfo2.setValue("EUR");

                request.addProperty(propInfo);
                request.addProperty(propInfo2);

                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

                envelope.setOutputSoapObject(request);
                envelope.dotNet = true;

                HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

                try {
                    androidHttpTransport.call(SOAP_ACTION, envelope);

                    SoapPrimitive resultsRequestSOAP = (SoapPrimitive) envelope.getResponse();

                    tvResult.setText(resultsRequestSOAP.toString());

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

                }

            }
        }); 
    }
}

他の人が以前にこの問題を抱えていて、簡単な解決策がうまくいったことを発見し、それらすべてを試しましたが、成功しませんでした.

AsyncTask でネットワーク アクティビティを実行しましたが、成功しませんでした。コードは次のとおりです。

package com.example.ws;

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.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

    TextView tvResult;
    Button btnConvert;

    final String NAME_SPACE = "http://www.webserviceX.NET/";
    String URL = "http://www.webserviceX.NET/CurrencyConvertor.asmx";
    final String METHOD_NAME = "ConversionRate";
    final String SOAP_ACTION = "http://www.webserviceX.NET/ConversionRate";
    String res;

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

        tvResult = (TextView) findViewById(R.id.tvPretvoreniIznos);
        btnConvert = (Button) findViewById(R.id.btnPretvori);
        btnConvert.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                new convert().execute();

            }
        });
    }

    public class convert extends AsyncTask<String, Void, String> {

        SoapObject result = null;

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub

            SoapObject request = new SoapObject(NAME_SPACE, METHOD_NAME);
            PropertyInfo propInfo = new PropertyInfo();
            propInfo.name = "FromCurrency";
            propInfo.type = PropertyInfo.STRING_CLASS;
            propInfo.setValue("USD");

            PropertyInfo propInfo2 = new PropertyInfo();
            propInfo2.name = "ToCurrency";
            propInfo2.type = PropertyInfo.STRING_CLASS;
            propInfo2.setValue("EUR");

            request.addProperty(propInfo);
            request.addProperty(propInfo2);

            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);

            envelope.setOutputSoapObject(request);
            envelope.dotNet = true;

            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

            try {
                 androidHttpTransport.call(SOAP_ACTION, envelope);

                 SoapPrimitive resultsRequestSOAP = (SoapPrimitive) envelope.getResponse();

                 res = resultsRequestSOAP.toString();

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

    return null;
        }

        protected void onPostExecute(String r) {
            String res = r;
            tvResult.setText(res);

        }
    }
}

私が間違っていることを確認してください。

4

2 に答える 2