RapidAPI返回不同的json格式

时间:2020-07-08 08:16:21

标签: java android json api retrofit2

我是Retrofit的新手,正在使用API​​。我正在尝试在Logcat中输出数据,但它没有给我有意义的数据。

让我先告诉你我做了什么。

来自API的JSON文件(这只是一个片段):

"ASIN":"B07RF1XD36"
"title":"Acer Aspire 5 Slim Laptop, 15.6 inches Full HD IPS Display, AMD Ryzen 3 3200U, Vega 3 Graphics, 4GB DDR4, 128GB SSD, Backlit Keyboard, Windows 10 in S Mode, A515-43-R19L,Silver"
"price":"$321.81"
"listPrice":"$349.99"
"imageUrl":"https://m.media-amazon.com/images/I/41vMYgD92xL._SL160_.jpg"
"detailPageURL":"https://www.amazon.com/dp/B07RF1XD36"
"rating":"4.4"
"totalReviews":"5920"
"subtitle":""
"isPrimeEligible":"1"

型号

package com.example.retrofittutorial;

import com.google.gson.annotations.SerializedName;

public class AmazonShopItems {
    @SerializedName("ASIN")
    private String ASIN;

    @SerializedName("title")
    private String title;

    @SerializedName("price")
    private String price;

    @SerializedName("listPrice")
    private String listPrice;

    @SerializedName("imageUrl")
    private String imageUrl;

    @SerializedName("detailPageURL")
    private String detailPageURL;

    @SerializedName("rating")
    private String rating;

    @SerializedName("totalReviews")
    private String totalReviews;

    @SerializedName("subtitle")
    private String subtitle;

    @SerializedName("isPrimeEligible")
    private String isPrimeEligible;

    public AmazonShopItems(String ASIN, String title, String price, String listPrice, String imageUrl, String detailPageURL, String rating, String totalReviews, String subtitle, String isPrimeEligible) {
        this.ASIN = ASIN;
        this.title = title;
        this.price = price;
        this.listPrice = listPrice;
        this.imageUrl = imageUrl;
        this.detailPageURL = detailPageURL;
        this.rating = rating;
        this.totalReviews = totalReviews;
        this.subtitle = subtitle;
        this.isPrimeEligible = isPrimeEligible;
    }

    public String getASIN() {
        return ASIN;
    }

    public void setASIN(String ASIN) {
        this.ASIN = ASIN;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public String getListPrice() {
        return listPrice;
    }

    public void setListPrice(String listPrice) {
        this.listPrice = listPrice;
    }

    public String getImageUrl() {
        return imageUrl;
    }

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
    }

    public String getDetailPageURL() {
        return detailPageURL;
    }

    public void setDetailPageURL(String detailPageURL) {
        this.detailPageURL = detailPageURL;
    }

    public String getRating() {
        return rating;
    }

    public void setRating(String rating) {
        this.rating = rating;
    }

    public String getTotalReviews() {
        return totalReviews;
    }

    public void setTotalReviews(String totalReviews) {
        this.totalReviews = totalReviews;
    }

    public String getSubtitle() {
        return subtitle;
    }

    public void setSubtitle(String subtitle) {
        this.subtitle = subtitle;
    }

    public String getIsPrimeEligible() {
        return isPrimeEligible;
    }

    public void setIsPrimeEligible(String isPrimeEligible) {
        this.isPrimeEligible = isPrimeEligible;
    }
}

改造实例

package com.example.retrofittutorial;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitInstance {
    private static Retrofit retrofit;
    private static final String BASE_URL = "https://amazon-price1.p.rapidapi.com";

    public static Retrofit getRetrofitInstance(){
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .client(client)
                .build();
        return retrofit;
    }
}

接口

package com.example.retrofittutorial;

import java.util.List;

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Header;
import retrofit2.http.Query;

public interface JsonPlaceholderAPI {

    @GET("search")
    Call<List<AmazonShopItems>> getASI(@Query("keywords") String keywords,
                                       @Query("marketplace") String marketplace,
                                       @Header("x-rapidapi-host") String hostheader,
                                       @Header("x-rapidapi-key") String keyheader);
}

主要活动

package com.example.retrofittutorial;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        testSearch();
    }

    private void testSearch(){
        JsonPlaceholderAPI retrofitInterface = RetrofitInstance.getRetrofitInstance().create(JsonPlaceholderAPI.class);
        Call<List<AmazonShopItems>> listCall = retrofitInterface.getASI("laptop", "US","amazon-price1.p.rapidapi.com", "//api key hidden for privacy");
        listCall.enqueue(new Callback<List<AmazonShopItems>>() {
            @Override
            public void onResponse(Call<List<AmazonShopItems>> call, Response<List<AmazonShopItems>> response) {
                System.out.println("PRINTTTT");
                System.out.println(response.body());
            }

            @Override
            public void onFailure(Call<List<AmazonShopItems>> call, Throwable t) {
                System.out.println("FAILED");
                System.out.println(call);
            }
        });
    }
}

这是我对System.out.println(response.body());

的输出
2020-07-08 04:01:04.040 10382-10382/com.example.retrofittutorial I/System.out: [com.example.retrofittutorial.AmazonShopItems@c374639, com.example.retrofittutorial.AmazonShopItems@404747e, com.example.retrofittutorial.AmazonShopItems@8d82ddf, com.example.retrofittutorial.AmazonShopItems@b7faa2c, com.example.retrofittutorial.AmazonShopItems@53d0cf5, com.example.retrofittutorial.AmazonShopItems@f6f5a8a, com.example.retrofittutorial.AmazonShopItems@bbc8fb, com.example.retrofittutorial.AmazonShopItems@4533d18, com.example.retrofittutorial.AmazonShopItems@c9e8371, com.example.retrofittutorial.AmazonShopItems@5f09556]

输出应该是这样的吗?如何更改它,以便获得可以用于我的应用程序的实际有意义的数据?

0 个答案:

没有答案