尝试从 Android 连接到 Google 图书

发布于 2024-12-13 15:04:56 字数 10738 浏览 0 评论 0原文

让我们从顶部开始。我正在尝试创建一个应用程序,该应用程序采用 ISBN,然后连接到 Google 图书,并从那里获取有关该书的信息。我能够使用纯java来做到这一点:

import java.net.URL;
import com.google.gdata.client.books.BooksService;
import com.google.gdata.client.books.VolumeQuery;
import com.google.gdata.data.books.VolumeEntry;
import com.google.gdata.data.books.VolumeFeed;
public class books 
{
public static void main(String[] args)
{       
    ////////////////////////////////////////////////
    try
    {                               
        BooksService booksService = new BooksService("UAH");
        String isbn = "9780262140874";
        URL url = new URL("http://www.google.com/books/feeds/volumes/?q=ISBN%3C" + isbn + "%3E");
        VolumeQuery volumeQuery = new VolumeQuery(url);
        VolumeFeed volumeFeed = booksService.query(volumeQuery, VolumeFeed.class);
        VolumeEntry bookInfo=volumeFeed.getEntries().get(0);

        System.out.println("Title: " + bookInfo.getTitles().get(0));
        System.out.println("Id: " + bookInfo.getId());
        System.out.println("Authors: " + bookInfo.getCreators());
        System.out.println("Description: "+bookInfo.getDescriptions()+"\n");
    }catch(Exception ex){System.out.println(ex.getMessage());}
    /////////////////////////////////////////////////                           
 }}

我使用了 \gdata-src.java-1.46.0\gdata\java\lib 中的 jars 书籍和客户端以及来自 google-api-java-client-1.5.0- 的依赖项beta\dependency 并且它运行得很好。 当我尝试将其转换为 android 时,它给了我错误 “转换为 Dalvik 格式失败,错误 1”,因此无法编译。然后我尝试做 android 工具 ->修复项目,但仍然没有运气,我尝试更改构建目标中的级别。我已经清洗了100次了,还是没用。所以然后我决定删除 google-api-java-client-1.5.0-beta\dependency (导致错误的 jars),然后它会编译但崩溃,因为

11-02 20:44:27.093: WARN/System.err(485): java.lang.NoClassDefFoundError: com.google.gdata.client.books.BooksService

我的 android 代码如下:

import java.net.URL;

import com.google.gdata.client.books.BooksService;
import com.google.gdata.client.books.VolumeQuery;
import com.google.gdata.data.books.VolumeEntry;
import com.google.gdata.data.books.VolumeFeed;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AndroidBooksApiActivity extends Activity {
Button btnSearch;
TextView tvTitle,tvISBN,tvAuthor,tvDatePublished;
EditText etSearch;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    btnSearch=(Button)findViewById(R.id.btnFindBook);
    tvTitle=(TextView)findViewById(R.id.tvTitle);
    tvISBN=(TextView)findViewById(R.id.tvISBN);
    tvAuthor=(TextView)findViewById(R.id.tvAuthor);
    tvDatePublished=(TextView)findViewById(R.id.tvDatePublished);
    etSearch=(EditText)findViewById(R.id.etSearch);
    btnSearch.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            try
            {   
                //String isbn;
                //isbn=etSearch.getText().toString();
                String isbn = "9780262140874";          

                BooksService booksService = new BooksService("UAH");        

                URL url = new URL("http://www.google.com/books/feeds/volumes/?q=ISBN%3C" + isbn + "%3E");

                VolumeQuery volumeQuery = new VolumeQuery(url);

                VolumeFeed volumeFeed = booksService.query(volumeQuery, VolumeFeed.class);

                VolumeEntry bookInfo=volumeFeed.getEntries().get(0);

                tvTitle.setText("Title: " + bookInfo.getTitles().get(0));
                tvISBN.setText("ISBN: " + isbn);
                tvAuthor.setText("Authors: " + bookInfo.getCreators());
                tvDatePublished.setText("DatePublished: "+bookInfo.getDates()+"\n");
            }catch(Throwable ex){tvTitle.setText(ex.getMessage()); ex.printStackTrace();}

          }
    });
}

}

如果你还有更多我将尽我所能回答问题。我仍在学习,所以我的错误可能很简单,我只是没有看到。

    java.lang.IllegalArgumentException: already added:         Lcom/google/common/annotations/GwtCompatible;
[2011-11-03 14:25:51 - androidBooksApi] Dx  at com.android.dx.dex.file.ClassDefsSection.add(ClassDefsSection.java:123)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at com.android.dx.dex.file.DexFile.add(DexFile.java:143)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at com.android.dx.command.dexer.Main.processClass(Main.java:372)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at com.android.dx.command.dexer.Main.processFileBytes(Main.java:346)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at com.android.dx.command.dexer.Main.access$400(Main.java:59)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:294)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:244)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:130)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:108)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at com.android.dx.command.dexer.Main.processOne(Main.java:313)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at com.android.dx.command.dexer.Main.processAllFiles(Main.java:233)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at com.android.dx.command.dexer.Main.run(Main.java:185)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at java.lang.reflect.Method.invoke(Unknown Source)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at com.android.ide.eclipse.adt.internal.build.DexWrapper.run(DexWrapper.java:179)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at com.android.ide.eclipse.adt.internal.build.BuildHelper.executeDx(BuildHelper.java:652)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at com.android.ide.eclipse.adt.internal.build.builders.PostCompilerBuilder.build(PostCompilerBuilder.java:510)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at org.eclipse.core.internal.events.BuildManager$2.run(BuildManager.java:728)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:199)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:239)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at org.eclipse.core.internal.events.BuildManager$1.run(BuildManager.java:292)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:295)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at org.eclipse.core.internal.events.BuildManager.basicBuildLoop(BuildManager.java:351)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at org.eclipse.core.internal.events.BuildManager.build(BuildManager.java:374)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at org.eclipse.core.internal.events.AutoBuildJob.doBuild(AutoBuildJob.java:143)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at org.eclipse.core.internal.events.AutoBuildJob.run(AutoBuildJob.java:241)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)
[2011-11-03 14:25:53 - androidBooksApi] Dx warning: Ignoring InnerClasses attribute for an anonymous inner class
(org.joda.time.DateTimeZone$1) that doesn't come with an
associated EnclosingMethod attribute. This class was probably produced by a
compiler that did not target the modern .class file format. The recommended
solution is to recompile the class from source, using an up-to-date compiler
and without specifying any "-target" type options. The consequence of ignoring
this warning is that reflective operations on this class will incorrectly
indicate that it is *not* an inner class.
[2011-11-03 14:25:57 - androidBooksApi] Dx 
trouble processing "javax/transaction/HeuristicCommitException.class":

Ill-advised or mistaken usage of a core class (java.* or javax.*)
when not building a core library.

This is often due to inadvertently including a core library file
in your application's project, when using an IDE (such as
Eclipse). If you are sure you're not intentionally defining a
core class, then this is the most likely explanation of what's
going on.

However, you might actually be trying to define a class in a core
namespace, the source of which you may have taken, for example,
from a non-Android virtual machine project. This will most
assuredly not work. At a minimum, it jeopardizes the
compatibility of your app with future versions of the platform.
It is also often of questionable legality.

If you really intend to build a core library -- which is only
appropriate as part of creating a full virtual machine
distribution, as opposed to compiling an application -- then use
the "--core-library" option to suppress this error message.

If you go ahead and use "--core-library" but are in fact
building an application, then be forewarned that your application
will still fail to build or run, at some point. Please be
prepared for angry customers who find, for example, that your
application ceases to function once they upgrade their operating
system. You will be to blame for this problem.

If you are legitimately using some code that happens to be in a
core package, then the easiest safe alternative you have is to
repackage that code. That is, move the classes in question into
your own package namespace. This means that they will never be in
conflict with core system classes. JarJar is a tool that may help
you in this endeavor. If you find that you cannot do this, then
that is an indication that the path you are on will ultimately
lead to pain, suffering, grief, and lamentation.

[2011-11-03 14:25:57 - androidBooksApi] Dx 2 errors; aborting
[2011-11-03 14:25:57 - androidBooksApi] Conversion to Dalvik format failed with error 1

我尝试过使用 gdata-src.java-1.46.0.zip\gdata\java\deps 中的 deps ,并且再次适用于 java 应用程序,但不适用于 android 应用程序。当我在 android 应用程序中使用 deps 文件夹时,它说

11-03 20:48:50.894: WARN/System.err(334): java.lang.NoClassDefFoundError: com.google.gdata.client.books.BooksService

我知道这不可能,因为 java 应用程序有一个定义。

Lets start from the top. I am trying to create an application that takes a ISBN then connects to Google Books and from there grabs the information about the book. I was able to do this using plain java:

import java.net.URL;
import com.google.gdata.client.books.BooksService;
import com.google.gdata.client.books.VolumeQuery;
import com.google.gdata.data.books.VolumeEntry;
import com.google.gdata.data.books.VolumeFeed;
public class books 
{
public static void main(String[] args)
{       
    ////////////////////////////////////////////////
    try
    {                               
        BooksService booksService = new BooksService("UAH");
        String isbn = "9780262140874";
        URL url = new URL("http://www.google.com/books/feeds/volumes/?q=ISBN%3C" + isbn + "%3E");
        VolumeQuery volumeQuery = new VolumeQuery(url);
        VolumeFeed volumeFeed = booksService.query(volumeQuery, VolumeFeed.class);
        VolumeEntry bookInfo=volumeFeed.getEntries().get(0);

        System.out.println("Title: " + bookInfo.getTitles().get(0));
        System.out.println("Id: " + bookInfo.getId());
        System.out.println("Authors: " + bookInfo.getCreators());
        System.out.println("Description: "+bookInfo.getDescriptions()+"\n");
    }catch(Exception ex){System.out.println(ex.getMessage());}
    /////////////////////////////////////////////////                           
 }}

I used the jars books and client from \gdata-src.java-1.46.0\gdata\java\lib with the dependencies from google-api-java-client-1.5.0-beta\dependencies and it worked perfectly.
when I tried to convert it to android it gave me the error
"Conversion to Dalvik format failed with error 1" so it would not compile. I then tried doing the android tools -> fix projects and still no luck I tried change the levels in build targets. I have cleaned it 100 times and still it didn't work. So then I decided to remove the google-api-java-client-1.5.0-beta\dependencies (the jars causing the error) and then it would compile but crash because

11-02 20:44:27.093: WARN/System.err(485): java.lang.NoClassDefFoundError: com.google.gdata.client.books.BooksService

My android code is as follows:

import java.net.URL;

import com.google.gdata.client.books.BooksService;
import com.google.gdata.client.books.VolumeQuery;
import com.google.gdata.data.books.VolumeEntry;
import com.google.gdata.data.books.VolumeFeed;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AndroidBooksApiActivity extends Activity {
Button btnSearch;
TextView tvTitle,tvISBN,tvAuthor,tvDatePublished;
EditText etSearch;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    btnSearch=(Button)findViewById(R.id.btnFindBook);
    tvTitle=(TextView)findViewById(R.id.tvTitle);
    tvISBN=(TextView)findViewById(R.id.tvISBN);
    tvAuthor=(TextView)findViewById(R.id.tvAuthor);
    tvDatePublished=(TextView)findViewById(R.id.tvDatePublished);
    etSearch=(EditText)findViewById(R.id.etSearch);
    btnSearch.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            try
            {   
                //String isbn;
                //isbn=etSearch.getText().toString();
                String isbn = "9780262140874";          

                BooksService booksService = new BooksService("UAH");        

                URL url = new URL("http://www.google.com/books/feeds/volumes/?q=ISBN%3C" + isbn + "%3E");

                VolumeQuery volumeQuery = new VolumeQuery(url);

                VolumeFeed volumeFeed = booksService.query(volumeQuery, VolumeFeed.class);

                VolumeEntry bookInfo=volumeFeed.getEntries().get(0);

                tvTitle.setText("Title: " + bookInfo.getTitles().get(0));
                tvISBN.setText("ISBN: " + isbn);
                tvAuthor.setText("Authors: " + bookInfo.getCreators());
                tvDatePublished.setText("DatePublished: "+bookInfo.getDates()+"\n");
            }catch(Throwable ex){tvTitle.setText(ex.getMessage()); ex.printStackTrace();}

          }
    });
}

}

If you have any more questions I will answer to the best of my ability. I am still learning so it is possible my error is something easy I am just not seeing.

    java.lang.IllegalArgumentException: already added:         Lcom/google/common/annotations/GwtCompatible;
[2011-11-03 14:25:51 - androidBooksApi] Dx  at com.android.dx.dex.file.ClassDefsSection.add(ClassDefsSection.java:123)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at com.android.dx.dex.file.DexFile.add(DexFile.java:143)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at com.android.dx.command.dexer.Main.processClass(Main.java:372)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at com.android.dx.command.dexer.Main.processFileBytes(Main.java:346)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at com.android.dx.command.dexer.Main.access$400(Main.java:59)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:294)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:244)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:130)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:108)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at com.android.dx.command.dexer.Main.processOne(Main.java:313)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at com.android.dx.command.dexer.Main.processAllFiles(Main.java:233)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at com.android.dx.command.dexer.Main.run(Main.java:185)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at java.lang.reflect.Method.invoke(Unknown Source)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at com.android.ide.eclipse.adt.internal.build.DexWrapper.run(DexWrapper.java:179)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at com.android.ide.eclipse.adt.internal.build.BuildHelper.executeDx(BuildHelper.java:652)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at com.android.ide.eclipse.adt.internal.build.builders.PostCompilerBuilder.build(PostCompilerBuilder.java:510)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at org.eclipse.core.internal.events.BuildManager$2.run(BuildManager.java:728)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:199)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:239)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at org.eclipse.core.internal.events.BuildManager$1.run(BuildManager.java:292)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:295)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at org.eclipse.core.internal.events.BuildManager.basicBuildLoop(BuildManager.java:351)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at org.eclipse.core.internal.events.BuildManager.build(BuildManager.java:374)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at org.eclipse.core.internal.events.AutoBuildJob.doBuild(AutoBuildJob.java:143)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at org.eclipse.core.internal.events.AutoBuildJob.run(AutoBuildJob.java:241)
[2011-11-03 14:25:51 - androidBooksApi] Dx  at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)
[2011-11-03 14:25:53 - androidBooksApi] Dx warning: Ignoring InnerClasses attribute for an anonymous inner class
(org.joda.time.DateTimeZone$1) that doesn't come with an
associated EnclosingMethod attribute. This class was probably produced by a
compiler that did not target the modern .class file format. The recommended
solution is to recompile the class from source, using an up-to-date compiler
and without specifying any "-target" type options. The consequence of ignoring
this warning is that reflective operations on this class will incorrectly
indicate that it is *not* an inner class.
[2011-11-03 14:25:57 - androidBooksApi] Dx 
trouble processing "javax/transaction/HeuristicCommitException.class":

Ill-advised or mistaken usage of a core class (java.* or javax.*)
when not building a core library.

This is often due to inadvertently including a core library file
in your application's project, when using an IDE (such as
Eclipse). If you are sure you're not intentionally defining a
core class, then this is the most likely explanation of what's
going on.

However, you might actually be trying to define a class in a core
namespace, the source of which you may have taken, for example,
from a non-Android virtual machine project. This will most
assuredly not work. At a minimum, it jeopardizes the
compatibility of your app with future versions of the platform.
It is also often of questionable legality.

If you really intend to build a core library -- which is only
appropriate as part of creating a full virtual machine
distribution, as opposed to compiling an application -- then use
the "--core-library" option to suppress this error message.

If you go ahead and use "--core-library" but are in fact
building an application, then be forewarned that your application
will still fail to build or run, at some point. Please be
prepared for angry customers who find, for example, that your
application ceases to function once they upgrade their operating
system. You will be to blame for this problem.

If you are legitimately using some code that happens to be in a
core package, then the easiest safe alternative you have is to
repackage that code. That is, move the classes in question into
your own package namespace. This means that they will never be in
conflict with core system classes. JarJar is a tool that may help
you in this endeavor. If you find that you cannot do this, then
that is an indication that the path you are on will ultimately
lead to pain, suffering, grief, and lamentation.

[2011-11-03 14:25:57 - androidBooksApi] Dx 2 errors; aborting
[2011-11-03 14:25:57 - androidBooksApi] Conversion to Dalvik format failed with error 1

I have tried using the the deps in gdata-src.java-1.46.0.zip\gdata\java\deps and again that worked for the java app but not for the android one. When I use the deps folder in the android app it says that

11-03 20:48:50.894: WARN/System.err(334): java.lang.NoClassDefFoundError: com.google.gdata.client.books.BooksService

I know that can't be because the java application has a definition.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱花细雨 2024-12-20 15:04:56

您能否将您的工具配置为使用 --debug 标志运行 dx ?不幸的是,知道dxConversion to Dalvik format failed with error 1而失败,并没有足够的信息来解决该问题。如果您可以粘贴 dx 的完整输出,我们就有更好的机会找出问题所在。

Could you configure your tools to run dx with the --debug flag? Unfortunately knowing that dx failed with Conversion to Dalvik format failed with error 1 isn't enough information to fix the problem. If you can paste the full output of dx we have a better chance of figuring out what's wrong.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文