我使用php和json请求将数据从android插入mysql。当事件触发时,它会在logcat中给出异常。我和你分享我的clogcat,这样你就可以更好地了解发生了什么,这是我的logcat:
10-07 03:55:13.073:E / AndroidRuntime(1665):致命异常:主要 10-07 03:55:13.073:E / AndroidRuntime(1665): android.os.NetworkOnMainThreadException 10-07 03:55:13.073: E / AndroidRuntime(1665):at android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133) 10-07 03:55:13.073:E / AndroidRuntime(1665):at java.net.InetAddress.lookupHostByName(InetAddress.java:385)10-07 03:55:13.073:E / AndroidRuntime(1665):at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)10-07 03:55:13.073:E / AndroidRuntime(1665):at java.net.InetAddress.getAllByName(InetAddress.java:214)10-07 03:55:13.073:E / AndroidRuntime(1665):at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137) 10-07 03:55:13.073:E / AndroidRuntime(1665):at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 10-07 03:55:13.073:E / AndroidRuntime(1665):at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) 10-07 03:55:13.073:E / AndroidRuntime(1665):at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360) 10-07 03:55:13.073:E / AndroidRuntime(1665):at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 10-07 03:55:13.073:E / AndroidRuntime(1665):at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 10-07 03:55:13.073:E / AndroidRuntime(1665):at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) 10-07 03:55:13.073:E / AndroidRuntime(1665):at com.example.receiptorganizer.Registration $ 1.insert(Registration.java:78) 10-07 03:55:13.073:E / AndroidRuntime(1665):at com.example.receiptorganizer.Registration $ 1.onClick(Registration.java:58) 10-07 03:55:13.073:E / AndroidRuntime(1665):at android.view.View.performClick(View.java:4240)10-07 03:55:13.073: E / AndroidRuntime(1665):at android.view.View $ PerformClick.run(View.java:17721)10-07 03:55:13.073:E / AndroidRuntime(1665):at android.os.Handler.handleCallback(Handler.java:730)10-07 03:55:13.073:E / AndroidRuntime(1665):at android.os.Handler.dispatchMessage(Handler.java:92)10-07 03:55:13.073:E / AndroidRuntime(1665):at android.os.Looper.loop(Looper.java:137)10-07 03:55:13.073: E / AndroidRuntime(1665):at android.app.ActivityThread.main(ActivityThread.java:5103)10-07 03:55:13.073:E / AndroidRuntime(1665):at java.lang.reflect.Method.invokeNative(Native Method)10-07 03:55:13.073:E / AndroidRuntime(1665):at java.lang.reflect.Method.invoke(Method.java:525)10-07 03:55:13.073: E / AndroidRuntime(1665):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737) 10-07 03:55:13.073:E / AndroidRuntime(1665):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)10-07 03:55:13.073:E / AndroidRuntime(1665):at dalvik.system.NativeStart.main(原生方法)
答案 0 :(得分:1)
当您在主线程(即应用程序的UI线程)中处理网络调用(进行Web服务调用)时,会出现此错误。
使用AsyncTask或Handler管理您的网络服务电话。
请参阅此answer
您的异步任务类可以是这样的
class TheTask extends AsyncTask<Void,Void,Void>
{
protected void onPreExecute()
{ super.onPreExecute();
//display progressdialog.
}
protected void doInBackground(Void ...params)
{
//http request. do not update ui here
return null;
}
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
//dismiss progressdialog.
//update ui
}
}
你可以使用这样的处理程序:
Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
// network call here
}
});
使用 AsyncTask 是一种更好的解决方案。
答案 1 :(得分:0)
请在google android.os.NetworkOnMainThreadException中查找此例外,或者只关注tutroial
答案 2 :(得分:0)
使用AsyncTask或Thread来调用webservice以从您的应用程序发送数据。