0

I am doing small project whereby I am required to consume Google Book API webservices in order to retrieve certain information and then validate (have not done the validation part but stuck on the API part)

Please find below my code

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());

}

}

I run this and I keep getting this instead of the full JSON file

<kind>books#volumes</kind>

and no matter what iteration I try, i can't seem to get anything else than this. I tried changing ISBN number, API, using for loops, iterator, I only get one element which is the

books#volumes

instead of what is supposed to be here (https://www.googleapis.com/books/v1/volumes?q=isbn:1781100233)

I read from here that you have to add JSONStreamBuilder and JSONStreamFormatter in the axis2.xml file but it does not work, I am still being given just the first line.

How can I get the other lines?

halfer
  • 19,824
  • 17
  • 99
  • 186
Kelvin Chong
  • 222
  • 1
  • 3
  • 17

0 Answers0