0

we are trying to make a currency converter using web service at http://www.webserviceX.NET/CurrencyConvertor.asmx We have searched everywhere for an answer to our problem, but even solutions to the same problem with other users didn't work for us and we don't know what to do. The problem is that our result keeps being null... this is our code:

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());

                }

            }
        }); 
    }
}

We have found that others have had this problem before and simple solutions worked for them, and we tried them all but with no success.

I did network activity in an AsyncTask but with no success. Here is code:

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);

        }
    }
}

Can you please check what am I doing wrong?

TikaTaka
  • 1
  • 2
  • possible duplicate of [android.os.NetworkOnMainThreadException](http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception) – Peter O. Apr 25 '13 at 14:27

2 Answers2

1

This post has 2 years, but I found the solution to the problem. You have two errors, one is you don´t override onPostExecute method; and the other is, you have to use the global "String res" variable. So the code has to be:

@Override
protected void onPostExecute(String r) {
    tvResult.setText(res);
}
0

You are trying access Network on Main (UI thread). This is no longer allowed, starting Honeycomb an above. You may choose to do the network activity in an AsyncTask. See the accepted answer in this SO question.

Community
  • 1
  • 1
Pradeep Pati
  • 5,779
  • 3
  • 29
  • 43
  • Thanks for your answer. I did network activity in an AsyncTask but I still got null instead of result. I add new code in our question. – TikaTaka Apr 29 '13 at 21:05