android.os.NetworkOnMainThreadException
当我尝试从我的 PC 主机检索 JSON 数据时,我得到了一个,这是我的代码
public JSONObject getJSONObject(String url)
{
Log.v("", "enter");
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
Log.v("", "httpclient");
HttpPost httppost = new HttpPost(url);
Log.v("", "httppost");
httppost.setHeader("Content-type", "application/json");
Log.v("", "setHeader");
InputStream inputStream = null;
HttpResponse response;
try
{
response = httpclient.execute(httppost);
Log.v("", "response");
HttpEntity entity = response.getEntity();
Log.v("", "entity");
inputStream = entity.getContent();
Log.v("", "inputStream");
Log.v("", buildJSONString( inputStream ));
return new JSONObject( buildJSONString( inputStream ) );
}
}
我的活动电话
JSONObject jObject = (new JSONParser(this))
.getJSONObject("http://192.168.0.81:8080/content/test.json");
和我的日志猫
09-12 12:32:05.878: V/(11103): enter
09-12 12:32:05.878: V/(11103): httpclient
09-12 12:32:05.878: V/(11103): httppost
09-12 12:32:05.878: V/(11103): setHeader
09-12 12:32:05.878: W/System.err(11103): android.os.NetworkOnMainThreadException
09-12 12:32:05.888: W/System.err(11103): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1108)
09-12 12:32:05.888: W/System.err(11103): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
09-12 12:32:05.888: W/System.err(11103): at libcore.io.IoBridge.connectErrno(IoBridge.java:133)
09-12 12:32:05.888: W/System.err(11103): at libcore.io.IoBridge.connect(IoBridge.java:118)
09-12 12:32:05.888: W/System.err(11103): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
09-12 12:32:05.888: W/System.err(11103): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
09-12 12:32:05.888: W/System.err(11103): at java.net.Socket.connect(Socket.java:849)
09-12 12:32:05.888: W/System.err(11103): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
09-12 12:32:05.888: W/System.err(11103): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:154)
09-12 12:32:05.888: W/System.err(11103): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
09-12 12:32:05.888: W/System.err(11103): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
09-12 12:32:05.888: W/System.err(11103): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
09-12 12:32:05.888: W/System.err(11103): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:580)
09-12 12:32:05.888: W/System.err(11103): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:512)
09-12 12:32:05.888: W/System.err(11103): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:490)
09-12 12:32:05.888: W/System.err(11103): at com.dwaik.jsonparser.JSONParser.getJSONObject(JSONParser.java:72)
它指出我的例外是response = httpclient.execute(httppost);
我获得了互联网许可,访问服务器实际上我可以在同一个应用程序中从它下载文件。
任何的想法?