gani vvs
gani vvs

Reputation: 1

NetworkOnMainThreadException in Android

I have this exception. How can I do this using Threading concept?

public class WebService extends AsyncTask<String, String, String>{

    private final String NAMESPACE ="http://tempuri.org/";//"http://localhost:9583/YP_Android_WebService/YP_Android_WebService.asmx";//
    private final String URL ="http://10.0.2.2:1243/YP_Android_WebService/YP_Android_WebService.asmx?WSDL";

    public boolean checkUser(String _EmailID, String _Password) {

         boolean result = false; 

        final String SOAP_ACTION ="http://tempuri.org/checkUser";//"http://localhost:9583/YP_Android_WebService/YP_Android_WebService.asmx/checkUserLogin";//
        final String METHOD_NAME = "checkUser";

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

        PropertyInfo propertyInfoEmail = new PropertyInfo();
        propertyInfoEmail.setName("_EmailID");
        propertyInfoEmail.setValue(_EmailID);
        propertyInfoEmail.setType(String.class);
        request.addProperty(propertyInfoEmail);

        PropertyInfo propertyInfoPassword = new PropertyInfo();
        propertyInfoPassword.setName("_Password");
        propertyInfoPassword.setValue(_Password);
        propertyInfoPassword.setType(String.class);
        request.addProperty(propertyInfoPassword);

         final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true; // put this only if the web service is .NET one
            envelope.setOutputSoapObject(request);
            final HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

        try {


            androidHttpTransport.call(SOAP_ACTION, envelope);
            SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
            Log.i("myApp", response.toString());
            if(response.toString().equalsIgnoreCase("true")){
                result = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}

The Exception is at this point.

androidHttpTransport.call(SOAP_ACTION, envelope); ti throws NetworkonMainThread Exception.

Can any one help me to do this using Thread concept

Upvotes: 0

Views: 747

Answers (4)

Alex Lockwood
Alex Lockwood

Reputation: 83311

The reason why you are getting this exception is because you are attempting to perform an expensive operation on the UI thread, which can significantly slow your app down and cause it to force close. You should wrap your code in an AsyncTask (or Thread) instead.

Read this for more info:

Why Ice Cream Sandwich Crashes Your App

Upvotes: 1

sbrichards
sbrichards

Reputation: 2210

You should use Asynctask, but if you do not want to simply add the below code snippet (although not recommended):

if (android.os.Build.VERSION.SDK_INT > 9) { // disabling strict-mode
                                                    // for newer builds
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                    .permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

Upvotes: 0

gsb
gsb

Reputation: 5640

You are not using the Asynctask properly. You have unimplemented methods. You have to do all the Network / Database operations in the doInBackground() method, and as @Dirk said, UI in the onPostExecute() method.

This tutorial is also very helpful for you to understand AsyncTasks and learn more about Threads.

Upvotes: 4

Dirk J&#228;ckel
Dirk J&#228;ckel

Reputation: 3025

You can use an AsyncTask to solve this problem. Do the networking in doInBackground() and the UI modifying in onPostExecute().

Upvotes: 0

Related Questions