Android异步任务错误实现摘要
我试图理解AsyncTask是如何工作的。我有以下代码,但我得到以下错误:类'AsyncTask'必须声明为抽象的或实现抽象方法'doInBackground(Params…)Android异步任务错误实现摘要,android,android-asynctask,Android,Android Asynctask,我试图理解AsyncTask是如何工作的。我有以下代码,但我得到以下错误:类'AsyncTask'必须声明为抽象的或实现抽象方法'doInBackground(Params…) 私有类WeatherDataTask扩展了AsyncTask{ 受保护的字符串doInBackgrond()引发异常{ HttpURLConnection=null; BufferedReader reader=null; 字符串数据=null; 试一试{ URL=新URL(“http://api.openweather
私有类WeatherDataTask扩展了AsyncTask{
受保护的字符串doInBackgrond()引发异常{
HttpURLConnection=null;
BufferedReader reader=null;
字符串数据=null;
试一试{
URL=新URL(“http://api.openweathermap.org/data/2.5/weather?zip=90210,美制单位=英制单位);
connection=(HttpURLConnection)url.openConnection();
connection.setRequestMethod(“GET”);
connection.connect();
InputStream=connection.getInputStream();
StringBuffer=新的StringBuffer();
reader=新的BufferedReader(新的InputStreamReader(流));
弦线;
而((line=reader.readLine())!=null){
buffer.append(第+行“\n”);
}
if(buffer.length()==0){
数据=空;
}
返回数据=buffer.toString();
}捕获(IOE异常){
Log.e(“占位符片段”,“错误”,e);
数据=空;
返回数据;
}最后{
if(连接!=null){
连接断开();
}
if(读卡器!=null){
试一试{
reader.close();
}捕获(最终IOE例外){
Log.e(“占位符片段”,“错误关闭流”,e);
}
}
}
}
受保护的void onPostExecute(字符串jsonParse){
if(jsonParse==null){
TextView进度=(TextView)findViewById(R.id.container);
progress.setText(“无法获取天气”);
}
否则{
试一试{
JSONObject jsonRootObject=新的JSONObject(jsonParse);
int temp=Integer.parseInt(jsonRootObject.getJSONObject(“main”).getString(“temp”);
TextView displayTemp=(TextView)findViewById(R.id.container);
displayTemp.setText(温度);
}捕获(JSONException e){
e、 printStackTrace();
}
}
}
}
我只是想得到温度并更新文本视图。谢谢您只需要将
doInBackgrond()抛出异常
更改为doInBackground(Void…params)
添加@Override到doInBackground方法,当您扩展AsyncTask时,至少必须重写doInBackground方法。
移除投掷。doInBackground不是你的“doInBackground”打字错误
private class TestTask extends AsyncTask<String,Integer,Boolean>{
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(Boolean aBoolean) {
super.onPostExecute(aBoolean);
}
@Override
protected Boolean doInBackground(String... params) throws Exception{
return null;
}
}
请注意,它不会引发异常,因此在其中包含“throws exception”意味着您没有重写超类方法并与其发生冲突。谢谢。我试过了,但还是有同样的错误。我还需要重写该方法吗?也许您还需要从
doInBackgrond()
中删除throws Exception
。。而且您有输入错误:doInBackgrond()
而不是doInBackground()
@Override表示法是可选的doInBackground不能从方法本身引发异常。当我添加@Override时,我得到错误:“方法不从其超类重写”
private class TestTask extends AsyncTask<String,Integer,Boolean>{
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(Boolean aBoolean) {
super.onPostExecute(aBoolean);
}
@Override
protected Boolean doInBackground(String... params) throws Exception{
return null;
}
}
/**
* Override this method to perform a computation on a background thread. The
* specified parameters are the parameters passed to {@link #execute}
* by the caller of this task.
*
* This method can call {@link #publishProgress} to publish updates
* on the UI thread.
*
* @param params The parameters of the task.
*
* @return A result, defined by the subclass of this task.
*
* @see #onPreExecute()
* @see #onPostExecute
* @see #publishProgress
*/
protected abstract Result doInBackground(Params... params);