android.os.NetworkOnMainThreadException for webservice (ksoap)

拥有回忆 提交于 2019-11-30 13:38:35

You can't do Network operations on the main thread. Checkout : http://developer.android.com/reference/android/os/AsyncTask.html

for painless background threading :)

EDIT: Since I am still getting up-votes for this answer even though it's several years old I would no longer suggest using AsyncTask. It has many known problems which are described here (http://blog.danlew.net/2014/06/21/the-hidden-pitfalls-of-asynctask/). Instead I would urge you to use Retrofit or one of the other nice http clients which handles the threading for you.

Full example:

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

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity {
    private String TAG ="Vik";

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

        AsyncCallWS task = new AsyncCallWS();
        task.execute(); 
    }

    private class AsyncCallWS extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... params) {
            Log.i(TAG, "doInBackground");
            calculate();
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            Log.i(TAG, "onPostExecute");
        }

        @Override
        protected void onPreExecute() {
            Log.i(TAG, "onPreExecute");
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            Log.i(TAG, "onProgressUpdate");
        }

    }

    public void calculate() 
    {
        String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
        String METHOD_NAME = "CelsiusToFahrenheit";
        String NAMESPACE = "http://tempuri.org/";
        String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";

        try { 
            SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
            Request.addProperty("Celsius", "32");

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

            HttpTransportSE transport= new HttpTransportSE(URL);

            transport.call(SOAP_ACTION, soapEnvelope);
            SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();

            Log.i(TAG, "Result Celsius: " + resultString);
        }
        catch(Exception ex) {
            Log.e(TAG, "Error: " + ex.getMessage());
        }

        SOAP_ACTION = "http://tempuri.org/FahrenheitToCelsius";
        METHOD_NAME = "FahrenheitToCelsius";
        try { 
            SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
            Request.addProperty("Fahrenheit", "100");

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

            HttpTransportSE transport= new HttpTransportSE(URL);

            transport.call(SOAP_ACTION, soapEnvelope);
            SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();

            Log.i(TAG, "Result Fahrenheit: " + resultString);
        }
        catch(Exception ex) {
            Log.e(TAG, "Error: " + ex.getMessage());
        }
    }

}

Try adding strictmode to your application. In your onCreate method, add

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!