【问题标题】:Android Retrofit 2 Custom JSON requestAndroid Retrofit 2 自定义 JSON 请求
【发布时间】:2017-09-15 11:15:02
【问题描述】:

我正在使用改造 2 开发应用程序。 我的请求 JSON 如下

{
    "FirstName":"ABC",
    "LastName":"XYZ",
    "Email":"abc@xyz.com",
    "Password":123456",
    "PhoneNo":"1234567890",
    "Country":"1",
    "State":"1",
    "City":"1",
    "Town":"YYYYYY",
    "Details":[{
        "Education":"BE",
        "Sports":"Cricket",
        "Hobby":"TV" 
    }]
}

我的 API 服务调用如下

public interface APIService {
    @POST("/signup")
    Call<SignUpResponseModel> signUp(@Body SignUpRequestParent body);
}

我的 API Util 类如下

public class ApiUtils {
    private ApiUtils() {}
    public static final String BASE_URL = "http://URL/";

    public static APIService getAPIService() {
        return RetrofitClient.getClient(BASE_URL).create(APIService.class);
    }
}

我的改造客户端类如下:

public class RetrofitClient {
    private static Retrofit retrofit = null;
    public static Retrofit getClient(String baseUrl) {
        if (retrofit==null) {
            retrofit = new Retrofit.Builder()
                    .client(getHeader())
                    .baseUrl(baseUrl)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }

    public static OkHttpClient getHeader() {
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient okClient = new OkHttpClient.Builder()
                .addInterceptor(interceptor)
                .addNetworkInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Interceptor.Chain chain) throws IOException {
                        Request request = null;
                        Request original = chain.request();
                        // Request customization add request headers
                        Request.Builder requestBuilder = original.newBuilder()
                            .addHeader("Authorization", "auth")
                        request = requestBuilder.build();
                        return chain.proceed(request);
                    }
                }).build();
        return okClient;
    }
}

我的请求模型类如下:

public class SignUpRequestParent {
    final String FirstName;
    final String LastName;
    final String Email;
    final String Password;
    final String PhoneNo;
    final String Country;
    final String State;
    final String City;
    final String Town;
    final List<SignUpRequestChild> Details;
    public SignUpRequestParent(String FirstName, String LastName, String Email, String Password, String PhoneNo, String Country, String State, String City, String Town, List<SignUpRequestChild> Details) {
        this.FirstName = FirstName;
        this.LastName = LastName;
        this.Email = Email;
        this.Password = Password;
        this.PhoneNo = PhoneNo;
        this.Country = Country;
        this.State = State;
        this.City = City;
        this.Town = Town;
        this.Details = Details;
    }
}

public class SignUpRequestChild {
    final String Education;
    final String Sports;
    final String Hobby;

    public SignUpRequestChild(String Education, String Sports, String Hobby) {
        this.Education = Education;
        this.Sports = Sports;
        this.Hobby = Hobby;
    }
}

下面是我得到错误的网络服务的调用代码。

mAPIService.signUp(new SignUpRequestParent(name, surName, email, password, mobileNumber, country, state, city, Town, new SignUpRequestChild(Education,Sports,Hobby))).enqueue(new Callback<SignUpResponseModel>() {
    @Override
    public void onResponse(Call<SignUpResponseModel> call, Response<SignUpResponseModel> response) {
        if(response.isSuccessful()) {                                                                        
            Log.e("SignUpResponse", response.body().toString());
        }
    }
    @Override
    public void onFailure(Call<SignUpResponseModel> call, Throwable t) {
        Log.e("RetroFit", ""+t);
    }
});

调用代码的第一行给出了错误。因为我不知道如何从类中创建嵌套的 JSON 数组。

请告诉我该怎么做。我错在哪里。

提前谢谢你。

【问题讨论】:

  • 能否请您添加错误的堆栈跟踪?
  • 也是 SignUpResponseModel 定义
  • @cgomezmendez - 我无法打电话。编码时的错误是:将第 11 个参数转换为 Java.util.List。或将第 11 个参数更改为方法“SignUpRequestParent”,从 List 更改为 SignUpRequestChild。
  • 我在任何地方都看不到第 11 个参数。
  • 对不起它的第 10 个参数。

标签: android json retrofit retrofit2


【解决方案1】:

所以我认为你的问题是你正在这样做

mAPIService.signUp(new SignUpRequestParent(name, surName,
 email, password, mobileNumber, country, state, city,
  Town, new SignUpRequestChild(Education,Sports,Hobby))).enqueue(new Callback<SignUpResponseModel>() {
    @Override
    public void onResponse(Call<SignUpResponseModel> call,
     Response<SignUpResponseModel> response) {
        if(response.isSuccessful()) {                                                                        
            Log.e("SignUpResponse", response.body().toString());
        }
    }
    @Override
    public void onFailure(Call<SignUpResponseModel> call, Throwable t) {
        Log.e("RetroFit", ""+t);
    }
});

但是你的

new SignUpRequestChild(Education,Sports,Hobby))

根据你的构造函数,它应该是一个列表,所以你真的应该这样做

List childs = new ArrayList<SignUpRequestChild>();
childs.add(new SignUpRequestChild(Education,Sports,Hobby)));
mAPIService.signUp(new SignUpRequestParent(name, surName,
 email, password, mobileNumber, country, state, city,
  Town, childs).enqueue(new Callback<SignUpResponseModel>() {
    @Override
    public void onResponse(Call<SignUpResponseModel> call,
     Response<SignUpResponseModel> response) {
        if(response.isSuccessful()) {                                                                        
            Log.e("SignUpResponse", response.body().toString());
        }
    }
    @Override
    public void onFailure(Call<SignUpResponseModel> call, Throwable t) {
        Log.e("RetroFit", ""+t);
    }
});

【讨论】:

【解决方案2】:

您应该提供带有错误的堆栈跟踪。从略读代码中我可以看到,SignUpRequestParent 构造函数接受一个 SignUpRequestChild 对象列表,而您在进行 api 调用时传递了一个新的 SignUpRequestChild 对象。

【讨论】:

    【解决方案3】:

    我觉得你叫错了,试试这个

    将此添加到您的 RetrofitClient 类中

      public static APIService getService() {
        return getClient().create(APIService.class);
      }
    

    然后从任何地方调用它

    RetrofitClient.getService.someMethodHere().enqueue ... 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-11
      • 2016-05-31
      • 2015-12-11
      • 1970-01-01
      相关资源
      最近更新 更多