簡體   English   中英

我正在發送 JSONObject 代碼 = 500,消息 = 內部服務器錯誤

[英]I'm sending JSONObject code=500, message=Internal Server Error

我將多個 JSON 數組發布到,但它給出了一個內部服務器錯誤,但它在郵遞員上工作正常

界面

 @Headers({
            "Content-Type: application/json",
            "Authorization: Basic c3ltby1hcHAtaW9zOmE4ODI4NjY1LTU1MzgtNGNlYy1hYzU4LWE0YmU0NmE1Y2Y3OA==",
            "client-id: bitmoon-app-android"
    })
    @POST("medical-ic10/public/api/user/pending")
        Call<JSONObject>  SUBMIT_OFFLINE_RECORD(@Body JSONObject offlineRequest);

執行

 public void getData(JSONObject J) {

        Api api= RetrofitClientInstance.getRetrofitInstance().create(Api.class);
        Call<JSONObject> call=api.SUBMIT_OFFLINE_RECORD(J);
        call.enqueue(new Callback<JSONObject>() {
            @Override
            public void onResponse(Call<JSONObject> call, Response<JSONObject> response) {

            }

            @Override
            public void onFailure(Call<JSONObject> call, Throwable t) {
                Toast.makeText(Offlinerecord_Activity.this, ""+t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
        Log.e("GSON",t.getLocalizedMessage());
            }
        });
    }

JSON 對象

{"data":[{"id":"15","pName":"15","pAge":"15","pGender":"15","pPhone":"15","pNIC":"15","pDiagnosis":"15","dName":"15","dSpeciality":"15","dPhone":"15","dInstitution":"15","dAssistantName":"15","dCity":"15"}]}

郵遞員屏幕

在此處輸入圖片說明

您應該在 API 接口中使用了 Gson 庫中的JsonObject類。 (當您使用 Retrofit 時,您不需要單獨包含 Gson 庫)如下所示

Call<JsonObject>  SUBMIT_OFFLINE_RECORD(@Body JsonObject offlineRequest);

如果上述解決方案不起作用,請創建 POJO 類並將其作為參數傳遞給@Body注釋。

用戶.java

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class User {

    @SerializedName("id")
    @Expose
    private String id;
    @SerializedName("pName")
    @Expose
    private String pName;
    @SerializedName("pAge")
    @Expose
    private String pAge;
    @SerializedName("pGender")
    @Expose
    private String pGender;
    @SerializedName("pPhone")
    @Expose
    private String pPhone;
    @SerializedName("pNIC")
    @Expose
    private String pNIC;
    @SerializedName("pDiagnosis")
    @Expose
    private String pDiagnosis;
    @SerializedName("dName")
    @Expose
    private String dName;
    @SerializedName("dSpeciality")
    @Expose
    private String dSpeciality;
    @SerializedName("dPhone")
    @Expose
    private String dPhone;
    @SerializedName("dInstitution")
    @Expose
    private String dInstitution;
    @SerializedName("dAssistantName")
    @Expose
    private String dAssistantName;
    @SerializedName("dCity")
    @Expose
    private String dCity;

    public User(String id, String pName, String pAge, String pGender, String pPhone, String pNIC, String pDiagnosis, String dName, String dSpeciality, String dPhone, String dInstitution, String dAssistantName, String dCity) {
        this.id = id;
        this.pName = pName;
        this.pAge = pAge;
        this.pGender = pGender;
        this.pPhone = pPhone;
        this.pNIC = pNIC;
        this.pDiagnosis = pDiagnosis;
        this.dName = dName;
        this.dSpeciality = dSpeciality;
        this.dPhone = dPhone;
        this.dInstitution = dInstitution;
        this.dAssistantName = dAssistantName;
        this.dCity = dCity;
    }

    // other constructors, getter and setter methods

}

數據.java

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Example {

    @SerializedName("data")
    @Expose
    private List<Datum> data = null;

    public Example(List<Datum> data) {
        this.data = data;
    }
}

並將@Body注釋參數從JSONObject更改為Data如下所示

Call<JsonObject>  SUBMIT_OFFLINE_RECORD(@Body Data offlineRequest);

POJO 類的使用

User user = new User(/* pass all required parameters to constructor */);
Data data = new Data(user);  // pass User object to constructor 

// pass Data object to API request method
Api api= RetrofitClientInstance.getRetrofitInstance().create(Api.class);
Call<JsonObject> call = api.SUBMIT_OFFLINE_RECORD(data);  
// implement call listeners

請注意,我在Call<>參數中也將參數從JSONObject更改為JsonObject

暫無
暫無

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

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