Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 标记“”上的语法错误;扩展;,实现AsyncTask时引发预期的错误_Java_Android_Android Asynctask - Fatal编程技术网

Java 标记“”上的语法错误;扩展;,实现AsyncTask时引发预期的错误

Java 标记“”上的语法错误;扩展;,实现AsyncTask时引发预期的错误,java,android,android-asynctask,Java,Android,Android Asynctask,我试图在现有的源代码中实现asynctask,但在标记“extends”上出现语法错误,并抛出预期的 如果我改变 public void run() extends AsyncTask { 到 正如eclipse似乎要求我做的那样,我最终得到了: No exception of type AsyncTask can be thrown; an exception type must be a subclass of Throwable 我只需要找出我在asyncTask实现中做错了什么 JA

我试图在现有的源代码中实现asynctask,但在标记“extends”上出现语法错误,并抛出预期的

如果我改变

public void run() extends AsyncTask {

正如eclipse似乎要求我做的那样,我最终得到了:

No exception of type AsyncTask can be thrown; an exception type must be a subclass of Throwable
我只需要找出我在asyncTask实现中做错了什么

JAVA
公共类getyoutubeuserviceostask实现可运行{
公共静态最终字符串LIBRARY=“LIBRARY”;
私人最终处理人回复;
私有最终字符串用户名;
公共GetYouTubeUserVideoStatsk(处理程序replyTo,字符串用户名){
this.replyTo=replyTo;
this.username=用户名;
}
@凌驾
public void run()扩展了异步任务{
试一试{
HttpClient=new DefaultHttpClient();
HttpUriRequest请求=新的HttpGet(“https://gdata.youtube.com/feeds/api/videos?author=“+username+”&v=2&alt=jsonc”);
HttpResponse response=client.execute(请求);
字符串jsonString=StreamUtils.convertToString(response.getEntity().getContent());
JSONObject json=新的JSONObject(jsonString);
JSONArray JSONArray=json.getJSONObject(“数据”).getJSONArray(“项目”);
列表视频=新建ArrayList();
for(int i=0;i
您的asyncTask应该如下所示,类应该扩展
asyncTask
而不是实现
可运行的

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

}
}
public final类getyoutubeuservicedestask扩展异步任务{
公共静态最终字符串LIBRARY=“LIBRARY”;
私人最终处理人回复;
私有最终字符串用户名;
公共GetYouTubeUserVideoStatsk(处理程序replyTo,字符串用户名){
this.replyTo=replyTo;
this.username=用户名;
}
/* 
*@see android.os.AsyncTask#doInBackground(Params[])
*/
@凌驾
受保护的Void doInBackground(Void…arg0){
试一试{
HttpClient=new DefaultHttpClient();
HttpUriRequest请求=新的HttpGet(“https://gdata.youtube.com/feeds/api/videos?author=“+username+”&v=2&alt=jsonc”);
HttpResponse response=client.execute(请求);
字符串jsonString=StreamUtils.convertToString(response.getEntity().getContent());
JSONObject json=新的JSONObject(jsonString);
JSONArray JSONArray=json.getJSONObject(“数据”).getJSONArray(“项目”);
列表视频=新建ArrayList();
for(int i=0;i
谢谢!我即将对其进行测试-但将run方法更改为doInBackground会在另一个引用run()的类中导致错误。在本例中,我如何引用doInBackground而不是run()(这样我就可以测试上面的方法)new getyoutubeuservicedestask(responseHandler,“blundelp”).run();您应该如下调用它:
newgetyoutubeuservicedestask(responseHandler,“blundelp”).execute()
这将自动调用
doInBackground
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
          }
}
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) {

}
}