-2

我有一个应用程序,它在提供电话号码和密码后连接到 .NET 服务以获取 id。ID 是在服务器端创建的。

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);

    Settings.getSettings(this);

    sqlHelper = SqlHelper.getSqlHelper(this);

    dialog = ProgressDialog.show(SplashScreen.this, "Connecting to server.. ", "Please wait for a few seconds...", true);

    Thread splashThread = new Thread()  
    {
        @Override
        public void run() {
            try 
            {
                if (SqlHelper.checkDataBase(SplashScreen.this) == true)
                {
                    uploadData();

                }
            } 
            catch (Exception e) 
            {
                Log.e("Error! ", " Data could not be uploaded at the moment");
            } 
            finally 
            {
                runOnUiThread(changeMessage);
                finish();
                Intent i = new Intent();
                i.setClassName("show.android.fife", "show.android.fife.Monitor");
                startActivity(i);
                overridePendingTransition(R.anim.infromright, R.anim.outtoleft);
                dialog.dismiss();
                SplashScreen.this.finish();
            }
        }
    };
    splashThread.start();
}

uploadData() 方法如下:

public void uploadData()
{
    int userId = this.getUserId();
    if(userId != 0 || userId != -1)//user setup
    {
        String wsResponse = "";
        try
        {
            wsResponse = WebServiceUtils.callWebService(encryptData(
                            this.getUnsubmittedQuestionnaires(this.csrUnsubmittedQuestionnaires())), 
                                Integer.toString(userId), "insertAnswers", this);

            wsResponse = WebServiceUtils.callWebService(
                    encryptData(this.getUnsubmittedPEF(crsUnsubmittedPEF())), 
                    Integer.toString(userId), "insertPEF", this);

            if(wsResponse=="Thanks, data has been submitted")
            {
                this.updateDbSetAllSent();
            }
            currentStatus = COMPLETE;
        }
        catch(Exception e)
        {
            currentStatus = FAILED;
        }
    }
}

我收到以下错误。我知道有一些类似的问题,但无法解决我的错误。


LogCat 下面:

02-06 15:00:31.281: W/Settings(6522): Setting http_proxy has moved from android.provider.Settings.System to android.provider.Settings.Secure, value is unchanged.
02-06 15:00:45.226: W/System.err(6522): android.os.NetworkOnMainThreadException
02-06 15:00:45.226: W/System.err(6522):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
02-06 15:00:45.226: W/System.err(6522):     at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
02-06 15:00:45.226: W/System.err(6522):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
02-06 15:00:45.231: W/System.err(6522):     at java.net.InetAddress.getAllByName(InetAddress.java:220)
02-06 15:00:45.231: W/System.err(6522):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
02-06 15:00:45.231: W/System.err(6522):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
02-06 15:00:45.236: W/System.err(6522):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
02-06 15:00:45.236: W/System.err(6522):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
02-06 15:00:45.241: W/System.err(6522):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
02-06 15:00:45.241: W/System.err(6522):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
02-06 15:00:45.241: W/System.err(6522):     at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:460)
02-06 15:00:45.246: W/System.err(6522):     at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:432)
02-06 15:00:45.246: W/System.err(6522):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
02-06 15:00:45.251: W/System.err(6522):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
02-06 15:00:45.251: W/System.err(6522):     at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
02-06 15:00:45.251: W/System.err(6522):     at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:188)
02-06 15:00:45.256: W/System.err(6522):     at libcore.net.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:280)
02-06 15:00:45.256: W/System.err(6522):     at org.ksoap2.transport.ServiceConnectionSE.openOutputStream(ServiceConnectionSE.java:109)
02-06 15:00:45.256: W/System.err(6522):     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:157)
02-06 15:00:45.261: W/System.err(6522):     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:96)
02-06 15:00:45.261: W/System.err(6522):     at show.android.fife.WebServiceUtils.callGetIdWebService(WebServiceUtils.java:88)
02-06 15:00:45.266: W/System.err(6522):     at show.android.fife.WebServiceSetupActivity.getId(WebServiceSetupActivity.java:73)
02-06 15:00:45.266: W/System.err(6522):     at show.android.fife.WebServiceSetupActivity.access$0(WebServiceSetupActivity.java:71)
02-06 15:00:45.271: W/System.err(6522):     at show.android.fife.WebServiceSetupActivity$1.onClick(WebServiceSetupActivity.java:66)
02-06 15:00:45.276: W/System.err(6522):     at android.view.View.performClick(View.java:3627)
02-06 15:00:45.276: W/System.err(6522):     at android.view.View$PerformClick.run(View.java:14329)
02-06 15:00:45.276: W/System.err(6522):     at android.os.Handler.handleCallback(Handler.java:605)
02-06 15:00:45.281: W/System.err(6522):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-06 15:00:45.281: W/System.err(6522):     at android.os.Looper.loop(Looper.java:137)
02-06 15:00:45.286: W/System.err(6522):     at android.app.ActivityThread.main(ActivityThread.java:4511)
02-06 15:00:45.286: W/System.err(6522):     at java.lang.reflect.Method.invokeNative(Native Method)
02-06 15:00:45.291: W/System.err(6522):     at java.lang.reflect.Method.invoke(Method.java:511)
02-06 15:00:45.291: W/System.err(6522):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
02-06 15:00:45.291: W/System.err(6522):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
02-06 15:00:45.296: W/System.err(6522):     at dalvik.system.NativeStart.main(Native Method)
02-06 15:00:45.296: D/AndroidRuntime(6522): Shutting down VM
02-06 15:00:45.296: W/dalvikvm(6522):       threadid=1: thread exiting with uncaught exception (group=0x40c651f8)
02-06 15:00:45.311: E/AndroidRuntime(6522): FATAL EXCEPTION: main
02-06 15:00:45.311: E/AndroidRuntime(6522): java.lang.NullPointerException
02-06 15:00:45.311: E/AndroidRuntime(6522):     at show.android.fife.WebServiceSetupActivity.getId(WebServiceSetupActivity.java:73)
02-06 15:00:45.311: E/AndroidRuntime(6522):     at show.android.fife.WebServiceSetupActivity.access$0(WebServiceSetupActivity.java:71)
02-06 15:00:45.311: E/AndroidRuntime(6522):     at show.android.fife.WebServiceSetupActivity$1.onClick(WebServiceSetupActivity.java:66)
02-06 15:00:45.311: E/AndroidRuntime(6522):     at android.view.View.performClick(View.java:3627)
02-06 15:00:45.311: E/AndroidRuntime(6522):     at android.view.View$PerformClick.run(View.java:14329)
02-06 15:00:45.311: E/AndroidRuntime(6522):     at android.os.Handler.handleCallback(Handler.java:605)
02-06 15:00:45.311: E/AndroidRuntime(6522):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-06 15:00:45.311: E/AndroidRuntime(6522):     at android.os.Looper.loop(Looper.java:137)
02-06 15:00:45.311: E/AndroidRuntime(6522):     at android.app.ActivityThread.main(ActivityThread.java:4511)
02-06 15:00:45.311: E/AndroidRuntime(6522):     at java.lang.reflect.Method.invokeNative(Native Method)
02-06 15:00:45.311: E/AndroidRuntime(6522):     at java.lang.reflect.Method.invoke(Method.java:511)
02-06 15:00:45.311: E/AndroidRuntime(6522):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
02-06 15:00:45.311: E/AndroidRuntime(6522):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
02-06 15:00:45.311: E/AndroidRuntime(6522):     at dalvik.system.NativeStart.main(Native Method)

任何帮助将不胜感激。

4

2 回答 2

1

当应用程序尝试在其主线程上执行网络操作时引发的异常。

这仅针对面向 Honeycomb SDK 或更高版本的应用程序抛出。允许以早期 SDK 版本为目标的应用程序在其主事件循环线程上进行网络连接,但非常不鼓励这样做。请参阅文档设计响应性。

请参阅NetworkOnMainThreadException

在所有关闭网络连接中使用AsyncTask来解决此问题。

于 2013-02-06T16:03:38.143 回答
1

honeycomb android开始不允许在主线程上进行网络操作,因此您会遇到异常。将所有与网络相关的操作移至单独的线程。有几个选项,如 AsyncTask(这适用于简短的后台任务)、Executor、Handler 等,网上很容易找到教程。尝试自己实现,如果您再次遇到任何错误,请在此处发布。祝你好运!

于 2013-02-06T16:16:12.047 回答