有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

WSO2(JSON)带JAVA的Google Book API

我正在做一个小项目,在这个项目中,我需要使用Google Book API Web服务来检索某些信息,然后进行验证(没有完成验证部分,但停留在API部分)

请在下面找到我的代码

package au.edu.swin.waa;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.util.Iterator;
import javax.xml.stream.XMLStreamException;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.impl.builder.StAXOMBuilder;
import org.apache.axis2.AxisFault;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.ConfigurationContextFactory;

public class GoogleBooksJSONClient {

/**
 * @param args
 * @throws AxisFault 
 */
public static void main(String[] args) throws AxisFault {


// 1st invocation ...

    String epr = "https://www.googleapis.com/books/v1/volumes?q=isbn:0131465759&key=MYAPIKEY";

    EndpointReference targetEPR = new EndpointReference(epr);

    Options options = new Options();
    options.setTo(targetEPR);
    options.setProperty(Constants.Configuration.MESSAGE_TYPE, "application/json");
    options.setProperty(Constants.Configuration.HTTP_METHOD, Constants.Configuration.HTTP_METHOD_GET);
    options.setAction(null); // important

    File configFile = new File("axis2.xml");
    ConfigurationContext clientConfigurationContext;
    clientConfigurationContext = ConfigurationContextFactory
            .createConfigurationContextFromFileSystem(null, configFile.getAbsolutePath());

    ServiceClient sender = new ServiceClient(clientConfigurationContext, null);
    sender.setOptions(options);

    OMElement response = sender.sendReceive(null); // no payload

    System.out.println("returned from 1st call to google books."); 

    processResponsePayload(response);

}

private static void processResponsePayload(OMElement response) {

    // testing if there is anything returned ...
            System.out.println("to string: "+response.toString());
            System.out.println("get text: "+response.getText());

    Iterator iterator = response.getChildrenWithLocalName("return");
    OMElement returnElement = (OMElement) iterator.next();
    System.out.println(returnElement.getText());

}

}

我运行这个,我不断得到这个,而不是完整的JSON文件

<kind>books#volumes</kind>

无论我尝试什么样的迭代,我似乎都无法得到比这更好的结果。我试着改变ISBN号,API,使用for循环,迭代器,我只得到一个元素,那就是

books#volumes

而不是应该在这里(https://www.googleapis.com/books/v1/volumes?q=isbn:1781100233

我从here读到,您必须在axis2中添加JSONStreamBuilderJSONStreamFormatter。xml文件,但它不工作,我仍然只得到了第一行

我怎么才能接其他电话


共 (0) 个答案