当前位置: 首页 > 知识库问答 >
问题:

我在改造RESTAPI调用中得到了Null body响应,但在postman中效果很好

沃阳飙
2023-03-14

我在Android中通过改装调用RestAPI,每一个其他请求工作正常,并给我有效的响应,但此请求加载有点慢,需要时间加载,但当我得到响应其代码为200,但响应体为空等了一段时间后。

public interface CSCartRestApi {
@GET("products?items_per_page=1")
Call<Product> getProducts();
}

这是OkkHttp客户端,它的加载也扩展到加载json,因为它很慢。

public class APIClient {
OkHttpClient authClient=new OkHttpClient
                            .Builder()
                            .connectTimeout(50, TimeUnit.SECONDS)
                            .readTimeout(30, TimeUnit.SECONDS)
                            .callTimeout(30, TimeUnit.SECONDS)
                             .retryOnConnectionFailure(false)
                            .addInterceptor(new 
BasicAuthInterceptor(username, password))
                            .build();
}

在这个方法中调用API

private void getTopRatedProducts() {
    Call<Product> productssCall=apiService.getProducts();
    productssCall.enqueue(new Callback<Product>() {
        @Override
        public void onResponse(Call<Product> call, Response<Product> response) {
            Log.d("heloto", "onResponse: "+new GsonBuilder().setPrettyPrinting().create().toJson(response));

           // Log.d("heloto", "onResponse: "+response.errorBody().toString());

        }

        @Override
        public void onFailure(Call<Product> call, Throwable t) {

        }
    });
}

这是我的反应体。。。。有人能帮我解释一下为什么我在这里得到了空身体吗。这是回应

2020-03-23 11:52:12.823 5780-5780/com.ws.design.coco_ecommerce_ui_kit D/heloto: onResponse: {
  "body": {},
  "rawResponse": {
    "body": {
      "contentLength": -1,
      "contentType": {
        "mediaType": "application/json",
        "subtype": "json",
        "type": "application"
      }
    },
    "code": 200,
    "headers": {
      "namesAndValues": [
        "Date",
        "Mon, 23 Mar 2020 06:51:59 GMT",
        "Server",
        "Apache",
        "Vary",
        "Accept-Encoding,User-Agent",
        "Keep-Alive",
        "timeout\u003d5, max\u003d100",
        "Connection",
        "Keep-Alive",
        "Content-Type",
        "application/json"
      ]
    },
    "message": "OK",
    "networkResponse": {
      "code": 200,
      "headers": {
        "namesAndValues": [
          "Date",
          "Mon, 23 Mar 2020 06:51:59 GMT",
          "Server",
          "Apache",
          "Vary",
          "Accept-Encoding,User-Agent",
          "Content-Encoding",
          "gzip",
          "Content-Length",
          "1710",
          "Keep-Alive",
          "timeout\u003d5, max\u003d100",
          "Connection",
          "Keep-Alive",
          "Content-Type",
          "application/json"
        ]
      },
      "message": "OK",
      "protocol": "HTTP_1_1",
      "receivedResponseAtMillis": 1584946332654,
      "request": {
        "cacheControl": {
          "immutable": false,
          "isPrivate": false,
          "isPublic": false,
          "maxAgeSeconds": -1,
          "maxStaleSeconds": -1,
          "minFreshSeconds": -1,
          "mustRevalidate": false,
          "noCache": false,
          "noStore": false,
          "noTransform": false,
          "onlyIfCached": false,
          "sMaxAgeSeconds": -1
        },
        "headers": {
          "namesAndValues": [
            "Authorization",
            "Basic bXVhemJ1dHQ5N0BnbWFpbC5jb206VjA0SDM1a1Q1SkNzSVQwVmMzODBMN3VZQjZmM0YzOTM\u003d",
            "Host",
            "www.awintoo.com",
            "Connection",
            "Keep-Alive",
            "Accept-Encoding",
            "gzip",
            "User-Agent",
            "okhttp/3.12.2"
          ]
        },
        "method": "GET",
        "tags": {
          "class retrofit2.Invocation": {
            "arguments": [],
            "method": {
              "artMethod": 2704825648,
              "override": false
            }
          }
        },
        "url": {
          "host": "www.awintoo.com",
          "password": "",
          "pathSegments": [
            "api",
            "products"
          ],
          "port": 80,
          "queryNamesAndValues": [
            "items_per_page",
            "1"
          ],
          "scheme": "http",
          "url": "http://www.awintoo.com/api/products?items_per_page\u003d1",
          "username": ""
        }
      },
      "sentRequestAtMillis": 1584946319635
    },
    "protocol": "HTTP_1_1",
    "receivedResponseAtMillis": 1584946332654,
    "request": {
      "headers": {
        "namesAndValues": [
          "Authorization",
          "Basic bXVhemJ1dHQ5N0BnbWFpbC5jb206VjA0SDM1a1Q1SkNzSVQwVmMzODBMN3VZQjZmM0YzOTM\u003d"
        ]
      },
      "method": "GET",
      "tags": {
        "class retrofit2.Invocation": {
          "arguments": [],
          "method": {
            "artMethod": 2704825648,
            "override": false
          }
        }
      },
      "url": {
        "host": "www.awintoo.com",
        "password": "",
        "pathSegments": [
          "api",
          "products"
        ],
        "port": 80,
        "queryNamesAndValues": [
          "items_per_page",
          "1"
        ],
        "scheme": "http",
        "url": "http://www.awintoo.com/api/products?items_per_page\u003d1",
        "username": ""
      }
    },
    "sentRequestAtMillis": 1584946319635
  }
}

这是Mode类产品。

public class Product{

@SerializedName("product_id")
@Expose
private String productId;
@SerializedName("product")
@Expose
private String product;
@SerializedName("product_type")
@Expose
private String productType;
@SerializedName("parent_product_id")
@Expose
private String parentProductId;
@SerializedName("product_code")
@Expose
private String productCode;
@SerializedName("status")
@Expose
private String status;
@SerializedName("company_id")
@Expose
private String companyId;
@SerializedName("list_price")
@Expose
private String listPrice;
@SerializedName("amount")
@Expose
private String amount;
@SerializedName("weight")
@Expose
private String weight;
@SerializedName("length")
@Expose
private String length;
@SerializedName("width")
@Expose
private String width;
@SerializedName("height")
@Expose
private String height;
@SerializedName("shipping_freight")
@Expose
private String shippingFreight;
@SerializedName("low_avail_limit")
@Expose
private String lowAvailLimit;
@SerializedName("timestamp")
@Expose
private String timestamp;
@SerializedName("updated_timestamp")
@Expose
private String updatedTimestamp;
@SerializedName("usergroup_ids")
@Expose
private String usergroupIds;
@SerializedName("is_edp")
@Expose
private String isEdp;
@SerializedName("edp_shipping")
@Expose
private String edpShipping;
@SerializedName("unlimited_download")
@Expose
private String unlimitedDownload;
@SerializedName("tracking")
@Expose
private String tracking;
@SerializedName("free_shipping")
@Expose
private String freeShipping;
@SerializedName("zero_price_action")
@Expose
private String zeroPriceAction;
@SerializedName("is_pbp")
@Expose
private String isPbp;
@SerializedName("is_op")
@Expose
private String isOp;
@SerializedName("is_oper")
@Expose
private String isOper;
@SerializedName("is_returnable")
@Expose
private String isReturnable;
@SerializedName("return_period")
@Expose
private String returnPeriod;
@SerializedName("avail_since")
@Expose
private String availSince;
@SerializedName("out_of_stock_actions")
@Expose
private String outOfStockActions;
@SerializedName("localization")
@Expose
private String localization;
@SerializedName("min_qty")
@Expose
private String minQty;
@SerializedName("max_qty")
@Expose
private String maxQty;
@SerializedName("qty_step")
@Expose
private String qtyStep;
@SerializedName("list_qty_count")
@Expose
private String listQtyCount;
@SerializedName("tax_ids")
@Expose
private String taxIds;
@SerializedName("age_verification")
@Expose
private String ageVerification;
@SerializedName("age_limit")
@Expose
private String ageLimit;
@SerializedName("options_type")
@Expose
private String optionsType;
@SerializedName("exceptions_type")
@Expose
private String exceptionsType;
@SerializedName("details_layout")
@Expose
private String detailsLayout;
@SerializedName("shipping_params")
@Expose
private String shippingParams;
@SerializedName("facebook_obj_type")
@Expose
private String facebookObjType;
@SerializedName("__variation_code")
@Expose
private Object variationCode;
@SerializedName("__variation_options")
@Expose
private Object variationOptions;
@SerializedName("__is_default_variation")
@Expose
private String isDefaultVariation;
@SerializedName("buy_now_url")
@Expose
private String buyNowUrl;
@SerializedName("url")
@Expose
private String url;
@SerializedName("upc")
@Expose
private String upc;
@SerializedName("price")
@Expose
private String price;
@SerializedName("category_ids")
@Expose
private List<Integer> categoryIds = null;
@SerializedName("seo_name")
@Expose
private String seoName;
@SerializedName("seo_path")
@Expose
private String seoPath;
@SerializedName("main_category")
@Expose
private Integer mainCategory;
@SerializedName("variation_feature_ids")
@Expose
private VariationFeatureIds variationFeatureIds;
@SerializedName("variation_feature_collection")
@Expose
private VariationFeatureCollection variationFeatureCollection;
@SerializedName("variation_group_id")
@Expose
private Integer variationGroupId;
@SerializedName("variation_group_code")
@Expose
private String variationGroupCode;
@SerializedName("variation_parent_product_id")
@Expose
private Integer variationParentProductId;
@SerializedName("variation_sub_group_id")
@Expose
private String variationSubGroupId;
@SerializedName("variation_features")
@Expose
private VariationFeatures variationFeatures;
@SerializedName("variation_name")
@Expose
private String variationName;
@SerializedName("main_pair")
@Expose
private MainPair mainPair;
@SerializedName("image_pairs")
@Expose
private ImagePairs imagePairs;
@SerializedName("base_price")
@Expose
private String basePrice;
@SerializedName("selected_options")
@Expose
private List<Object> selectedOptions = null;
@SerializedName("has_options")
@Expose
private Boolean hasOptions;
@SerializedName("product_options")
@Expose
private List<Object> productOptions = null;
@SerializedName("discounts")
@Expose
private Discounts discounts;
@SerializedName("product_features")
@Expose
private ProductFeatures productFeatures;
@SerializedName("qty_content")
@Expose
private List<Object> qtyContent = null;
@SerializedName("premoderation_reason")
@Expose
private String premoderationReason;

public String getProductId() {
    return productId;
}

public void setProductId(String productId) {
    this.productId = productId;
}

public String getProduct() {
    return product;
}

public void setProduct(String product) {
    this.product = product;
}

public String getProductType() {
    return productType;
}

public void setProductType(String productType) {
    this.productType = productType;
}

public String getParentProductId() {
    return parentProductId;
}

public void setParentProductId(String parentProductId) {
    this.parentProductId = parentProductId;
}

public String getProductCode() {
    return productCode;
}

public void setProductCode(String productCode) {
    this.productCode = productCode;
}

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

public String getCompanyId() {
    return companyId;
}

public void setCompanyId(String companyId) {
    this.companyId = companyId;
}

public String getListPrice() {
    return listPrice;
}

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

public String getAmount() {
    return amount;
}

public void setAmount(String amount) {
    this.amount = amount;
}

public String getWeight() {
    return weight;
}

public void setWeight(String weight) {
    this.weight = weight;
}

public String getLength() {
    return length;
}

public void setLength(String length) {
    this.length = length;
}

public String getWidth() {
    return width;
}

public void setWidth(String width) {
    this.width = width;
}

public String getHeight() {
    return height;
}

public void setHeight(String height) {
    this.height = height;
}

public String getShippingFreight() {
    return shippingFreight;
}

public void setShippingFreight(String shippingFreight) {
    this.shippingFreight = shippingFreight;
}

public String getLowAvailLimit() {
    return lowAvailLimit;
}

public void setLowAvailLimit(String lowAvailLimit) {
    this.lowAvailLimit = lowAvailLimit;
}

public String getTimestamp() {
    return timestamp;
}

public void setTimestamp(String timestamp) {
    this.timestamp = timestamp;
}

public String getUpdatedTimestamp() {
    return updatedTimestamp;
}

public void setUpdatedTimestamp(String updatedTimestamp) {
    this.updatedTimestamp = updatedTimestamp;
}

public String getUsergroupIds() {
    return usergroupIds;
}

public void setUsergroupIds(String usergroupIds) {
    this.usergroupIds = usergroupIds;
}

public String getIsEdp() {
    return isEdp;
}

public void setIsEdp(String isEdp) {
    this.isEdp = isEdp;
}

public String getEdpShipping() {
    return edpShipping;
}

public void setEdpShipping(String edpShipping) {
    this.edpShipping = edpShipping;
}

public String getUnlimitedDownload() {
    return unlimitedDownload;
}

public void setUnlimitedDownload(String unlimitedDownload) {
    this.unlimitedDownload = unlimitedDownload;
}

public String getTracking() {
    return tracking;
}

public void setTracking(String tracking) {
    this.tracking = tracking;
}

public String getFreeShipping() {
    return freeShipping;
}

public void setFreeShipping(String freeShipping) {
    this.freeShipping = freeShipping;
}

public String getZeroPriceAction() {
    return zeroPriceAction;
}

public void setZeroPriceAction(String zeroPriceAction) {
    this.zeroPriceAction = zeroPriceAction;
}

public String getIsPbp() {
    return isPbp;
}

public void setIsPbp(String isPbp) {
    this.isPbp = isPbp;
}

public String getIsOp() {
    return isOp;
}

public void setIsOp(String isOp) {
    this.isOp = isOp;
}

public String getIsOper() {
    return isOper;
}

public void setIsOper(String isOper) {
    this.isOper = isOper;
}

public String getIsReturnable() {
    return isReturnable;
}

public void setIsReturnable(String isReturnable) {
    this.isReturnable = isReturnable;
}

public String getReturnPeriod() {
    return returnPeriod;
}

public void setReturnPeriod(String returnPeriod) {
    this.returnPeriod = returnPeriod;
}

public String getAvailSince() {
    return availSince;
}

public void setAvailSince(String availSince) {
    this.availSince = availSince;
}

public String getOutOfStockActions() {
    return outOfStockActions;
}

public void setOutOfStockActions(String outOfStockActions) {
    this.outOfStockActions = outOfStockActions;
}

public String getLocalization() {
    return localization;
}

public void setLocalization(String localization) {
    this.localization = localization;
}

public String getMinQty() {
    return minQty;
}

public void setMinQty(String minQty) {
    this.minQty = minQty;
}

public String getMaxQty() {
    return maxQty;
}

public void setMaxQty(String maxQty) {
    this.maxQty = maxQty;
}

public String getQtyStep() {
    return qtyStep;
}

public void setQtyStep(String qtyStep) {
    this.qtyStep = qtyStep;
}

public String getListQtyCount() {
    return listQtyCount;
}

public void setListQtyCount(String listQtyCount) {
    this.listQtyCount = listQtyCount;
}

public String getTaxIds() {
    return taxIds;
}

public void setTaxIds(String taxIds) {
    this.taxIds = taxIds;
}

public String getAgeVerification() {
    return ageVerification;
}

public void setAgeVerification(String ageVerification) {
    this.ageVerification = ageVerification;
}

public String getAgeLimit() {
    return ageLimit;
}

public void setAgeLimit(String ageLimit) {
    this.ageLimit = ageLimit;
}

public String getOptionsType() {
    return optionsType;
}

public void setOptionsType(String optionsType) {
    this.optionsType = optionsType;
}

public String getExceptionsType() {
    return exceptionsType;
}

public void setExceptionsType(String exceptionsType) {
    this.exceptionsType = exceptionsType;
}

public String getDetailsLayout() {
    return detailsLayout;
}

public void setDetailsLayout(String detailsLayout) {
    this.detailsLayout = detailsLayout;
}

public String getShippingParams() {
    return shippingParams;
}

public void setShippingParams(String shippingParams) {
    this.shippingParams = shippingParams;
}

public String getFacebookObjType() {
    return facebookObjType;
}

public void setFacebookObjType(String facebookObjType) {
    this.facebookObjType = facebookObjType;
}

public Object getVariationCode() {
    return variationCode;
}

public void setVariationCode(Object variationCode) {
    this.variationCode = variationCode;
}

public Object getVariationOptions() {
    return variationOptions;
}

public void setVariationOptions(Object variationOptions) {
    this.variationOptions = variationOptions;
}

public String getIsDefaultVariation() {
    return isDefaultVariation;
}

public void setIsDefaultVariation(String isDefaultVariation) {
    this.isDefaultVariation = isDefaultVariation;
}

public String getBuyNowUrl() {
    return buyNowUrl;
}

public void setBuyNowUrl(String buyNowUrl) {
    this.buyNowUrl = buyNowUrl;
}

public String getUrl() {
    return url;
}

public void setUrl(String url) {
    this.url = url;
}

public String getUpc() {
    return upc;
}

public void setUpc(String upc) {
    this.upc = upc;
}

public String getPrice() {
    return price;
}

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

public List<Integer> getCategoryIds() {
    return categoryIds;
}

public void setCategoryIds(List<Integer> categoryIds) {
    this.categoryIds = categoryIds;
}

public String getSeoName() {
    return seoName;
}

public void setSeoName(String seoName) {
    this.seoName = seoName;
}

public String getSeoPath() {
    return seoPath;
}

public void setSeoPath(String seoPath) {
    this.seoPath = seoPath;
}

public Integer getMainCategory() {
    return mainCategory;
}

public void setMainCategory(Integer mainCategory) {
    this.mainCategory = mainCategory;
}

public VariationFeatureIds getVariationFeatureIds() {
    return variationFeatureIds;
}

public void setVariationFeatureIds(VariationFeatureIds variationFeatureIds) {
    this.variationFeatureIds = variationFeatureIds;
}

public VariationFeatureCollection getVariationFeatureCollection() {
    return variationFeatureCollection;
}

public void setVariationFeatureCollection(VariationFeatureCollection variationFeatureCollection) {
    this.variationFeatureCollection = variationFeatureCollection;
}

public Integer getVariationGroupId() {
    return variationGroupId;
}

public void setVariationGroupId(Integer variationGroupId) {
    this.variationGroupId = variationGroupId;
}

public String getVariationGroupCode() {
    return variationGroupCode;
}

public void setVariationGroupCode(String variationGroupCode) {
    this.variationGroupCode = variationGroupCode;
}

public Integer getVariationParentProductId() {
    return variationParentProductId;
}

public void setVariationParentProductId(Integer variationParentProductId) {
    this.variationParentProductId = variationParentProductId;
}

public String getVariationSubGroupId() {
    return variationSubGroupId;
}

public void setVariationSubGroupId(String variationSubGroupId) {
    this.variationSubGroupId = variationSubGroupId;
}

public VariationFeatures getVariationFeatures() {
    return variationFeatures;
}

public void setVariationFeatures(VariationFeatures variationFeatures) {
    this.variationFeatures = variationFeatures;
}

public String getVariationName() {
    return variationName;
}

public void setVariationName(String variationName) {
    this.variationName = variationName;
}

public MainPair getMainPair() {
    return mainPair;
}

public void setMainPair(MainPair mainPair) {
    this.mainPair = mainPair;
}

public ImagePairs getImagePairs() {
    return imagePairs;
}

public void setImagePairs(ImagePairs imagePairs) {
    this.imagePairs = imagePairs;
}

public String getBasePrice() {
    return basePrice;
}

public void setBasePrice(String basePrice) {
    this.basePrice = basePrice;
}

public List<Object> getSelectedOptions() {
    return selectedOptions;
}

public void setSelectedOptions(List<Object> selectedOptions) {
    this.selectedOptions = selectedOptions;
}

public Boolean getHasOptions() {
    return hasOptions;
}

public void setHasOptions(Boolean hasOptions) {
    this.hasOptions = hasOptions;
}

public List<Object> getProductOptions() {
    return productOptions;
}

public void setProductOptions(List<Object> productOptions) {
    this.productOptions = productOptions;
}

public Discounts getDiscounts() {
    return discounts;
}

public void setDiscounts(Discounts discounts) {
    this.discounts = discounts;
}

public ProductFeatures getProductFeatures() {
    return productFeatures;
}

public void setProductFeatures(ProductFeatures productFeatures) {
    this.productFeatures = productFeatures;
}

public List<Object> getQtyContent() {
    return qtyContent;
}

public void setQtyContent(List<Object> qtyContent) {
    this.qtyContent = qtyContent;
}

public String getPremoderationReason() {
    return premoderationReason;
}

public void setPremoderationReason(String premoderationReason) {
    this.premoderationReason = premoderationReason;
}

}

共有1个答案

壤驷敏学
2023-03-14

从代码中重新移动这些行:

.connectTimeout(50, TimeUnit.SECONDS)
                            .readTimeout(30, TimeUnit.SECONDS)
                            .callTimeout(30, TimeUnit.SECONDS)
                             .retryOnConnectionFailure(false)
 类似资料:
  • 这是html文件 当我在浏览器中运行这个时,请求不会被发送(我在后端的跟踪球中看不到任何东西),url工作正常,如果在浏览器和邮递员中使用,这是邮递员请求和响应的截图

  • 错误提示: 通用域名格式。谷歌。格森。JsonSyntaxException:java。lang.IllegalStateException:应为BEGIN\u对象,但在改装中的第1行第2列路径$处为BEGIN\u数组 这是我的职责: 我的回答是

  • 我在后端使用Axios时遇到了问题。这可能是一个非常简单的修复,因为我是新手。 邮递员:对于有效和无效的凭据都收到正确的响应。 axios:对于有效的crendentials会收到正确的响应,但是当输入无效的凭据时,axios方法的catch块会运行。

  • 问题内容: 我想制作一个用C ++编写的服务器来支持我的游戏。我学习了套接字的基础知识,并编写了一个运行良好的基本聊天程序。现在,我想创建一个像Apache这样的HTTP服务器,但是仅用于AJAX请求- 响应部分。 我认为刚开始时,我复制了一个Apache响应文本,并使用C ++服务器程序发送了准确的响应。 问题是浏览器(Firefox)连接到apache,并且一切正常,除了所有请求均得到正确的响

  • 相关问题 < code>Json的响应是< code >新行分隔符Json: 我正在使用来提出请求: 我可以在我的中看到响应,但收到以下错误: 中格式错误的JSON < li >响应在< code>NdJson中,它无法正确解析它 如何正确解析它?

  • 我正在使用改型从http URL获取数据。我的界面类: 我的请求方法。 作为回应,我得到了一个空的身体。服务器的响应是200 OK。 但是当我在浏览器中打开网址时,我在屏幕上看到了JSONObject。