Help us understand the problem. What is going on with this article?

JAVAでAmazon Product Advertising APIを使用するときのメモ(2016/11時点)

More than 3 years have passed since last update.

JAVAでAmazon Product Advertising APIを使用するときの手順・サンプルコードをメモ書きします。なお、当該APIは今後仕様変更の可能性もあるので、今回のメモは2016/11時点での内容とします。

1.アカウント作成

amazonのサイトでアカウント登録をします。Amazon Product Advertising API関連リンク集の記事が参考になります。

2.ヘルパークラス・サンプルコードの取得

Product Advertising API Signed Requests Sample Code - Java REST/QUERYよりコードをダウンロードします。

3.APIリクエスト先のURL確認

Anatomy of a REST RequestからAPIのリクエスト先のURLを確認します。

4.APIリクエストのコード例

以下に歌手名で音楽を検索する場合を例に、サンプルコードを記載します。なお、パラメータのリファレンスはProduct Advertising APIのページに詳しく記載されています。

AmazonApiSample.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class AmazonApiSample {

    public static String searchMusic(String artist) {
        // ダウンロードしたヘルパークラスでオブジェクトの作成
        SignedRequestsHelper helper;
        try {
            // ENDPOINTに確認したURLのドメインを入力
            helper = SignedRequestsHelper.getInstance(AmazonConstants.ENDPOINT, AmazonConstants.AWS_ACCESS_KEY_ID, AmazonConstants.AWS_SECRET_KEY);
        } catch (Exception e) {
            // exceptionの時はnullを返す
            return null;
        }
        // Mapにリクエストのキー情報をつめる
        Map<String, String> params = new HashMap<String, String>();
        params.put("Service", "AWSECommerceService");
        params.put("AWSAccessKeyId", AmazonConstants.AWS_ACCESS_KEY_ID);
        params.put("Version", "2013-08-01");
        params.put("AssociateTag", AmazonConstants.AWS_ASSOCIATETAG);
        params.put("Operation", "ItemSearch");
        params.put("SearchIndex", "Music");
        params.put("Artist", artist);
        params.put("Sort", "-releasedate");
        params.put("ContentType", "text/xml");

        HttpURLConnection httpURLConnection = null;
        BufferedReader bufferedReader = null;

        try{
            // リクエスト処理
            String requestUrl = helper.sign(params);
            URL url = new URL(requestUrl);
            httpURLConnection = (HttpURLConnection)url.openConnection();
            String charSet = "UTF-8";
            String method = "GET";
            httpURLConnection.setRequestMethod( method );
            InputStreamReader inputStreamReader = new InputStreamReader( httpURLConnection.getInputStream(), charSet );
            bufferedReader = new BufferedReader( inputStreamReader );

            // レスポンスのXMLを取得。
            String oneLine = null;
            String responseXml = "";
            while( true ){
                oneLine = bufferedReader.readLine();
                // 行がNULLの場合
                if(oneLine == null){
                    break;
                // レスポンスの文字列へ行を追加
                }else{
                    responseXml += oneLine;
                }
            }
            return responseXml;
        }catch( Exception e ){
            // exceptionの時はnullを返す
            return null;
        }finally{

            if( bufferedReader != null ){
                try{
                    bufferedReader.close();
                }catch( IOException e ){
                    // エラー時は無処理
                }
            }
            if( httpURLConnection != null ){
                httpURLConnection.disconnect();
            }
        }
    }

}
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした