【发布时间】:2018-04-30 06:55:01
【问题描述】:
我正在尝试使用EBay's Inventory API 执行以下两个请求:
POST: bulkUpdatePriceQuantity(创建新列表)
PUT: createOrReplaceInventoryItem(更新列表的价格/数量)使用
我对@987654324@ 和OKHTTP 还很陌生,想知道是否有人可以发布一个简单示例,说明如何创建新列表并更新现有列表的价格/数量。
我花了几天时间阅读有关Retrofit 和OKHTTP 的内容,这似乎很令人困惑。就像我不明白在哪里/如何添加 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