+
82
-

微信支付APP接入出现android.os.NetworkOnMainThreadException


调试微信app支付的时候出现httpget错误,怎么解决?求大神

2019-08-15 13:25:37.862 32633-32633/net.sourceforge.simcpux W/System.err: android.os.NetworkOnMainThreadException
2019-08-15 13:25:37.862 32633-32633/net.sourceforge.simcpux W/System.err:     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1513)
2019-08-15 13:25:37.862 32633-32633/net.sourceforge.simcpux W/System.err:     at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:117)
2019-08-15 13:25:37.863 32633-32633/net.sourceforge.simcpux W/System.err:     at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:105)
2019-08-15 13:25:37.863 32633-32633/net.sourceforge.simcpux W/System.err:     at java.net.InetAddress.getAllByName(InetAddress.java:1154)
2019-08-15 13:25:37.863 32633-32633/net.sourceforge.simcpux W/System.err:     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:142)
2019-08-15 13:25:37.864 32633-32633/net.sourceforge.simcpux W/System.err:     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:170)
2019-08-15 13:25:37.864 32633-32633/net.sourceforge.simcpux W/System.err:     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:124)
2019-08-15 13:25:37.864 32633-32633/net.sourceforge.simcpux W/System.err:     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:366)
2019-08-15 13:25:37.865 32633-32633/net.sourceforge.simcpux W/System.err:     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:598)
2019-08-15 13:25:37.865 32633-32633/net.sourceforge.simcpux W/System.err:     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:516)
2019-08-15 13:25:37.866 32633-32633/net.sourceforge.simcpux W/System.err:     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:494)
2019-08-15 13:25:37.866 32633-32633/net.sourceforge.simcpux W/System.err:     at net.sourceforge.simcpux.wxapi.HttpUtil.httpGet(HttpUtil.java:80)
2019-08-15 13:25:37.866 32633-32633/net.sourceforge.simcpux W/System.err:     at net.sourceforge.simcpux.MainActivity$1.onClick(MainActivity.java:59)
2019-08-15 13:25:37.866 32633-32633/net.sourceforge.simcpux W/System.err:     at android.view.View.performClick(View.java:6611)
2019-08-15 13:25:37.867 32633-32633/net.sourceforge.simcpux W/System.err:     at android.view.View.performClickInternal(View.java:6588)
2019-08-15 13:25:37.867 32633-32633/net.sourceforge.simcpux W/System.err:     at android.view.View.access$3100(View.java:788)
2019-08-15 13:25:37.867 32633-32633/net.sourceforge.simcpux W/System.err:     at android.view.View$PerformClick.run(View.java:25973)
2019-08-15 13:25:37.867 32633-32633/net.sourceforge.simcpux W/System.err:     at android.os.Handler.handleCallback(Handler.java:873)
2019-08-15 13:25:37.868 32633-32633/net.sourceforge.simcpux W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:99)
2019-08-15 13:25:37.868 32633-32633/net.sourceforge.simcpux W/System.err:     at android.os.Looper.loop(Looper.java:193)
2019-08-15 13:25:37.868 32633-32633/net.sourceforge.simcpux W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6921)
2019-08-15 13:25:37.869 32633-32633/net.sourceforge.simcpux W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
2019-08-15 13:25:37.869 32633-32633/net.sourceforge.simcpux W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
2019-08-15 13:25:37.869 32633-32633/net.sourceforge.simcpux W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:937)


高手回复

+
1
-
对新手来说莫过于一个很大的坑了,遇到这个问题,你会发现,ip正确,给了访问网络的权限,buffer = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));始终会是个空对象,最后终于找到答案:从Honeycomb SDK(3.0)开始,google不再允许网络请求(HTTP、Socket)等相关操作直接在Main Thread类中
修改方法1:
在setContentView(R.layout.activity_main)下添加:
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
方法2:新开线程
new Thread(httpget).start();
/**
* 下载线程
*/
Runnable httpget= new Runnable(){

@Override
public void run() {
// TODO Auto-generated method stub
httpgetmethod();
}
};
by ppzhuzhu
我知道答案,我要回答