Skip to main content
Published: December 04 2014, 5:12:00 AMUpdated: November 28 2020, 12:57:02 PM

Use the below program to find a category in which the item has to be listed. You need to replace the text "INSERT_YOUR_TOKEN" with your user token.  The assumption is that you are already using eBay SDK for Java.

 

 

 

 package test.categorysearch;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import com.ebay.sdk.ApiContext;
import com.ebay.sdk.ApiCredential;
import com.ebay.sdk.ApiLogging;
import com.ebay.sdk.call.GetSuggestedCategoriesCall;
import com.ebay.soap.eBLBaseComponents.CategoryType;
import com.ebay.soap.eBLBaseComponents.GetSuggestedCategoriesRequestType;
import com.ebay.soap.eBLBaseComponents.GetSuggestedCategoriesResponseType;
import com.ebay.soap.eBLBaseComponents.SuggestedCategoryType;

public class GetSuggestedCategories
{
 public static void main(String args[])
 {
  GetSuggestedCategories caller=new GetSuggestedCategories();
  
  ApiContext apiContext = new ApiContext();

     ApiCredential cred = apiContext.getApiCredential();
  
     cred.seteBayToken("INSERT_YOUR_TOKEN");
       
     apiContext.setApiServerUrl("https://api.ebay.com/wsapi");
      
  ApiLogging apiLogging=apiContext.getApiLogging();
  apiLogging.setLogSOAPMessages(true);
  apiLogging.setLogHTTPHeaders(true);
  
  
  GetSuggestedCategoriesCall call=new GetSuggestedCategoriesCall();
  call.setApiContext(apiContext);
  
  
  GetSuggestedCategoriesRequestType request=new GetSuggestedCategoriesRequestType();
  
  try
  {
  
  BufferedReader br=new BufferedReader(new InputStreamReader(System.in));  
  String query="";
  
  
  System.out.println("Enter the product name(Type 9 to quit):");
  query=br.readLine();
  while(!query.equals("9"))
  {
 
  request.setQuery(query);
  
  GetSuggestedCategoriesResponseType response=(GetSuggestedCategoriesResponseType)call.execute(request);
  
  if(response.getCategoryCount()>0)
  {
  caller.processResponse(response);
  }
  else
  {
   System.out.println("No match found!!!");
  }
  
  System.out.println("Enter the product name(Type 9 to quit):");
  query=br.readLine();
  
  }
  }
  catch(Exception e)
  {
   e.printStackTrace();
  }
  
   
  
 }
 
 private void processResponse(GetSuggestedCategoriesResponseType response)
 {
  SuggestedCategoryType suggestedCategory=null;
  for(int i=0;i<response.getSuggestedCategoryArray().getSuggestedCategoryLength();i++)
  {
   
   suggestedCategory=response.getSuggestedCategoryArray().getSuggestedCategory(i);   
   CategoryType category=suggestedCategory.getCategory();
 
   
   System.out.println("CategoryID:"+category.getCategoryID());
   System.out.println("Category Name:"+category.getCategoryName());
   System.out.println("Rank(Higher the better):"+suggestedCategory.getPercentItemFound());
   System.out.println("Parent Category Name:"+category.getCategoryParentName(0));
   System.out.println("********************************************");   
   
  }
  System.out.println("-------------------------------------------------");
  
 }
}

 

How well did this answer your question?
Answers others found helpful