如题,我是希望发送一个xml文件到服务器,但是运行之后发现报错,想知道是怎么一回事,这个报错要怎么解决?错误信息如下:
08-11 22:18:32.180: W/System.err(22906): android.os.NetworkOnMainThreadException
08-11 22:18:32.190: W/System.err(22906): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1077)
08-11 22:18:32.190: W/System.err(22906): at java.net.InetAddress.lookupHostByName(InetAddress.java:477)
08-11 22:18:32.190: W/System.err(22906): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:277)
08-11 22:18:32.190: W/System.err(22906): at java.net.InetAddress.getAllByName(InetAddress.java:249)
08-11 22:18:32.190: W/System.err(22906): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:136)
08-11 22:18:32.190: W/System.err(22906): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
08-11 22:18:32.190: W/System.err(22906): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
08-11 22:18:32.190: W/System.err(22906): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
08-11 22:18:32.190: W/System.err(22906): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
08-11 22:18:32.190: W/System.err(22906): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
08-11 22:18:32.190: W/System.err(22906): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
另外附上我的代码:
1 private void post() 2 { 3 String urlString = "http://www.gzconcern.cn/kangxin/post_pinggu"; 4 String srcPath = Environment.getExternalStorageDirectory().getPath() 5 + "/sys/PingGu/" + MOKUAI + ".xml"; 6 URI url = null; 7 String end = "\r\n"; 8 String twoHyphens = "--"; 9 String boundary = "******"; 10 try 11 { 12 url = new URI(urlString); 13 HttpClient httpclient = new DefaultHttpClient(); 14 HttpPost request = new HttpPost(url); 15 StringEntity s = new StringEntity(du(srcPath), HTTP.UTF_8); 16 s.setContentType("text/xml charset=utf-8"); 17 request.setEntity(s); 18 HttpResponse re=httpclient.execute(request); 19 String result = EntityUtils.toString(re.getEntity(), HTTP.UTF_8); 20 Log.i("result", result); 21 } 22 catch (Exception e) 23 { 24 e.printStackTrace(); 25 }
26 }
经过研究所得,这是因为这样的长耗时工作不能在ui线程下处理,而我把这个写在了ui线程下所以报错.于是我进行了异步操作,问题得到了解决
不是web的客户端是android
代表什么呢?
@琴弦欲奏: 你应该是通过android的手机提交xml到服务器上得吧?