java.lang.ClassCastException: com.google.gdata.data.TextContent无法转换为com.google.gdata.data.OutOfLineContent

10

我希望能在工作表中将所有单元格滑动,但我做不到。

我的代码是:

SpreadsheetService service = new SpreadsheetService("MyApp");
   try{
       URL SPREADSHEET_URL = new URL("https://spreadsheets.google.com/feeds/worksheets/1-8ATDLTqmzo4QCQijeJ_swZAcmsh/public/full");
       SpreadsheetFeed feed = service.getFeed(SPREADSHEET_URL,SpreadsheetFeed.class);
       List<SpreadsheetEntry> spreadsheets = feed.getEntries();
       if (spreadsheets.size() == 0){
           System.out.println("NO SPREADSHEET");
       }

      for(int i = 0; i<spreadsheets.size(); i++){
          System.out.println(spreadsheets.get(i).getTitle().getPlainText());
      }
      List<WorksheetEntry> worksheets = spreadsheets.get(0).getWorksheets();
      for (int j=0; j<worksheets.size(); j++){
          System.out.println(worksheets.get(j).getTitle().getPlainText());
          URL listFeedUrl = worksheets.get(j).getListFeedUrl();
          ListFeed listFeed = service.getFeed(listFeedUrl,ListFeed.class);

      }

错误报告在最后一行:

ListFeed listFeed = service.getFeed(listFeedUrl,ListFeed.class);

当我编译我的代码时,出现了以下错误:

Exception in thread "main" java.lang.ClassCastException: com.google.gdata.data.TextContent cannot be cast to com.google.gdata.data.OutOfLineContent
at com.google.gdata.data.spreadsheet.WorksheetEntry.getFeedUrlString(WorksheetEntry.java:129)
at com.google.gdata.data.spreadsheet.WorksheetEntry.getListFeedUrl(WorksheetEntry.java:98)
at it.unical.mat.google_data.MySpreadsheetIntegration.main(MySpreadsheetIntegration.java:40)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

使用的导入:

import android.support.multidex.MultiDex;
import com.google.gdata.client.authn.oauth.*;
import com.google.gdata.client.spreadsheet.*;
import com.google.gdata.data.*;
import com.google.gdata.data.batch.*;
import com.google.gdata.data.spreadsheet.*;
import com.google.gdata.util.*;
import java.io.IOException;
import java.net.*;
import java.util.*;
import java.util.jar.Attributes;

请附上您的错误日志和导入语句。 - Rahul Chaudhary
@RahulChaudhary,请查看我的编辑。 - mc_marad
实际上,在WorksheetEntry的实现中,您可以在第129行看到一个向下转型:((OutOfLineContent)(this.getContent())).getUri()。这是不允许的,这就是为什么会出现ClassCastException的原因。 - Stefano Vuerich
这是Google Sheets文档中提到的相同代码,有没有其他可替代的代码来完成这个任务? - Ashish Awasthi
谢谢您的帮助,但是在我的代码上没有起作用。 - Venki WAR
显示剩余3条评论
1个回答

2

下面提到了两种可能的解决方案

1. 如果您查看调试控制台,可以看到Intellij Ide调用反射的作用。使用其他IDE(可能是eclipse)进行此执行可能会有可能性。从方法com.google.gdata.data.spreadsheet.WorksheetEntry.getFeedUrlString(WorksheetEntry.java:129中搜索出造成异常的确切代码部分。

    private String getFeedUrlString(String linkRelKind) {
    Version spreadsheetVersion = state.service.getProtocolVersion();

    if (spreadsheetVersion.isCompatible(SpreadsheetService.Versions.V1)) {
      Link feedLink = this.getLink(linkRelKind, Link.Type.ATOM);
      return feedLink.getHref();
    } else { // must be SpreadsheetService.Versions.V2; only 2 versions for now
      // List or Cells feed Url?
      if (linkRelKind.equals(Namespaces.LIST_LINK_REL)) {
        // the list feed is stored as a <content> tag
        return ((OutOfLineContent)(this.getContent())).getUri();
      } else { // it must be Namespaces.CELLS_LINK_REL
        // the cells feed is stored in the <link> tag
        Link feedLink = this.getLink(linkRelKind, Link.Type.ATOM);
        return feedLink.getHref();
      }
    }
  }

下一个可能的解决方案可能是,在编译期间:
ListFeed listFeed = service.getFeed(listFeedUrl,ListFeed.class);

在上述代码中,您正在尝试保存通过服务从特定feed URL获取的Feed对象。

只需为从URL获取列表提供的try catch块,并在可能的情况下处理异常。

    try{
    service.getFeed(listFeedUrl,ListFeed.class);
    }
    catch(IOException e){
e.printStackTrace();
}
catch(ParseException e1){
e1.printStackTrace();
}
catch(ResourceNotFoundException e2){
e2.printStackTrace();
}
catch(ServiceException e3){
e3.printStackTrace();
}

请调试出该行的内联值。 希望这能对您有所帮助。 谢谢。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接