我有一个应用程序,在提供电话号码和密码后连接到.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)
非常感谢任何帮助。
答案 0 :(得分:1)
当应用程序尝试在其主线程上执行网络操作时引发的异常。
仅针对Honeycomb SDK或更高版本的应用程序进行此操作。针对早期SDK版本的应用程序可以在其主要事件循环线程上进行网络连接,但是非常不鼓励这样做。请参阅文档设计响应性。
请参阅NetworkOnMainThreadException
在网络连接中使用AsyncTask来解决此问题。
答案 1 :(得分:1)
从蜂窝开始,android不允许在主线程上进行网络操作,因此您将获得异常。将所有网络相关操作移动到单独的线程。有几个选项,如AsyncTask(这适用于短时间背景任务),Executor,Handler等,教程可以在网上轻松获得。如果您再次遇到任何错误,请尝试自己实现,然后在此处发布。祝你好运!