试图使用api; bufferedinputstream导致崩溃?

时间:2015-10-18 18:53:54

标签: android api inputstream

我现在正试图使用​​API​​;第一次使用API​​,第一次尝试在Android上做某事,所以我不确定我在做什么。

首先,我有这个:

InputStream in = null;


        try {

            URL url = new URL(urlGetTerm);


            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();


            in = new BufferedInputStream(urlConnection.getInputStream());


        } catch (Exception e ) {

            System.out.println(e.getMessage());

            return lst;

        }

我发现它在in = newBufferedInputStream(url.Connection.getInputStream())中崩溃了; 将日志消息放到各处后。

为什么会崩溃?当我连接'使用urlConnection到API网址,不应该让getInputStream给我json响应吗?正确访问urlGetTerm的url会将我带到一个带有json响应的页面,因此实际的url没有任何问题。

日志:

10-18 14:58:20.356  30708-30708/com.example.naomikoo.classalert E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.naomikoo.classalert/com.example.naomikoo.classalert.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5039)
            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:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.android.internal.os.LoggingPrintStream.println(LoggingPrintStream.java:298)
            at com.example.naomikoo.classalert.uwApi.getResult(uwApi.java:50)
            at com.example.naomikoo.classalert.MainActivity.onCreate(MainActivity.java:75)
            at android.app.Activity.performCreate(Activity.java:5104)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5039)
            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:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

删除System.out.println(e.getMessage());e.getMessage()null

您无法在主线程上进行网络操作 当应用程序尝试在其主线程上执行网络操作时,将抛出异常(NetworkOnMainThreadException)。

使用以下代码来避免此错误,或者在线程或AsyncTask中调用您的网络操作。

    if (android.os.Build.VERSION.SDK_INT > 9)
    {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }