异步任务执行doInBackground()时发生错误?

时间:2017-02-24 07:39:49

标签: java android android-asynctask

显示Async Task时出错。执行doInBackground()时出错  虽然我试图运行这个我使用了最小的姜饼SDK 这是异步任务代码

 private class TitleAsynctask extends AsyncTask<URL,Void,List<view>>{





     @Override
        public List<view> doInBackground(URL... urls){

            URL url = Query_utils.createurl(POEM_TITLE);
            String json = "";
            Log.d(LOG_TAG,"this worked");
            {
                try {
                    json = Query_utils.makehttprequest(url);
                    Log.d(LOG_TAG, "make Httprequest works");
                } catch (IOException e) {

                }
            }
            List<view> title_view = Query_utils.extracttitlefromjson(json);
            return title_view;

        }
        @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
        @Override
        protected void onPostExecute(List<view> data) {
            madapter.clear();


            if (data != null && !data.isEmpty()){
                madapter.addAll(data);
            }
        }


and the code for http request is 

     public static String makehttprequest(URL url) throws IOException {
            String jsonResponse = "";
            if (url == null) {
                return jsonResponse;
            }

            HttpURLConnection urlConnection = null;
            InputStream inputStream = null;
            try {
                urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setReadTimeout(10000 /* milliseconds */);
                urlConnection.setConnectTimeout(15000 /* milliseconds */);
                urlConnection.setRequestMethod("GET");
                urlConnection.connect();

                if (urlConnection.getResponseCode() == 200) {
                    inputStream = urlConnection.getInputStream();
                    jsonResponse = readInputfromStraeam(inputStream);
                } else {
                    Log.e(LOG_TAG, "Error in connection!! Bad Response ");
                }

            } catch (IOException e) {
                Log.e(LOG_TAG, "Problem retrieving the earthquake JSON results.", e);
            } finally {
                if (urlConnection == null) {
                    urlConnection.disconnect();
                }
                if (inputStream == null) {
                    inputStream.close();
                }
            }
            Log.d(LOG_TAG," " + jsonResponse);

            return jsonResponse;
        }

和来自流的读取输入

public static String readInputfromStraeam (InputStream inputStream) throws IOException{

            StringBuilder output = new StringBuilder();

            if(inputStream!=null){
                InputStreamReader reader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
                BufferedReader bufferedReader = new BufferedReader(reader);
                String Line = bufferedReader.readLine();
                while (Line!=null){

                    output.append(Line);
                    Line =bufferedReader.readLine();
                }

            }
            return output.toString();
        }

错误是

java.lang.RuntimeException: An error occured while executing doInBackground()
                      at android.os.AsyncTask$3.done(AsyncTask.java:304)
                      at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
                      at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
                      at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                      at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
                      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                      at java.lang.Thread.run(Thread.java:818)
                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.InputStream.close()' on a null object reference
                      at com.example.ashura.poemtest.Query_utils.makehttprequest(Query_utils.java:105)
                      at com.example.ashura.poemtest.MainActivity$TitleAsynctask.doInBackground(MainActivity.java:62)
                      at com.example.ashura.poemtest.MainActivity$TitleAsynctask.doInBackground(MainActivity.java:52)
                      at android.os.AsyncTask$2.call(AsyncTask.java:292)
                      at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                      at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
                      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
                      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
                      at java.lang.Thread.run(Thread.java:818)

1 个答案:

答案 0 :(得分:2)

你的问题在这里

            if (inputStream == null) {
                inputStream.close();
            }

如果inputStreamnull,则您尝试拨打不存在的close()。如果close不是inputStream,则只需null,因此改进将类似于

            if (inputStream != null) {
                inputStream.close();
            }