4

我已经试图弄清楚这几个小时了。我已经搜索了答案,但我找不到答案。我以前从来没有费心问过,这是我第一次问问题。基本上我正在做的是为了组织目的而分解我的编码。下面的代码片段工作得很好,但是当我把它放到另一个类中时, urlConnect(); 连接得很好。我在下面做了标记。

public String downloadUrl(String strUrl) throws IOException{
    String data = "";
    InputStream iStream = null;
    HttpURLConnection urlConnection = null;

    try{
        URL url = new URL(strUrl);
        String line = "";

        // Creating an http connection to communicate with url 
        urlConnection = (HttpURLConnection) url.openConnection();

        /* Connecting to url */
        urlConnection.connect(); <-------------------------works in this snippet

        /* Reading data from url */
        iStream = urlConnection.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
        StringBuffer sb  = new StringBuffer();

        while((line = br.readLine()) != null){
            sb.append(line);
        }
        data = sb.toString();
        br.close();
    }
    catch(Exception e){
        Log.d("Exception while downloading url", e.toString());
    }
    finally {
        iStream.close();
        urlConnection.disconnect();
    }
    return data;
}

因此,以下代码段几乎相同。但由于某种原因,它不想连接:

public String getJSONobject(String strUrl) throws IOException{
    String data = "";
    InputStream iStream = null;
    HttpURLConnection urlConnection = null;

    try{
        URL url = new URL(strUrl);
        String line = "";
        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.connect();    <------------------ Does not work

        iStream = urlConnection.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
        StringBuffer sb  = new StringBuffer();

        while((line = br.readLine()) != null){
            sb.append(line);
        }
        data = sb.toString();
        br.close();
        iStream.close();    
    }               
    catch (MalformedURLException e1) {
        e1.printStackTrace();
    } 
    catch (IOException e1) {
        e1.printStackTrace();
    } 
    finally {
        if(null != urlConnection) { urlConnection.disconnect(); 
    }
    return data;

日志猫:

>at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1131)
>at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
>at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
>at java.net.InetAddress.getAllByName(InetAddress.java:214)
>at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
>at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
>at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
>at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
>at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
>at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
>at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:461)
>at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:433)
>at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
>at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
>at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
>at libcore.net.http.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:165)
>at com.navsquad.shsu.katlas2.RouteJSONobject.getJSONobject(RouteJSONobject.java:57)
>at com.navsquad.shsu.katlas2.CreateRoute.create(CreateRoute.java:41)
>at com.navsquad.shsu.katlas2.MainActivity$3.onMapClick(MainActivity.java:84)
>at com.google.android.gms.maps.GoogleMap$6.onMapClick(Unknown Source)
>at com.google.android.gms.internal.t$a.onTransact(Unknown Source)
>at android.os.Binder.transact(Binder.java:326)
>at com.google.android.gms.maps.internal.IOnMapClickListener$Stub$Proxy.onMapClick(IOnMapClickListener.java:93)
>at maps.i.s.b(Unknown Source)
>at maps.y.v.c(Unknown Source)
>at maps.y.bf.onSingleTapConfirmed(Unknown Source)
>at maps.d.v.onSingleTapConfirmed(Unknown Source)
>at maps.d.j.handleMessage(Unknown Source)
>at android.os.Handler.dispatchMessage(Handler.java:99)
>at android.os.Looper.loop(Looper.java:137)
>at android.app.ActivityThread.main(ActivityThread.java:5059)
>at java.lang.reflect.Method.invokeNative(Native Method)
>at java.lang.reflect.Method.invoke(Method.java:511)
>at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
>at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
>at dalvik.system.NativeStart.main(Native Method)

所以再次澄清一下,我所做的只是为我自己的组织开设一个单独的课程。最初我有一个解析器,但我现在将它分成不同的类,所以我可以将其他解析器与字符串/JSON 对象一起使用。如果您能帮助我弄清楚与以前有什么不同,我将不胜感激。原始类在一个已实现的 AsyncTask 类中。

4

1 回答 1

7
android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1131)

您似乎试图在应用程序的 UI 线程上调用阻塞网络 API。

在 Android 3.0 及以上版本中,有一个新的应用程序策略不允许在主线程上执行网络调用。

请通过http://developer.android.com/reference/android/os/StrictMode.html

您可以通过放置以下代码片段来禁用严格模式。(我不会在生产环境中推荐这种方法。)

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
于 2013-04-21T05:09:32.473 回答