令牌“extends”上的语法错误,抛出预期 - 实现AsyncTask时

时间:2013-12-03 15:39:53

标签: java android android-asynctask

我正在尝试将asynctask实现到我现有的源代码中,但是我收到一条错误,指出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
          }
}

1 个答案:

答案 0 :(得分:2)

您的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) {

}
}