2010-06-02 5 views
1

이베이 Finding APIFeedback API으로 시작한 이래로 GAE/J에 기본 API 구현을 배포해야합니다.eBay 찾기/피드백 API 구현

문제는 다음과 같습니다

  1. 우리는 어떻게 이베이 SDK의 로컬 디바이스 환경에 시작합니까?

  2. Finding API 및 피드백에 대한 Java 자습서가 없습니다.

  3. GAE/J + ebay API로 인해 복잡성이 발생하지 않을까요?

올바른 방향으로 헤드 스타트를 찾고 있는데, Eclipse + GAE 플러그인을 사용하고 있습니다. 단계를 안내하여

답변

4

여기

package ebay; 

import java.io.ByteArrayInputStream; 
import java.io.InputStream; 
import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.xpath.XPath; 
import javax.xml.xpath.XPathConstants; 
import javax.xml.xpath.XPathExpression; 
import javax.xml.xpath.XPathFactory; 
import org.w3c.dom.Document; 
import org.w3c.dom.Node; 
import org.w3c.dom.NodeList; 

import ebay.URLReader; 

/** 
* 
* @author rajeev jha ([email protected]) 
* 
*/ 
public class EbayDriver { 

    public final static String EBAY_APP_ID = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx"; 
    public final static String EBAY_FINDING_SERVICE_URI = "http://svcs.ebay.com/services/search/FindingService/v1?OPERATION-NAME=" 
      + "{operation}&SERVICE-VERSION={version}&SECURITY-APPNAME=" 
      + "{applicationId}&GLOBAL-ID={globalId}&keywords={keywords}" 
      + "&paginationInput.entriesPerPage={maxresults}"; 
    public static final String SERVICE_VERSION = "1.0.0"; 
    public static final String OPERATION_NAME = "findItemsByKeywords"; 
    public static final String GLOBAL_ID = "EBAY-US"; 
    public final static int REQUEST_DELAY = 3000; 
    public final static int MAX_RESULTS = 10; 
    private int maxResults; 

    public EbayDriver() { 
     this.maxResults = MAX_RESULTS; 

    } 

    public EbayDriver(int maxResults) { 
     this.maxResults = maxResults; 
    } 

    public String getName() { 
     return IDriver.EBAY_DRIVER; 
    } 

    public void run(String tag) throws Exception { 

     String address = createAddress(tag); 
     print("sending request to :: ", address); 
     String response = URLReader.read(address); 
     print("response :: ", response); 
     //process xml dump returned from EBAY 
     processResponse(response); 
     //Honor rate limits - wait between results 
     Thread.sleep(REQUEST_DELAY); 


    } 

    private String createAddress(String tag) { 

     //substitute token 
     String address = EbayDriver.EBAY_FINDING_SERVICE_URI; 
     address = address.replace("{version}", EbayDriver.SERVICE_VERSION); 
     address = address.replace("{operation}", EbayDriver.OPERATION_NAME); 
     address = address.replace("{globalId}", EbayDriver.GLOBAL_ID); 
     address = address.replace("{applicationId}", EbayDriver.EBAY_APP_ID); 
     address = address.replace("{keywords}", tag); 
     address = address.replace("{maxresults}", "" + this.maxResults); 

     return address; 

    } 

    private void processResponse(String response) throws Exception { 


     XPath xpath = XPathFactory.newInstance().newXPath(); 
     InputStream is = new ByteArrayInputStream(response.getBytes("UTF-8")); 
     DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder builder = domFactory.newDocumentBuilder(); 


     Document doc = builder.parse(is); 
     XPathExpression ackExpression = xpath.compile("//findItemsByKeywordsResponse/ack"); 
     XPathExpression itemExpression = xpath.compile("//findItemsByKeywordsResponse/searchResult/item"); 

     String ackToken = (String) ackExpression.evaluate(doc, XPathConstants.STRING); 
     print("ACK from ebay API :: ", ackToken); 
     if (!ackToken.equals("Success")) { 
      throw new Exception(" service returned an error"); 
     } 

     NodeList nodes = (NodeList) itemExpression.evaluate(doc, XPathConstants.NODESET); 

     for (int i = 0; i < nodes.getLength(); i++) { 

      Node node = nodes.item(i); 

      String itemId = (String) xpath.evaluate("itemId", node, XPathConstants.STRING); 
      String title = (String) xpath.evaluate("title", node, XPathConstants.STRING); 
      String itemUrl = (String) xpath.evaluate("viewItemURL", node, XPathConstants.STRING); 
      String galleryUrl = (String) xpath.evaluate("galleryURL", node, XPathConstants.STRING); 

      String currentPrice = (String) xpath.evaluate("sellingStatus/currentPrice", node, XPathConstants.STRING); 

      print("currentPrice", currentPrice); 
      print("itemId", itemId); 
      print("title", title); 
      print("galleryUrl", galleryUrl); 

     } 

     is.close(); 

    } 

    private void print(String name, String value) { 
     System.out.println(name + "::" + value); 
    } 

    public static void main(String[] args) throws Exception { 
     EbayDriver driver = new EbayDriver(); 
     String tag = "Velo binding machine"; 
     driver.run(java.net.URLEncoder.encode(tag, "UTF-8")); 

    } 
} 

과 이베이 REST API를 사용하여 정보는 URLReader의 클래스

package ebay; 

import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.net.URL; 
import java.net.URLConnection; 

/** 
* 
* @author rajeev jha([email protected]) 
* 
*/ 
public class URLReader { 

    public static String read(String address) throws Exception{ 

     URL url = new URL(address); 
     URLConnection connection = url.openConnection(); 
     connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8"); 

     String line; 
     String response; 
     long totalBytes = 0 ; 

     StringBuilder builder = new StringBuilder(); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
     while ((line = reader.readLine()) != null) { 
      builder.append(line); 
      totalBytes += line.getBytes("UTF-8").length ; 
      //System.out.println("Total bytes read :: " + totalBytes); 
     } 

     response = builder.toString(); 

     return response ; 
    } 

} 
0123입니다

해피 코딩!

1

단계 :


http://developer.ebay.com/DevZone/XML/docs/HowTo/FirstCall/MakingCallJava.html

이있어, 간단한 구현은 HTTP GET/POST가 될 수 있습니다

http://svcs.ebay.com/services/search/FindingService/v1?OPERATION-NAME=findItemsByKeywords 
    &SERVICE-VERSION=1.0.0 
    &SECURITY-APPNAME=YourAppID 
    &RESPONSE-DATA-FORMAT=XML 
    &REST-PAYLOAD 
    &keywords=harry%20potter%20phoenix 

Source (이름 - 값 (NV) 쌍을 사용)

이제 appid로 SECURITY-APPNAME을 지정하면 XML의 항목 목록이 반환됩니다. 이제 XML을 HTML로 포맷팅하면됩니다.

예제 : thelostlogbook.appspot.com

사용하여 내장

: JSP를, HTTP 요청 (POST) 전체 예를 들어 누구 때문에 내가 가져 오기 위해 사용하고 코드의 샘플을 게시 하겠어을 해치지 않을

관련 문제