我尝试使用EBay's Inventory API执行以下两个请求:
POST: bulkUpdatePriceQuantity
(创建新商家信息)
PUT: createOrReplaceInventoryItem
(更新价格/列表数量)
我对Retrofit
和OKHTTP
还不熟悉,并且想知道是否有人可以发布一个如何创建新商家信息和更新现有商品的价格/数量的简单示例。< / p>
我花了几天时间阅读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'
}";
}
}
然而,在这两种情况下,我都会遇到很多错误。我已经读了两个小时的技术(我的头在旋转),但我不清楚它。
如果有人可以发布一个如何进行这两项操作的简单示例,我将非常感激。
答案 0 :(得分: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>
希望有所帮助