嗨,我是新手使用Retrofit,我被迫将json发布到服务器上。 最初我只有字段参数,其中没有arraylist。但现在我有一个完整的json对象,其中包含string,boolean和arrayList。 所以我通过序列化创建了一个pojo类.pojo类的结构如下所示
public class SeekerProfileModel implements Serializable {
@SerializedName("_id")
private String _id=null;
@SerializedName("full_name")
private String fullName=null;
@SerializedName("phone")
private String phone=null;
@SerializedName("email")
private String email=null;
@SerializedName("native_address")
private String nativeAddress=null;
@SerializedName("fresher")
private Boolean isFresher=null;
@SerializedName("skills")
private String skills=null;
@SerializedName("resume_uri")
private String resumeUri=null;
@SerializedName("educational_details")
private ArrayList<EducationalInfoModel>educationalList=null;
public SeekerProfileModel(String _id,String fullName,String phone,String email,String nativeaddress,Boolean fresher,String skills,String resumeUri,ArrayList<EducationalInfoModel>educationalInfoList)
{
this._id=_id;
this.fullName=fullName;
this.phone=phone;
this.email=email;
this.nativeAddress=nativeaddress;
this.isFresher=fresher;
this.skills=skills;
this.resumeUri=resumeUri;
this.educationalList=educationalInfoList;
}
public ArrayList<EducationalInfoModel> getEducationalList() {
return educationalList;
}
public void setEducationalList(ArrayList<EducationalInfoModel> educationalList) {
this.educationalList = educationalList;
}
public String get_id() {
return _id;
}
public void set_id(String _id) {
this._id = _id;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getNativeAddress() {
return nativeAddress;
}
public void setNativeAddress(String nativeAddress) {
this.nativeAddress = nativeAddress;
}
public Boolean getFresher() {
return isFresher;
}
public void setFresher(Boolean fresher) {
isFresher = fresher;
}
public String getSkills() {
return skills;
}
public void setSkills(String skills) {
this.skills = skills;
}
public String getResumeUri() {
return resumeUri;
}
public void setResumeUri(String resumeUri) {
this.resumeUri = resumeUri;
}
}
这是arraylist的第二个pojo
public class EducationalInfoModel implements Serializable {
@SerializedName("_id")
private String _id=null;
@SerializedName("profile_id")
private String profileId=null;
@SerializedName("grade")
private String grade=null;
@SerializedName("board")
private String board=null;
@SerializedName("percentage")
private String percentage=null;
public EducationalInfoModel(String _id,String profileId,String grade,String board,String percentage)
{
this._id=_id;
this.profileId=profileId;
this.grade=grade;
this.board=board;
this.percentage=percentage;
}
public String get_id() {
return _id;
}
public void set_id(String _id) {
this._id = _id;
}
public String getProfileId() {
return profileId;
}
public void setProfileId(String profileId) {
this.profileId = profileId;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public String getBoard() {
return board;
}
public void setBoard(String board) {
this.board = board;
}
public String getPercentage() {
return percentage;
}
public void setPercentage(String percentage) {
this.percentage = percentage;
}
}
以下是我的RetrofitApiInterface
@POST("/synkku/operations.php")
void createNewSeekerProfile( @Query("module") String module,
@Query("action") String action,
@Body SeekerProfileModel body, Callback<SeekerProfileModel> callback);
最后是Retrofit的辅助类
public void addNewSeekerProfile(SeekerProfileModel seekerProfileModel) {
//Here we will handle the http request to insert user to mysql db
//Creating a RestAdapter
RestAdapter adapter = new RestAdapter.Builder()
.setEndpoint(Allconstants.MAIN_URL) //Setting the Root URL
.build(); //Finally building the adapter
//Creating object for our interface
RetroApiInterface api = adapter.create(RetroApiInterface.class);
//Defining the method insertuser of our interface
api.createNewSeekerProfile(
seekerProfileModel,
new Callback<SeekerProfileModel>() {
@Override
public void success(SeekerProfileModel seekerProfileModel, Response response) {
//On success we will read the server's output using bufferedreader
//Creating a bufferedreader object
BufferedReader reader = null;
//An string to store output from the server
String output = "";
try {
//Initializing buffered reader
reader = new BufferedReader(new InputStreamReader(response.getBody().in()));
//Reading the output in the string
output = reader.readLine();
System.out.println("####data coming as success"+response);
Toast.makeText(mContext,"data"+output,Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
System.out.println("###coming exception");
}
}
@Override
public void failure(RetrofitError error) {
}
}
);
}
现在的问题是数据没有发布 任何帮助将非常感谢
答案 0 :(得分:1)
使用此代替您的InterFace ....
public interface RetroApiInterface {
@POST("synkku/operations.php") //removed backslash from url
void createNewSeekerProfile(
@Body SeekerProfileModel body, Callback<SeekerProfileModel> callback);
}
并在RestAdapter中添加这些.addConverterFactory(GsonConverterFactory.create())
行....如下所示....
RestAdapter adapter = new RestAdapter.Builder()
.addConverterFactory(GsonConverterFactory.create()) //these line is convert your POJO into JSON and Vice Versa
.setEndpoint(Allconstants.MAIN_URL) //Setting the Root URL
.build();
注意: - 始终尝试启动Post或获取url withput反斜杠并在您的初始URL中添加反斜杠(例如www.XXXx.XXXX /)。
编辑: - 在您的成绩中添加这些依赖项....
添加
compile 'com.squareup.retrofit2:converter-gson:2.0.0'
compile 'com.google.code.gson:gson:2.7' //this is same as Ravi told
以上改造2.0及以上的例子自由化......
对于改造1.9你的所有网址都很好,RestAdapter也很好
检查onFaliure()
方法并在此处发布,如果有任何异常.........
答案 1 :(得分:0)
终于解决了这个问题..使用上述代码使用改装1.9的帖子请求绝对正常工作,因为我已经错过了另一个@query字符串以及改装界面中的正文,问题出在php编码json中我使用以下代码行解决 $ data = json_decode(file_get_contents('php:// input'),true); $全称= $数据[ 'FULL_NAME'];
感谢所有花时间帮助我解决这个问题的人