gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 19:21:11 24 4
gpt4 key购买 nike

I got an error displaying Async Task An error occurred while execution doInBackground(); 当我尝试运行它时,我使用了最小的 Gingerbread 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)

最佳答案

你的问题在这里

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

如果inputStreamnull,那么您尝试调用不存在的close()。如果 inputStream 不为 null,您只需要关闭它,因此改进类似于

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

关于java - 异步任务执行 doInBackground() 时发生错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42433163/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com