【问题标题】:Retrofit + OkHTTP with EBay's Inventory API改造 + OkHTTP 与 EBay 的 Inventory API
【发布时间】:2018-04-30 06:55:01
【问题描述】:

我正在尝试使用EBay's Inventory API 执行以下两个请求:

POST: bulkUpdatePriceQuantity(创建新列表)

PUT: createOrReplaceInventoryItem(更新列表的价格/数量)使用

我对@9​​87654324@ 和OKHTTP 还很陌生,想知道是否有人可以发布一个简单示例,说明如何创建新列表并更新现有列表的价格/数量。

我花了几天时间阅读有关RetrofitOKHTTP 的内容,这似乎很令人困惑。就像我不明白在哪里/如何添加 EBay 授权令牌以及如何将数据传递给 EBay(例如新价格/数量或新列表的详细信息)。

到目前为止,这就是我为Retrofit 提出的建议:

public interface RetrofitEBaySellAPIService {

@Headers("X-EBAY-C-PACKED-EXAMPLE:   Authorization: Bearer <TOKEN_GOES_HERE>")
@POST("/bulk_update_price_quantity")
// https://api.ebay.com/sell/inventory/v1/bulk_update_price_quantity
Call<List<Task>> getTasks();

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("https://api.ebay.com/sell/inventory/v1/")
        .addConverterFactory(GsonConverterFactory.create()) // error: GsonConverterFactory cannot be resolved
        .build();

RetrofitEBaySellAPIService service = retrofit.create(RetrofitEBaySellAPIService.class);

Response response = service.getClientList("").execute();


}

这就是我想出的OKHTTP

public class OKHTTPPostExample  {


public OKHTTPPostExample()
{
}


public static final MediaType JSON
= MediaType.parse("application/json; charset=utf-8");

OkHttpClient client = new OkHttpClient();

 public String post(String url, String json) throws IOException {
    RequestBody body = RequestBody.create(JSON, json);
    String header = "Authorization: Bearer <TOKEN_GOES_HERE?>";
    Headers headerbuild = Headers.of(header);
    Request request = new Request.Builder()
            .url(url)
            .post(body)
            .headers(headerbuild)
            .build();


    try (Response response = client.newCall(request).execute()) {

        return response.body().string();

    } 
}

 public String revisePriceAndQuantity(String sku) {
    return "{
        'requests' : [
        { 
        'sku' : 'SKU_STRING',
        "shipToLocationAvailability" :
        { 
        'quantity' : 'integer'
        }";




}




}

但是,在这两种情况下,我都会遇到很多错误。我已经阅读了这两种技术几个小时(我头晕目眩),但我并没有清楚地理解它。

如果有人可以发布一个如何执行这两种操作的简单示例,我将不胜感激。

【问题讨论】:

    标签: java rest retrofit okhttp ebay-api


    【解决方案1】:

    很遗憾,我没有开发者帐户来检查它是否真的有效,但这里是 bulkUpdatePriceQuantity 的一个示例

    package example;
    
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import retrofit2.Call;
    import retrofit2.Retrofit;
    import retrofit2.converter.gson.GsonConverterFactory;
    import retrofit2.http.Body;
    import retrofit2.http.HeaderMap;
    import retrofit2.http.POST;
    
    public class Runner {
    
        //DTOs
        public static class Offer {
            public Integer availableQuantity;
            public String offerId;
            public Price price;
        }
    
        public static class ShipToLocationAvailability {
            public Integer quantity;
        }
    
        public static class Price {
            public String currency;
            public String value;
        }
    
        public static class Request {
            public List<Offer> offers = null;
            public ShipToLocationAvailability shipToLocationAvailability;
            public String sku;
        }
    
        public static class Response {
    
            public String offerId;
            public String sku;
            public Integer statusCode;
    
        }
    
        public static class RequestBody{
            public List<Request> requests;
        }
    
        public static class ResponseBody{
            public List<Response> responses;
        }
    
    
        //api interface
        public static interface RetrofitEBaySellAPIService {
    
            @POST("/bulk_update_price_quantity")
            Call<ResponseBody> bulkUpdatePriceQuantity(@HeaderMap Map<String, String> headers, @Body RequestBody object);
        }
    
    
        //entry point
        public static void main(String[] args) throws IOException {
            /**
             * request should be initialized.
             * you can do it by creating all necessary objects manually
             * or by deserializing the object from json like this
             * RequestBody request = new Gson().fromJson(jsonString, RequestBody.class);
             * 
             * where jsonString is a string that contains json representation of your request body
             * 
             */
            RequestBody request = null; 
    
            Retrofit retrofit = new Retrofit.Builder().baseUrl("https://api.ebay.com/sell/inventory/v1/")
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
    
            RetrofitEBaySellAPIService service = retrofit.create(RetrofitEBaySellAPIService.class);
            Map<String, String> headers = new HashMap<>();
            //token should hold a valid active token
            String token = null;
            //put all the headers you need in that map
            headers.put("Authorization", "Bearer " + token);
            ResponseBody response = service.bulkUpdatePriceQuantity(headers, request).execute().body();
    
        }
    
    }
    

    你需要在你的类路径中有converter-gson、gson和retrofit

    这是我的 pom.xml 中的一个片段

        <dependency>
            <groupId>com.squareup.retrofit2</groupId>
            <artifactId>converter-gson</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.2</version>
        </dependency>
        <dependency>
            <groupId>com.squareup.retrofit2</groupId>
            <artifactId>retrofit</artifactId>
            <version>2.3.0</version>
        </dependency>
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-25
      • 2014-07-12
      • 1970-01-01
      • 2015-12-28
      • 2014-03-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多