簡體   English   中英

在 Retrofit 和 @Body 注釋中使用 PATCH 方法

[英]Using PATCH Method In Retrofit and @Body Annotation

我的網址是

http://sales.xxxxx.com/api/v2/customers/5101117835

當我使用此代碼時,響應代碼始終為 500。

接口代碼

@Headers({ "Content-Type: application/json;charset=UTF-8"})
@PATCH("customers/{custId}")
Call<GeoTag> postGeoTagData (@Body geoTagUserData geoTag, @Path("custId") String id, @Header("Authorization") String auth);

活動中

        Retrofit retrofit  = new Retrofit.Builder()
            .baseUrl(" http://sales.erprnd.com/api/v2/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    ApiInterface apiInterface = retrofit.create(ApiInterface.class);

   Call<GeoTag> geoTagCall =  apiInterface.postGeoTagData(geoTagUserData, CustCode, "Bearer "+body.getToken() );
   geoTagCall.enqueue(new Callback<GeoTag>() {
       @Override
       public void onResponse(Call<GeoTag> call, Response<GeoTag> response) {

           Toast.makeText(GeoTaggingActivity.this, ""+response.code(), Toast.LENGTH_SHORT).show();
       }

       @Override
       public void onFailure(Call<GeoTag> call, Throwable t) {
           Toast.makeText(GeoTaggingActivity.this, "Failed", Toast.LENGTH_SHORT).show();
       }
   });

當我使用 Postman 時,響應是 201 AND Response IS {},這沒問題。 我的 jsonObject 是

{
 "latitude":11.22,
 "longitude":22.11,
 "alt_address":"test address",
 "height":"12 ft",
 "width":"24 ft",
 "photo": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMAAAAD"
}

我創建了另一個類,我從用戶那里獲取所有信息,即geoTagUserData我完美地獲取了所有信息

請幫我找出錯誤。

最后我得到了解決方案,即當我從我的應用程序發送數據以將數據發送到服務器時,它只是獲取針對每個字段表單geoTagUserData類的值,它不會@post JSON格式的轉換數據。 當我的服務器發現我的值不是JSON格式時,它只是拒絕並返回 500 錯誤

所以,我的解決方案只是使用Gson 轉換器獲取用戶表單的值並將其轉換為JSON對象。 並將其發送到將返回 201 的服務器

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM