-6

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);
我获得了互联网许可,访问服务器实际上我可以在同一个应用程序中从它下载文件。
任何的想法?

4

2 回答 2

3

不允许使用主 (UI) 线程进行网络操作。

有几种使用后台线程的方法。我会推荐使用AsyncTask,因为它的结构很好,很容易理解。

http://developer.android.com/reference/android/os/AsyncTask.html

这也是一个 SO 示例:AsyncTask Android 示例

应该在 Google 上提供更多。

于 2013-09-12T09:44:49.207 回答
2
Android NetworkOnMainThreadException

因为你在 n/w operations表演main Ui thread

使用 AsyncTaskThread

AsyncTask: 文档

class myAsync extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub

                // Keep in mind You Can't Update UI Part here

              JSONObject jObject = (new JSONParser(this))
                .getJSONObject("http://192.168.0.81:8080/content/test.json");
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
                // Handle/Update UI Part
        }
    }

像这样称呼它

new myAsync().execute();
于 2013-09-12T09:44:47.977 回答