使用翻新功能在Json中获取数据

时间:2019-03-27 07:07:07

标签: android json retrofit retrofit2

预期为BEGIN_ARRAY,但位于第3行第26列的路径$ [0] .data

public class MainActivity extends AppCompatActivity {

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

        api_interface=ApiClient.getClient().create(Api_Interface.class);

        Call<List<CountryClas>> call=api_interface.getcountry("GetCountry","727","cl1oEntQ32PxZsS3VJnC+H+CY5oLfFLRU5j1H4bg+1g=");




      call.enqueue(new Callback<List<CountryClas>>() {
            @Override
            public void onResponse(Call<List<CountryClas>> call, Response<List<CountryClas>> response) {
                Log.e("Res",">>>>>>"+response.body());
                }

            @Override
            public void onFailure(Call<List<CountryClas>> call, Throwable t) {
                Log.e("Error",">>>>>>"+t.getMessage());
            }
        });
    }

}

InterFace

public interface Api_Interface {

@GET("json.php")
Call<List<CountryClas>> getcountry(@Query("action") String action, @Query("umID") String umID
        , @Query("OauthToken") String OauthToken);

}

ApiClient

public class ApiClient {
    private static final String BaseUrl="http://23.227.133.210/consultapro/";

    private static Retrofit retrofit=null;

    public static Retrofit getClient(){
        if (retrofit==null){
            retrofit=new Retrofit.Builder()
                    .baseUrl(BaseUrl)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
}

模型类

public class CountryClas {

    @SerializedName("status")
    @Expose
    private Boolean status;
    @SerializedName("data")
    @Expose
    private List<Datum> data = null;
    @SerializedName("message")
    @Expose
    private String message;

    public Boolean getStatus() {
        return status;
    }

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

    public List<Datum> getData() {
        return data;
    }

    public void setData(List<Datum> data) {
        this.data = data;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

}

第二个模型课程

public class Datum {

    @SerializedName("Country_Id")
    @Expose
    private String countryId;
    @SerializedName("Country_Name")
    @Expose
    private String countryName;
    @SerializedName("Country_Code")
    @Expose
    private String countryCode;

    public String getCountryId() {
        return countryId;
    }

    public void setCountryId(String countryId) {
        this.countryId = countryId;
    }

    public String getCountryName() {
        return countryName;
    }

    public void setCountryName(String countryName) {
        this.countryName = countryName;
    }

    public String getCountryCode() {
        return countryCode;
    }

    public void setCountryCode(String countryCode) {
        this.countryCode = countryCode;
    }

}

// Json响应

[     {         “状态”:是,         “数据”:[             {                 “ Country_Id”:“ 101”,                 “ Country_Name”:“印度”,                 “国家/地区代码”:“ 91”             },             {                 “ Country_Id”:“ 231”,                 “ Country_Name”:“美国”,                 “ Country_Code”:“ 1”             },             {                 “ Country_Id”:“ 230”,                 “ Country_Name”:“英国”,                 “国家/地区代码”:“ 44”             }       ],         “ message”:“找到国家列表”。     } ]

5 个答案:

答案 0 :(得分:1)

“预期BEGIN_ARRAY但为STRING”表示您定义了其数据类型的成员变量不匹配,预期为 String [] ,并且您可能使用了 Sting < / strong>,请重新检查。

答案 1 :(得分:0)

这意味着您的Json数据模型错误。如果您不知道出什么问题,只需使用一些在线json到Java模型转换器即可生成模型。

尝试这个converter

如果您发布正确的Json字符串,此模型应该可以正常工作

class Country {
@SerializedName("status")
private Boolean status;

@SerializedName("data")
private List<Data> data = null;

@SerializedName("message")
private String message;

public class Data {
    @SerializedName("Country_Id")
    private String countryId;

    @SerializedName("Country_Name")
    private String countryName;

    @SerializedName("Country_Code")
    private String countryCode;
}}

答案 2 :(得分:0)

我尝试在不进行身份验证的情况下连接您的api

http://23.227.133.210/consultapro/json.php?action=getCountry

然后我得到这样的答复

[{"status":false,"data":"-->getCountry-->3JfalKdsUf15fsfGIwjcXg== -->","message":"User is not authenticated."}]

在数据中,它返回String而不是ARRAY

所以我认为因为您请求的身份验证无效,这会使您的模型不正确-> JsonParseException

对不起,我的英语不好

答案 3 :(得分:0)

您可以使用Gson将响应主体转换为Java Objects / List。参见下面的代码示例

req.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {

            BufferedReader reader = null;
            StringBuilder sb = new StringBuilder();
            reader = new BufferedReader(new InputStreamReader(response.body().byteStream()));
            String line;
            try {
                while ((line = reader.readLine()) != null) {
                    sb.append(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            String result = sb.toString();

            ApiResponse apiResponse = new Gson().fromJson(result, ApiResponse.class);

答案 4 :(得分:0)

useEffect(() => {
    const { getStoreUsers, shopUsers } = props;
    setLoading(true);
    getStoreUsers().then(() => {
      setLoading(false);
    }).catch(() => {
      setLoading(false);
    });
  }, [shopUsers]);