onPostExecute没有收到字符串

时间:2017-10-09 07:30:15

标签: android json

我在Android Studio中有一个非常基本的程序,它从一个网站获取一个Json-Object(" https://jsonparsingdemo-cec5b.firebaseapp.com/jsonData/moviesDemoItem.txt"),只是在屏幕上显示它,而不解析它。 Json对象是正确传输的,但在onPostExecute中,String结果似乎为null,而Log.e(" setText")抛出了NullPointerException,而Log.e(" String&#34) ;)是正确的,既不抛出IOException也不抛出MalformedURLException:

     try {
            url = new URL("https://jsonparsingdemo-cec5b.firebaseapp.com/jsonData/moviesDemoItem.txt");
            connection = (HttpURLConnection) url.openConnection();
            connection.connect();

            InputStream stream = connection.getInputStream();

            reader = new BufferedReader(new InputStreamReader(stream));
            Log.e("TRY", "trybracket");
            StringBuffer buffer = new StringBuffer();

            String line = "";
            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }

            String result = buffer.toString();
            Log.e("String", result);
            return result;

        } catch (MalformedURLException e) {
            e.printStackTrace();
            Log.e("MUE", "Malformed");
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("IOE", "IOException");
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;

        }
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        tvData.setText(result);
        Log.e("setText", result);
    }
}

这也是我的Logcat:

E/TRY: trybracket
E/String: {  "movies": [{"movie": "Avengers","year": 2012}]}
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main 
              Process com.stasiak.jsonparsing, PID: 20061
              java.lang.NullPointerException: println needs a message
                    at android.util.Log.println_native(Native Method)
                    at android.util.Log.e(Log.java:238)
                    at com.stasiak.jsonparsing.MainActivity$JSONTask.onPostExecute(MainActivity.java:96)
                    at com.stasiak.jsonparsing.MainActivity$JSONTask.onPostExecute(MainActivity.java:39)
                    at android.os.AsyncTask.finish(AsyncTask.java:695)
                    at android.os.AsyncTask.-wrap1(Unknown Source:0)
                    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:712)
                    at android.os.Handler.dispatchMessage(Handler.java:105)
                    at android.os.Looper.loop(Looper.java:164)
                    at android.app.ActivityThread.main(ActivityThread.java:6541)<1 internal calls>
                    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

2 个答案:

答案 0 :(得分:1)

您将在null块中返回finally。将return null;移到外面最后阻止

答案 1 :(得分:0)

最后是将始终执行的块(除非system.exit()将完成)。这就是为什么它总是空的。

提示: - 总是在log的第二个参数中使用" ",否则它会在null的情况下抛出异常,比如.. message不应该为null等。

Log.e("String", " "+result);