Guest User

Untitled

a guest
Jul 13th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. import org.rakura.amazon.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.util.List;
  5. import javax.swing.*;
  6. import javax.xml.ws.*;
  7.  
  8. /**
  9. * The client for the Amazon e-commerce test program.
  10. * @version 1.10 9-12-09
  11. * @author Rakura
  12. */
  13. public class AmazonTest
  14. {
  15. public static void main(String[] args)
  16. {
  17. JFrame frame = new AmazonTestFrame();
  18. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  19. frame.setVisible(true);
  20. }
  21. }
  22.  
  23. /**
  24. * A frame to select the book author and to display the server response.
  25. */
  26. class AmazonTestFrame extends JFrame
  27. {
  28. public AmazonTestFrame()
  29. {
  30. setTitle("AmazonTest");
  31. setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
  32.  
  33. JPanel panel = new JPanel();
  34.  
  35. panel.add(new JLabel("Author:"));
  36. author = new JTextField(20);
  37. panel.add(author);
  38.  
  39. JButton searchButton = new JButton("Search");
  40. panel.add(searchButton);
  41. searchButton.addActionListener(new ActionListener()
  42. {
  43. public void actionPerformed(ActionEvent event)
  44. {
  45. result.setText("Please wait...");
  46. new SwingWorker<Void, Void>()
  47. {
  48. @Override
  49. protected Void doInBackground() throws Exception
  50. {
  51. String name = author.getText();
  52. String books = searchByAuthor(name);
  53. result.setText(books);
  54. return null;
  55. }
  56. }.execute();
  57. }
  58. });
  59.  
  60. result = new JTextArea();
  61. result.setLineWrap(true);
  62. result.setEditable(false);
  63.  
  64. if (accessKey.equals("your key here"))
  65. {
  66. result.setText("You need to edit the Amazon access key.");
  67. searchButton.setEnabled(false);
  68. }
  69.  
  70. add(panel, BorderLayout.NORTH);
  71. add(new JScrollPane(result), BorderLayout.CENTER);
  72. }
  73.  
  74. /**
  75. * Calls the Amazon web service to find titles that match the author.
  76. * @param name the author name
  77. * @return a description of the matching titles
  78. */
  79. private String searchByAuthor(String name)
  80. {
  81. AWSECommerceService service = new AWSECommerceService();
  82. AWSECommerceServicePortType port = service.getPort(AWSECommerceServicePortType.class);
  83. ItemSearchRequest request = new ItemSearchRequest();
  84. request.getResponseGroup().add("ItemAttributes");
  85. request.setSearchIndex("Books");
  86.  
  87. Holder<List<Items>> responseHolder = new Holder<List<Items>>();
  88. request.setAuthor(name);
  89. port.itemSearch("", accessKey, "", "", "", "", request, null, null, responseHolder);
  90.  
  91. List<Item> response = responseHolder.value.get(0).getItem();
  92.  
  93. StringBuilder r = new StringBuilder();
  94. for (Item item : response)
  95. {
  96. r.append("authors=");
  97. List<String> authors = item.getItemAttributes().getAuthor();
  98. r.append(authors);
  99. r.append(",title=");
  100. r.append(item.getItemAttributes().getTitle());
  101. r.append(",publisher=");
  102. r.append(item.getItemAttributes().getPublisher());
  103. r.append(",pubdate=");
  104. r.append(item.getItemAttributes().getPublicationDate());
  105. r.append("\n");
  106. }
  107. return r.toString();
  108. }
  109.  
  110. private static final int DEFAULT_WIDTH = 450;
  111. private static final int DEFAULT_HEIGHT = 300;
  112.  
  113. private static final String accessKey = "12Y1EEATQ8DDYJCVQYR2";
  114.  
  115. private JTextField author;
  116. private JTextArea result;
  117. }
Add Comment
Please, Sign In to add comment