【问题标题】:Failed to invoke public android.app.Service() with no args无法调用没有参数的公共 android.app.Service()
【发布时间】:2020-11-07 23:59:43
【问题描述】:

当我尝试从 API 改造响应中获取数据时,只有当 service Array 有数据时,如果它为空响应是成功的并且有其他所有数据,但是当 >service Array 对失败方法进行数据改造给出此消息:Failed to invoke public android.app.Service() with no args

第一次进行改造,卡在这里,累了,请提前致谢

服务数组有 JSON 格式的数据出错

 {
    id: 1,
    profile_image: "post_images/1594969413profile.jpg",
    firstname: "bilawal",
    lastname: "jabbar",
    address: "Sansfrasico",

    blog: [
    {
    id: 1,
    artist_profile_id: 1,
    title: "dsd",
    description: "sdsd",
    photo: "http://ec2-54-161-107-128.compute-1.server.com/post_images/1594811497blog.jpg",
    likes: 0,
    created_at: "15/07/20",
    like_link: "http://ec2-54-161-107-128.compute-1.server.com/api/like_blog/2",
    artist_image: "http://ec2-54-161-10128.compute1.server.com/post_images/1594969413profile.jpg",
    single_blog_link: "http://ec2-54-161-107-128.compute-1.server.com/api/blog/2"
    }],

    services: [
    {
    name: "test",
    details: "asasasasas",
    feature_image: "http://ec2-54-161-107-128.comp1.server.com/post_images/1594987588feature.jpg",
    price: 2,
    price_type: "Iix",
    rating: "No Yet Rating"
    }]
    }

当服务数组没有 JSON 格式的数据时工作顺利无错误

{
id: 2,
profile_image: "no",
firstname: "Hamza",
lastname: "Dj Yo",
address: "no",

blog: [ ],

services: [ ]
}

服务类

package com.example.djikon;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

    public class Service {
    
        @SerializedName("name")
        @Expose
        private String name;
        @SerializedName("details")
        @Expose
        private String details;
        @SerializedName("feature_image")
        @Expose
        private String featureImage;
        @SerializedName("price")
        @Expose
        private Integer price;
        @SerializedName("price_type")
        @Expose
        private String priceType;
        @SerializedName("rating")
        @Expose
        private String rating;
    
        /**
         * No args constructor for use in serialization
         *
         */
        public Service() {
        }
    
    
        public Service(String name, String details, String featureImage, Integer price, String priceType, String rating) {
            super();
            this.name = name;
            this.details = details;
            this.featureImage = featureImage;
            this.price = price;
            this.priceType = priceType;
            this.rating = rating;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getDetails() {
            return details;
        }
    
        public void setDetails(String details) {
            this.details = details;
        }
    
        public String getFeatureImage() {
            return featureImage;
        }
    
        public void setFeatureImage(String featureImage) {
            this.featureImage = featureImage;
        }
    
        public Integer getPrice() {
            return price;
        }
    
        public void setPrice(Integer price) {
            this.price = price;
        }
    
        public String getPriceType() {
            return priceType;
        }
    
        public void setPriceType(String priceType) {
            this.priceType = priceType;
        }
    
        public String getRating() {
            return rating;
        }
    
        public void setRating(String rating) {
            this.rating = rating;
        }
    
    }

如果需要,还有主要对象类

package com.example.djikon;

import android.app.Service;



import java.util.List;


    public class DJProfileModel {
    
    
        private Integer id;
    
        private String profile_image;
    
        private String firstname;
    
        private String lastname;
    
        private String address;
    
        private List<Blog_Model> blog;
    
        private List<Service> services;
    
        /**
         * No args constructor for use in serialization
         *
         * @param body
         */
        public DJProfileModel(DJProfileModel body) {
        }
    
    
        public DJProfileModel(Integer id, String profileImage, String firstname, String lastname, String address, List<Blog_Model> blog, List<Service> services) {
            super();
            this.id = id;
            this.profile_image = profileImage;
            this.firstname = firstname;
            this.lastname = lastname;
            this.address = address;
            this.blog = blog;
            this.services = services;
        }
    
        public Integer getId() {
            return id;
        }
    
        public String getProfile_image() {
            return profile_image;
        }
    
        public String getFirstname() {
            return firstname;
        }
    
        public String getLastname() {
            return lastname;
        }
    
        public String getAddress() {
            return address;
        }
    
        public List<Blog_Model> getBlog() {
            return blog;
        }
    
        public List<Service> getServices() {
            return services;
        }
    }

【问题讨论】:

    标签: android json api retrofit


    【解决方案1】:

    我在使用 'Here'JSONCONVERT 自动生成类时遇到了这个问题。为了解决这个问题,我为这个 Service Response Array 创建了自己的类。现在运行顺利。

    这是我的手工课:

    package com.example.djikon;
    
    public class model {
        private String name,
        details,
        feature_image,
        price_type,
        rating;
    
        private int price;
    
        public model(String name, String details, String feature_image, int price, String price_type, String rating) {
            this.name = name;
            this.details = details;
            this.feature_image = feature_image;
            this.price = price;
            this.price_type = price_type;
            this.rating = rating;
        }
    
        public String getName() {
            return name;
        }
    
        public String getDetails() {
            return details;
        }
    
        public String getFeature_image() {
            return feature_image;
        }
    
        public int getPrice() {
            return price;
        }
    
        public String getPrice_type() {
            return price_type;
        }
    
        public String getRating() {
            return rating;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-26
      • 2018-08-23
      • 2017-06-05
      • 1970-01-01
      • 2014-04-22
      • 1970-01-01
      • 2019-07-28
      • 2018-04-28
      相关资源
      最近更新 更多