- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在开发一个应用程序,我正在使用改造 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);
}
}
我的Retrofit客户端类如下:
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 数组。
请告诉我该怎么做。我哪里错了。
提前谢谢你。
最佳答案
所以我认为你的问题是你正在这样做
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);
}
});
关于Android Retrofit 2 自定义 JSON 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46238284/
我正在尝试迁移我的应用程序以使用 RxJava。我已经在使用 Retrofit,因此我正在尝试使用方法返回 Observables 的 Retrofit 接口(interface)。但是我现在在针对它
我想 post 数据如下: { "user_id":"14545646", "list":["4545645","4545645","4545645","4545645"]
我是改造新手。我向网站发出 POST 请求。网站以 HTML 形式返回响应。所以我会解析它。但是 Retrofit 尝试将其解析为 JSON。怎么办? @FormUrlEncoded @POST("/
我想异步执行 2 个网络调用 - 我正在使用 Retrofit+RxJava 来完成这个。这个逻辑来自一个简单的 Runner 类来测试解决方案。注意:这主要涉及服务器端的 RxJava。 我的代码如
我最近在改造的存储库中使用 Flow。 Result 的密封类 enum class ApiStatus{ SUCCESS, ERROR, LOADING } sealed c
我目前正在开发 Retrofit2 客户端的错误处理部分(使用 Retrofit 的 Rx 实现)。 找了一段时间,没有找到简单的方法将Retrofit返回的ResponseBody反序列化到我报错的
我从 retrofit 更新到 retrofit2 后出现此错误。 FATAL EXCEPTION: OkHttp Dispatcher Process: nz.co.datacom.mars.jun
在使用 RxJava 和 Retrofit 2 时,我正在尝试创建单元测试来覆盖我的应用何时收到特定响应。 我遇到的问题是,在 Retrofit 2 中,我看不到在不使用反射的情况下创建 retrof
在 Retrofit 1.9.x 中有一个 RetrofitError.Kind.NETWORK这让您可以轻松确定故障是否由网络错误引起。在新的 2.0 API 中,我们不再有 RetrofitErr
有没有办法用Refit动态输入参数? 我的 Refit 界面中有这段代码: [Get("/click?{parm}")] Task> SaveClick(string parm
有没有办法用Refit动态输入参数? 我的 Refit 界面中有这段代码: [Get("/click?{parm}")] Task> SaveClick(string parm
我知道,Retrofit 在内部使用 OkHttp。但是,我可以看到一些开发人员提供了以下方法的应用 return new Retrofit.Builder() .baseUrl(Bu
在项目上安装这个库之后: compile 'io.reactivex.rxjava2:rxandroid:2.0.1' compile 'io.reactivex.rxjava2:rxjava:2.0
在 Retrofit 1.x 中,我使用以下模式创建 API 服务类,该类模拟某些构建变体的不良网络连接。 // Retrofit 1 private T create(Class apiServi
Retrofit请求API格式(Android): @POST("getOrderStatus") @Headers("Content-Type:application/json") Obser
当我实例化 RestAdapter 时,我的应用程序总是崩溃 private void submitForm(SignupForm form){ RestAdapter adapter = n
我正要从 retrofit 1.9 迁移到最新版本并遇到问题... 我的设置: 三星 Galaxy S7 Edge(Android 7) 改造 2.3 OkHttp 3.8 迁移后突然遇到这个问题:
我正在尝试在 2.5.1-SNAPSHOT 中使用 retrofit 的协程支持,但我不断收到一个奇怪的异常。 我的改造服务类有: @GET("weather") suspend fun getFor
我正在尝试 Ktor通过转换一些当前正在使用的现有项目 Retrofit . 虽然我可以很容易地将请求转换为: client.get { url("$BASE_URL/something/so
使用改造 2,如何为上传的文件设置动态名称? 目前是这样的: @Part("avatar\"; filename=\"image\" ") RequestBody image, 但是,上传的文件名将是
我是一名优秀的程序员,十分优秀!