簡體   English   中英

使用EBay的庫存API進行改造+ OkHTTP

[英]Retrofit + OkHTTP with EBay's Inventory API

我正在嘗試使用EBay的Inventory API執行以下兩個請求:

POST: bulkUpdatePriceQuantity (創建新列表)

createOrReplaceInventoryItem createOrReplaceInventoryItem (更新價格/清單的數量),使用

我對RetrofitOKHTTP還是很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'
        }";




}




}

但是,在兩種情況下,我都會遇到很多錯誤。 我已經閱讀了數小時的兩種技術(我的頭在旋轉),但我不清楚。

如果有人可以發布一個簡單的示例說明如何完成這兩項操作,我將不勝感激。

不幸的是,我沒有開發人員帳戶來檢查它是否確實有效,但這是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和改造

這是我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>

希望能幫助到你

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM