我要向其中发布多个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"}]}
邮递员屏幕
答案 0 :(得分:0)
您应该已经在API接口中使用了Gson库中的JsonObject
类。 (使用Retrofit时,您无需单独包括Gson库)
Call<JsonObject> SUBMIT_OFFLINE_RECORD(@Body JsonObject offlineRequest);
如果上述解决方案无效,请创建POJO类,并将其作为参数传递给@Body
批注。
User.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
}
Data.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
请注意,我显然将JSONObject
参数的参数也从JsonObject
更改为Call<>
。