gpt4 book ai didi

java - token "extends"上的语法错误,预期会抛出 - 实现 AsyncTask 时

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

我正在尝试在我现有的源代码中实现异步任务,但我收到一条错误消息,指出 Syntax error on token "extends", throws expected

如果我改变

public void run() extends AsyncTask {

public void run() throws AsyncTask {

因为 eclipse 似乎要求我这样做 - 我最终得到:

No exception of type AsyncTask can be thrown; an exception type must be a subclass of Throwable

我只需要弄清楚我在 asyncTask 实现中做错了什么。

Java

public class GetYouTubeUserVideosTask implements Runnable {

public static final String LIBRARY = "Library";
private final Handler replyTo;
private final String username;

public GetYouTubeUserVideosTask(Handler replyTo, String username) {
this.replyTo = replyTo;
this.username = username;
}

@Override
public void run() extends AsyncTask {
try {

HttpClient client = new DefaultHttpClient();

HttpUriRequest request = new HttpGet("https://gdata.youtube.com/feeds/api/videos?author="+username+"&v=2&alt=jsonc");

HttpResponse response = client.execute(request);

String jsonString = StreamUtils.convertToString(response.getEntity().getContent());

JSONObject json = new JSONObject(jsonString);
JSONArray jsonArray = json.getJSONObject("data").getJSONArray("items");


List<Video> videos = new ArrayList<Video>();

for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);

String title = jsonObject.getString("title");
String url;
try {
url = jsonObject.getJSONObject("player").getString("mobile");
} catch (JSONException ignore) {
url = jsonObject.getJSONObject("player").getString("default");
}
String thumbUrl = jsonObject.getJSONObject("thumbnail").getString("sqDefault");


videos.add(new Video(title, url, thumbUrl));
}

Library lib = new Library(username, videos);

Bundle data = new Bundle();
data.putSerializable(LIBRARY, lib);


Message msg = Message.obtain();
msg.setData(data);
replyTo.sendMessage(msg);

} catch (ClientProtocolException e) {
Log.e("Feck", e);
} catch (IOException e) {
Log.e("Feck", e);
} catch (JSONException e) {
Log.e("Feck", e);
}
}
protected void onPostExecute(String result) {
// do something
}

protected void onPreExecute() {
// do something
}
protected void onProgressUpdate(String... text) {
// do something
}
}

最佳答案

您的 asyncTask 应该像下面这样,该类应该扩展 AsyncTask 而不是实现 Runnable:

public final class GetYouTubeUserVideosTask extends AsyncTask<Void, Void, Void> {

public static final String LIBRARY = "Library";
private final Handler replyTo;
private final String username;

public GetYouTubeUserVideosTask(Handler replyTo, String username) {
this.replyTo = replyTo;
this.username = username;
}
/*
* @see android.os.AsyncTask#doInBackground(Params[])
*/
@Override
protected Void doInBackground(Void... arg0) {
try {

HttpClient client = new DefaultHttpClient();

HttpUriRequest request = new HttpGet("https://gdata.youtube.com/feeds/api/videos?author="+username+"&v=2&alt=jsonc");

HttpResponse response = client.execute(request);

String jsonString = StreamUtils.convertToString(response.getEntity().getContent());

JSONObject json = new JSONObject(jsonString);
JSONArray jsonArray = json.getJSONObject("data").getJSONArray("items");


List<Video> videos = new ArrayList<Video>();

for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);

String title = jsonObject.getString("title");
String url;
try {
url = jsonObject.getJSONObject("player").getString("mobile");
} catch (JSONException ignore) {
url = jsonObject.getJSONObject("player").getString("default");
}
String thumbUrl = jsonObject.getJSONObject("thumbnail").getString("sqDefault");


videos.add(new Video(title, url, thumbUrl));
}

Library lib = new Library(username, videos);

Bundle data = new Bundle();
data.putSerializable(LIBRARY, lib);


Message msg = Message.obtain();
msg.setData(data);
replyTo.sendMessage(msg);

} catch (ClientProtocolException e) {
Log.e("Feck", e);
} catch (IOException e) {
Log.e("Feck", e);
} catch (JSONException e) {
Log.e("Feck", e);
}
return null;
}

/*
* @see android.os.AsyncTask#onPostExecute(java.lang.Object)
*/
@Override
protected void onPostExecute(Void result) {

}
}

关于java - token "extends"上的语法错误,预期会抛出 - 实现 AsyncTask 时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20355389/

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