NetworkOnMainThreadException Android中 [英] NetworkOnMainThreadException in Android

查看:94
本文介绍了NetworkOnMainThreadException 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,信封); TI抛出
  NetworkonMainThread异常。

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

任何一个可以帮助我做到这一点使用线程概念

Can any one help me to do this using Thread concept

推荐答案

您没有使用的AsyncTask 正常。你有没有实现的方法。你要做的所有的网络/数据库操作中的 doInBackground()方法,并作为@Dirk说,在UI中的 onPostExecute()方法。

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.

教程也非常有助于你理解 AsyncTasks 并了解更多有关主题

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

这篇关于NetworkOnMainThreadException Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆