这是我的课程,我正在使用api来打电话。 产品,产品,终点ZappoAPI。从控制器调用方法callRestAPI,其中异步调用rest api。我得到了空列表作为回应。
public class Product {
private String productId;
public Product(String productId) {
this.productId = productId;
}
public String getBrandName() {
return productId;
}
public void setBrandName(String productId) {
this.productId = productId;
}
@Override
public String toString() {
return(productId);
}
}
public class Products {
List<Product> products = new ArrayList<>();
public List<Product> getProducts() {
return products;
}
}
public interface ZappoAPI {
@GET("/Search")
Call<ZappoProducts> searchProducts(@Query("term") String tags, @Query("key") String key);
}
public void callRestAPI(){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.zappos.com")
.addConverterFactory(GsonConverterFactory.create())
.build();
// prepare call in Retrofit 2.0
ZappoAPI zappoAPI = retrofit.create(ZappoAPI.class);
Call<ZappoProducts> call = zappoAPI.searchProducts(this.prodName, IConstant.API_KEY);
//asynchronous call
call.enqueue(this);
String url = call.request().url().toString();
Log.d("url is",url);
Log.d("url is",url);
}
on callback method -
public void onResponse(Call<ZappoProducts> call, Response<ZappoProducts> response) {
boolean isSuccess = response.raw().isSuccessful();
List<Product> products = new ArrayList<>();
if(isSuccess){
products = response.body().getProducts();
}
String[] values = new String[products.size()];
for (int i=0;i<products.size();i++){
values[i] = products.get(i).toString();
}
ArrayAdapter<String> adapter = (ArrayAdapter<String>) this.adapter;
adapter.clear();
adapter.addAll(values);
}
现在,响应,我正在获得空产品清单。 知道怎么解决吗?