Android 在请求正文中发送阵列不起作用:使用改进版2.1.0

Android 在请求正文中发送阵列不起作用:使用改进版2.1.0,android,web-services,retrofit,Android,Web Services,Retrofit,当以数组形式发送“CustomDishItems”时,它给出的结果是:服务器在处理请求时遇到错误 它适用于:“CustomDishItems”: 以下是我使用PHPWeb服务在服务器上“发布”所需的数据 服务URL: 请求机构: { "AvailableFrom": "", "AvailableQty": "1", "AvailableTill": "Enter custom Date", "AvailableTo": "Jun 15, 2017", "

当以数组形式发送“CustomDishItems”时,它给出的结果是:服务器在处理请求时遇到错误

它适用于:“CustomDishItems”:

以下是我使用PHPWeb服务在服务器上“发布”所需的数据

服务URL:

请求机构:

{
    "AvailableFrom": "",
    "AvailableQty": "1",
    "AvailableTill": "Enter custom Date",
    "AvailableTo": "Jun 15, 2017",
    "Calories": "100",
    "Category": "nonVeg",
    "CreateDate": "May 31, 2017 12:10 PM",
    "CuisineId": "113",
    "CustomDishItems": [{
        "Cost": "100",
        "ItemName": "salted"
    }, {
        "Cost": "120",
        "ItemName": "sweet"
    }],
    "Customizable": "true",
    "Description": "test",
    "DishId": "0",
    "DishName": "test",
    "Ingredeients": "test",
    "MenuTitle": "menu1",
    "PreparingTime": "58",
    "Price": "100",
    "ProfileId": "227",
    "SessionToken": "97801380243422832"
}
Java代码是:

私人空间addDish(){


}我也遇到了同样的问题,我必须将JSONArray传递给注册请求。 这是我得到的解决方案

首先,创建一个名为ApiClient的类,如下所示:

    public class ApiClient {

    private static Retrofit retrofit = null;
    private static String BASE_URl = Urls.BASE_URL;


    public static Retrofit getClient() {
        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
        httpClient.connectTimeout(20 * 1000, TimeUnit.MILLISECONDS);
        httpClient.readTimeout(20 * 1000, TimeUnit.MILLISECONDS);
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        httpClient.addInterceptor(loggingInterceptor);

        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                    .client(httpClient.build())
                    .baseUrl(BASE_URl)
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }

        return retrofit;

    }
}
  • 创建一个接口来调用ApicClient类,如下所示:

    公共接口{

        @POST(Urls.registerUserUrl)
        Observable<LoginResponseEntity> getRegistrationResponse(@Body JsonObject request); // make sure JsonObject should be from com.google.gson
    }
    
    @POST(url.registerUserUrl)
    Observable getRegistrationResponse(@Body JsonObject request);//确保JsonObject应该来自com.google.gson
    }
    
  • 现在调用解析器:

    ApiInterface apiService =
            ApiClient.getClient().create(ApiInterface.class);
    Observable<LoginResponseEntity> call = apiService.getRegistrationResponse(getRegistrationRequestParameters());
    call.subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new DisposableObserver<LoginResponseEntity>() {
                @Override
                public void onNext(LoginResponseEntity loginResponseEntity) {
                    // handle your response here
                }
    
                @Override
                public void onError(Throwable e) {
    
                }
    
                @Override
                public void onComplete() {
                }
            });
    
    
    
    private JsonObject getRegistrationRequestParameters() {
    JSONObject commonParameters = UDF.commonParametersForJson(activity);
    try {
        JSONObject parentInformation = new JSONObject();
        parentInformation.put("firstname", "" + firstName);
        parentInformation.put("lastname", "" + lastName);
        parentInformation.put("contact", "" + phoneNumber);
        parentInformation.put("password", "" + password);
        commonParameters.put("personalinfo", parentInformation);
        JSONObject childObject = null;
        JSONArray childInformation = new JSONArray();
        for (int i = 0; i < alChildRequestEntity.size(); i++) {
            childObject = new JSONObject();
            childObject.put("childname", alChildRequestEntity.get(i).getName());
            childObject.put("pickupstopcode", alChildRequestEntity.get(i).getPickup_stop());
            childObject.put("dropoffstopcode", alChildRequestEntity.get(i).getDrop_stop());
            childInformation.put(childObject);
        }
        commonParameters.put("childinfo", childInformation);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    JsonParser jsonParser = new JsonParser();
    JsonObject gsonObject = (JsonObject) jsonParser.parse(commonParameters.toString());   // make sure JsonObject should be from com.google.gson
    return gsonObject;
    
    api接口apiService=
    ApiClient.getClient().create(apinterface.class);
    可观察调用=apiService.getRegistrationResponse(getRegistrationRequestParameters());
    call.subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .认购(新的可处置观察员){
    @凌驾
    public void onNext(LoginResponseEntity LoginResponseEntity){
    //在这里处理你的回答
    }
    @凌驾
    公共无效申报人(可丢弃的e){
    }
    @凌驾
    未完成的公共空间(){
    }
    });
    私有JsonObject getRegistrationRequestParameters(){
    JSONObject commonParameters=UDF.commonParametersForJson(活动);
    试一试{
    JSONObject parentInformation=新的JSONObject();
    parentInformation.put(“firstname”和“+firstname”);
    parentInformation.put(“lastname”和“+lastname”);
    parentInformation.put(“联系人”、“电话号码”);
    parentInformation.put(“密码”,“密码+”);
    commonParameters.put(“personalinfo”,parentInformation);
    JSONObject childObject=null;
    JSONArray childInformation=新的JSONArray();
    for(int i=0;i
    }


  • 使用以下代码将Arraylist转换为字符串及其工作原理:

    Gson Gson=新的Gson();
    字符串customDishListString=gson.toJson(customDishArrayList)

    您的问题是您的java代码。您的问题是缺少上述代码。请将您的java代码发布到服务器中检查Web服务代码。它在postman(api checker)中不起作用@Jaydeppatel:是的,在postman中也不起作用。但在PHP端,它工作得很好。我正在为itheena使用模型类。在传递此请求时,您会遇到什么错误?
        public class ApiClient {
    
        private static Retrofit retrofit = null;
        private static String BASE_URl = Urls.BASE_URL;
    
    
        public static Retrofit getClient() {
            OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
            httpClient.connectTimeout(20 * 1000, TimeUnit.MILLISECONDS);
            httpClient.readTimeout(20 * 1000, TimeUnit.MILLISECONDS);
            HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            httpClient.addInterceptor(loggingInterceptor);
    
            if (retrofit == null) {
                retrofit = new Retrofit.Builder()
                        .client(httpClient.build())
                        .baseUrl(BASE_URl)
                        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
            }
    
            return retrofit;
    
        }
    }
    
        @POST(Urls.registerUserUrl)
        Observable<LoginResponseEntity> getRegistrationResponse(@Body JsonObject request); // make sure JsonObject should be from com.google.gson
    }
    
    ApiInterface apiService =
            ApiClient.getClient().create(ApiInterface.class);
    Observable<LoginResponseEntity> call = apiService.getRegistrationResponse(getRegistrationRequestParameters());
    call.subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new DisposableObserver<LoginResponseEntity>() {
                @Override
                public void onNext(LoginResponseEntity loginResponseEntity) {
                    // handle your response here
                }
    
                @Override
                public void onError(Throwable e) {
    
                }
    
                @Override
                public void onComplete() {
                }
            });
    
    
    
    private JsonObject getRegistrationRequestParameters() {
    JSONObject commonParameters = UDF.commonParametersForJson(activity);
    try {
        JSONObject parentInformation = new JSONObject();
        parentInformation.put("firstname", "" + firstName);
        parentInformation.put("lastname", "" + lastName);
        parentInformation.put("contact", "" + phoneNumber);
        parentInformation.put("password", "" + password);
        commonParameters.put("personalinfo", parentInformation);
        JSONObject childObject = null;
        JSONArray childInformation = new JSONArray();
        for (int i = 0; i < alChildRequestEntity.size(); i++) {
            childObject = new JSONObject();
            childObject.put("childname", alChildRequestEntity.get(i).getName());
            childObject.put("pickupstopcode", alChildRequestEntity.get(i).getPickup_stop());
            childObject.put("dropoffstopcode", alChildRequestEntity.get(i).getDrop_stop());
            childInformation.put(childObject);
        }
        commonParameters.put("childinfo", childInformation);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    JsonParser jsonParser = new JsonParser();
    JsonObject gsonObject = (JsonObject) jsonParser.parse(commonParameters.toString());   // make sure JsonObject should be from com.google.gson
    return gsonObject;