9
String response = getResultForRequest(url);

其中 'url' 是 JSON 格式,使用 http GET 方法返回一堆数据。

public static String getResultForRequest(String urlString)
        throws IOException {
    URL url = new URL(urlString);
    HttpURLConnection urlConnection = (HttpURLConnection) url
            .openConnection();
    urlConnection.setRequestMethod("GET");
    urlConnection.setDoInput(true);
    urlConnection.setDoOutput(true);
    urlConnection.connect();

    InputStream is = urlConnection.getInputStream();
    if (is == null)
        return null;
    StringBuffer sb = new StringBuffer();
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    String line = null;
    try {
        while ((line = br.readLine()) != null)
            sb.append(line);
    } finally {
        br.close();
        is.close();
    }

    return sb.toString();
}

我无法从 getResultForRequest(url) 方法中传递的“url”获取 JSON 格式的数据。在urlConnection.connect() 中出现错误;. AndroidManifest.xml 文件中也提供了 Internet 权限。

这是我的日志。

10-09 13:27:35.264: E/AndroidRuntime(9984): FATAL EXCEPTION: main
10-09 13:27:35.264: E/AndroidRuntime(9984): java.lang.RuntimeException: Unable to start activity ComponentInfo{}: android.os.NetworkOnMainThreadException
10-09 13:27:35.264: E/AndroidRuntime(9984):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
10-09 13:27:35.264: E/AndroidRuntime(9984):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
10-09 13:27:35.264: E/AndroidRuntime(9984):     at android.app.ActivityThread.access$600(ActivityThread.java:140)
10-09 13:27:35.264: E/AndroidRuntime(9984):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
10-09 13:27:35.264: E/AndroidRuntime(9984):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-09 13:27:35.264: E/AndroidRuntime(9984):     at android.os.Looper.loop(Looper.java:137)
10-09 13:27:35.264: E/AndroidRuntime(9984):     at android.app.ActivityThread.main(ActivityThread.java:4898)
10-09 13:27:35.264: E/AndroidRuntime(9984):     at java.lang.reflect.Method.invokeNative(Native Method)
10-09 13:27:35.264: E/AndroidRuntime(9984):     at java.lang.reflect.Method.invoke(Method.java:511)
10-09 13:27:35.264: E/AndroidRuntime(9984):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
10-09 13:27:35.264: E/AndroidRuntime(9984):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
10-09 13:27:35.264: E/AndroidRuntime(9984):     at dalvik.system.NativeStart.main(Native Method)
10-09 13:27:35.264: E/AndroidRuntime(9984): Caused by: android.os.NetworkOnMainThreadException
10-09 13:27:35.264: E/AndroidRuntime(9984):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
10-09 13:27:35.264: E/AndroidRuntime(9984):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
10-09 13:27:35.264: E/AndroidRuntime(9984):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
10-09 13:27:35.264: E/AndroidRuntime(9984):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
10-09 13:27:35.264: E/AndroidRuntime(9984):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
10-09 13:27:35.264: E/AndroidRuntime(9984):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
10-09 13:27:35.264: E/AndroidRuntime(9984):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
10-09 13:27:35.264: E/AndroidRuntime(9984):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
10-09 13:27:35.264: E/AndroidRuntime(9984):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
10-09 13:27:35.264: E/AndroidRuntime(9984):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
10-09 13:27:35.264: E/AndroidRuntime(9984):     at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:461)
10-09 13:27:35.264: E/AndroidRuntime(9984):     at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:433)
10-09 13:27:35.264: E/AndroidRuntime(9984):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
10-09 13:27:35.264: E/AndroidRuntime(9984):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
10-09 13:27:35.264: E/AndroidRuntime(9984):     at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
10-09 13:27:35.264: E/AndroidRuntime(9984):     at libcore.net.http.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:165)
10-09 13:27:35.264: E/AndroidRuntime(9984):     at com.csoft.foursquare.FoursquareService.getResultForRequest(Service.java:564)
10-09 13:27:35.264: E/AndroidRuntime(9984):     at com.csoft.foursquare.FoursquareService.getUserDetails(Service.java:376)
10-09 13:27:35.264: E/AndroidRuntime(9984):     at com.csoft.checkin.CheckinHistoryActivity.onCreate(HistoryActivity.java:52)
10-09 13:27:35.264: E/AndroidRuntime(9984):     at android.app.Activity.performCreate(Activity.java:5206)
10-09 13:27:35.264: E/AndroidRuntime(9984):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
10-09 13:27:35.264: E/AndroidRuntime(9984):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
10-09 13:27:35.264: E/AndroidRuntime(9984):     ... 11 more

提前致谢。

4

3 回答 3

32

将此添加到您的 onCreate() 中:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

使用上述作为临时解决方案。否则使用线程或异步任务。

于 2013-10-09T10:42:41.460 回答
13

您正在尝试在主 (UI) 线程上建立网络连接。这在 Android 中是不允许的,因为它会暂停整个 UI,直到您从服务器获得响应。

尝试使用 AsyncTask,或在单独的线程上执行连接。

希望这可以帮助。

于 2013-10-09T08:20:02.127 回答
3

NetworkOnMainThreadException:当应用程序尝试在其主线程上执行网络操作时引发的异常。

您应该在 asynctask 上调用方法,然后只有代码才能工作。为避免它,您应该在另一个线程上调用它。因此 asynctask 更好。

http://android-developers.blogspot.in/2009/05/painless-threading.html

http://android-er.blogspot.in/2012/04/androidosnetworkonmainthreadexception.html

http://www.lucazanini.eu/2012/android/the-android-os-networkonmainthreadexception-exception/?lang=en

这是说明如何使用 asynctask 的链接

于 2013-10-09T08:33:28.667 回答