0

Here is this sample code (MainActivity):

private static final String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
private static final String METHOD_NAME = "CelsiusToFahrenheit";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
TextView tv;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv = (TextView)findViewById(R.id.text1);

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    request.addProperty("Celsius", "32");

    SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER11);
    soapEnvelope.dotNet = true;
    soapEnvelope.setOutputSoapObject(request);

    HttpTransportSE aht = new HttpTransportSE(URL);

    try {
        aht.call(SOAP_ACTION, soapEnvelope);
        SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
        tv.setText("Status: " + resultString);
    } catch (Exception e) {
        tv.setText("Problem: " + e.toString());
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

...

When I run this code I got this message Problem: android.os.NetworkOnMainThreadException. In the manifest.xml added the

<uses-permission android:name="android.permission.INTERNET" />

line. Here is the whole android project.

Does someone have any idea how to change this code? Thank you in advance for any help you can provide.

los.adrian
  • 509
  • 1
  • 6
  • 19

3 Answers3

2

This exception is thrown when an android application attempts to perform a network operation on its main thread. You must run your code in AsyncTask. Your code will look something like this You can declare resultString as an instance variable

class SoapTask extends AsyncTask<String, Void, Void> {

    private Exception exception;

        @Override
    protected Void doInBackground(Void... arg0) {
        try {
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            request.addProperty("Celsius", "32");

            SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER11);
            soapEnvelope.dotNet = true;
            soapEnvelope.setOutputSoapObject(request);

            HttpTransportSE aht = new HttpTransportSE(URL);

            try {
                aht.call(SOAP_ACTION, soapEnvelope);
                resultString = "Status:" + (SoapPrimitive)soapEnvelope.getResponse(); 
            } catch (Exception e) {
                resultString = "Problem:" + e.toString();
            }
        }

    protected void onPostExecute(Void result) {
         tv.setText("Status: " + resultString);
    }
}
Horrorgoogle
  • 7,858
  • 11
  • 48
  • 81
Periklis Douvitsas
  • 2,431
  • 1
  • 13
  • 14
0

Try this..

NetworkOnMainThreadException throws when you are performing network operation on its main thread.

So you have to run you network operations in AsyncTask

You can call AsyncTask like below

new SerializationConnection().execute(URL);

then SerializationConnection class

class SerializationConnection extends AsyncTask<String, Void, String> {

    protected String doInBackground(String... urls) {
        try {
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            request.addProperty("Celsius", "32");

            SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER11);
            soapEnvelope.dotNet = true;
            soapEnvelope.setOutputSoapObject(request);

            HttpTransportSE aht = new HttpTransportSE(urls[0]);
            aht.call(SOAP_ACTION, soapEnvelope);
            SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
            return "Status: " + resultString;
        } catch (Exception e) {
            return "Problem: " + e.toString();
        }
    }

    protected void onPostExecute(String result) {
        tv.setText(result);
    }
}
Hariharan
  • 24,741
  • 6
  • 50
  • 54
0

You are making a server call in main thread.if your target version is 9

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

For Better way try to use AsynTask

Yuvaraja
  • 715
  • 6
  • 22