为什么使用改造将数据发送到服务器时,我的JSON字符串会自动编码?

时间:2019-04-27 03:21:42

标签: java android json retrofit retrofit2

我实际上是一名iOS开发人员,并且对android开发知之甚少。而且我需要在iOS应用中复制从Android应用发送到服务器的请求。

在后端API中,要求类似于邮递员中的下图,我需要在“产品”键的值中发送类似编码字符串的内容:

enter image description here

这是改造生成器:

func(row):
   if row['MEASUREMENT'] == np.NaN:
       print row['VALUE']

new_df = df.apply(func, axis=1)

这是拨打电话的改造界面:

public class APIClient {


    // Base URL for API Requests
    private static final String BASE_URL = ConstantValues.ECOMMERCE_URL + "app/";

    private static APIRequests apiRequests;


    // Singleton Instance of APIRequests
    public static APIRequests getInstance() {
        if (apiRequests == null) {

            HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
            httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

            API_Interceptor apiInterceptor = new API_Interceptor.Builder()
                    .consumerKey(Utilities.getMd5Hash(ConstantValues.ECOMMERCE_CONSUMER_KEY))
                    .consumerSecret(Utilities.getMd5Hash(ConstantValues.ECOMMERCE_CONSUMER_SECRET))
                    .build();

            OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
                    .connectTimeout(60, TimeUnit.SECONDS)
                    .readTimeout(60, TimeUnit.SECONDS)
                    .writeTimeout(60, TimeUnit.SECONDS)
                    .addInterceptor(apiInterceptor)
                    .addInterceptor(httpLoggingInterceptor)
                    .build();


            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .client(okHttpClient)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();


            apiRequests = retrofit.create(APIRequests.class);

            return apiRequests;
        }
        else {
            return apiRequests;
        }
    }

}

以下是向服务器发出请求的代码:

    @FormUrlEncoded
    @POST("checkproductavailability")
    Call<AvailableProductsResponse> checkProductsAvailability(
            @Field("products") String products,
            @Field("status") int productStatus
    );

我认为,发出请求时'products'键的字符串来自 Call<AvailableProductsResponse> call = APIClient.getInstance().checkProductsAvailability( new Gson().toJson(cartItemsList) , ConstantValues.STATUS_INAVAILABLE_PRODUCTS ); call.enqueue(new Callback<AvailableProductsResponse>() { @Override public void onResponse(Call<AvailableProductsResponse> call, retrofit2.Response<AvailableProductsResponse> response) { AlertDialog.Builder ab = new AlertDialog.Builder(getActivity()); ab.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); if (response.isSuccessful()) { if (200 == response.body().getSuccess()) { AvailableProductsResponse.AvailableProductsResponseData aprd = response.body().getData(); List<AvailableProductInfo> inAvailableProductsList = aprd.getProductsList(); if (0 < inAvailableProductsList.size()) { showInavailableProductsDialog(inAvailableProductsList); } else { // Navigate to Shipping_Address Fragment Fragment fragment = new Checkout(); FragmentManager fragmentManager = getFragmentManager(); fragmentManager.beginTransaction().replace(R.id.main_fragment, fragment) .addToBackStack(getString(R.string.actionCart)) .commit(); } } } else { Log.e(TAG + ".onResponse: ", "message: " + response.message()); ab.setMessage(R.string.text_failed_connecting_server); ab.create().show(); } mDialogLoader.hideProgressDialog(); } @Override public void onFailure(Call<AvailableProductsResponse> call, Throwable t) { mDialogLoader.hideProgressDialog(); Toast.makeText(getContext(), "NetworkCallFailure : "+t, Toast.LENGTH_LONG).show(); } });

new Gson().toJson(cartItemsList)

,这是您看到的日志,如蓝色箭头所示。将数据发送到服务器时会自动编码 enter image description here

但是如果我这样记录Call<AvailableProductsResponse> call = APIClient.getInstance().checkProductsAvailability( new Gson().toJson(cartItemsList) , ConstantValues.STATUS_INAVAILABLE_PRODUCTS );

Gson().toJson(cartItemsList)

结果是这样的普通JSON字符串:

String json = new Gson().toJson(cartItemsList);
        Log.d("checkxxx",json)

那么为什么上面的JSON会自动变成这样的编码字符串

[
  {
    "customers_basket_date_added": "2019-04-27 09:40:35",
    "customers_basket_id": 3,
    "customers_basket_product": {
      "attributes": [],
      "attributes_price": "0.0",
      "categories_id": 6,
      "categories_name": "Perawatan Rambut",
      "customers_basket_quantity": 2,
      "images": [],
      "isSale_product": "0",
      "language_id": 0,
      "motorist_id": 0,
      "motorist_name": "",
      "id_jenis_products": 0,
      "qty_orders_max": 40,
      "qty_orders_min": 2,
      "qty_promo_bought": 0,
      "trans_limit": 0,
      "trans_live": 0,
      "manufacturers_id": 0,
      "parent_id": 0,
      "products_description": "Dijual per Renteng<br/> <br/>\r\nMinimal pembelian 2 Renteng <br/>\r\n1 Dus isi 20 Renteng<br/><br/>",
      "final_price": "4500.0",
      "products_id": 28,
      "products_image": "resources/assets/images/product_images/1548787433.emeron-hijab-clean-n-fresh.jpg",
      "products_liked": 0,
      "products_model": "",
      "products_name": "Emeron Hijab Clean & Fresh Sachet 12 ml",
      "products_ordered": 0,
      "products_price": "4500.0",
      "products_quantity": 2094,
      "products_status": 0,
      "products_tax_class_id": 0,
      "products_viewed": 0,
      "products_weight": "1",
      "products_weight_unit": "Rtg",
      "sort_order": 0,
      "tax_class_id": 0,
      "tax_priority": 0,
      "tax_rates_id": 0,
      "tax_zone_id": 0,
      "total_price": "9000.0"
    },
    "customers_basket_product_attributes": [],
    "customers_id": 0
  }
]

从使用改造发送数据时开始的编码过程位于何处?

0 个答案:

没有答案