1

我正在使用 Retrofit v2 库编译“com.squareup.retrofit:retrofit:2.0.0-beta2”来发出网络请求。将数据发布到服务器时,我得到空响应。我正在将 JSON 数据发送到服务器。

1.MyAPI.java

public interface MyAPI {
@GET("{roomID}")
Call<List<Message>>loadMessages(@Path("roomID") String roomID);

@POST("almabay_oauth/authorize")
Call<Example>login(@Body String str);

}

2.UserLogin.java

public class UserLogin {
@SerializedName("email")
String email;

@SerializedName("password")
String password;

public UserLogin(String email, String password) {
    this.email = email;
    this.password = password;
}

public String toString() {
    return "UserLogin {email =" + email + ",password = " + password + "}";
}

}

3.MainActivity.java

   Retrofit retrofit1 = new Retrofit.Builder()
            .baseUrl("http://phpstack-11819-25991-62288.cloudwaysapps.com/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    MyAPI myPostApi = retrofit1.create(MyAPI.class);
    UserLogin userLogin = new UserLogin("j@yahoo.com", "Admin123#");
    Gson gson = new Gson();
    String json = gson.toJson(userLogin);
    Log.e("json",json);
    Call<Example> call1 = myPostApi.login(json);
    call1.enqueue(new Callback<Example>() {
        @Override
        public void onResponse(Response<Example> response, Retrofit retrofit) {
            Log.e("Response", String.valueOf(response.body()));
            Example example = response.body();
            Detail detail = example.getDetail();
            Log.e("Access Token", detail.getAccessToken());
        }

        @Override
        public void onFailure(Throwable t) {
            Log.e("Failure", String.valueOf(t));

        }
    });

这是使用改造库将电子邮件 ID 和密码发布到服务器的代码段。在这里你可以看到,我正在使用 GSON 库将包含电子邮件 ID 和密码的对象转换为 JSON,并将 JSON 字符串发布到服务器。

4.预期反应

{
"detail": {
    "id": "249",
    "user_id": "479",
    "access_token": "TwxqCJvgYfNeh6AeEZvjHWpjJMtH9va22ItErYVqeIEvOeKsegLWcbgVYJD7fOai",
    "added_on": "2015-10-17 16:39:55",
    "updated_on": "2015-10-17 16:39:55",
    "status": "1",
    "success": true,
    "userInfo": {
        "userInfo": {
            "timeline": {
                "wall_user_id": "479"
            },
            "logged": true,
            "user": {
                "id": "479",
                "birthday": "2016-01-24",
                "comment_privacy": "everyone",
                "confirm_followers": "1",
                "current_city": "",
                "follow_privacy": "everyone",
                "gender": "male",
                "hometown": null,
                "message_privacy": "everyone",
                "timeline_post_privacy": "a:1:{i:0;s:1:\"5\";}",
                "feed_section": "1",
                "post_privacy": "a:1:{i:0;s:1:\"5\";}",
                "current_city_id": "",
                "userSkills": "",
                "birth": {
                    "date": "24",
                    "month": "01",
                    "year": "2016"
                },
                "about": "jhjjkk\nhjjhjkj\nhjj",
                "active": "1",
                "avatar_id": "397",
                "cover_id": "403",
                "cover_position": "35",
                "email": "nishantkango@yahoo.co.in",
                "email_verification_key": null,
                "email_verified": "0",
                "language": null,
                "last_logged": "1453645993",
                "name": "Nishant Kango",
                "time": "0",
                "timestamp": "2016-01-24 14:33:13",
                "timezone": null,
                "type": "user",
                "username": null,
                "verified": false,
                "user_id": "479",
                "first_name": "Nishant",
                "middle_name": null,
                "last_name": "Kango",
                "dob": "2016-01-24",
                "mobile": "8699022278",
                "title": null,
                "marital_status": "",
                "childrens": "0",
                "hobbies": "Travelling",
                "interests": "Technology",
                "feed_back": null,
                "status": "approved",
                "profile_pic": null,
                "date_created": "2014-10-29 00:00:00",
                "date_modified": "2016-01-24 14:28:24",
                "activation_code": null,
                "social_login_type": "facebook",
                "facebook_id": "10205077190793111",
                "linked_in_id": null,
                "google_id": null,
                "employment_status": null,
                "is_deleted": "0",
                "website": "http://www.nishantkango.com",
                "reference_institute": "",
                "industry": "0",
                "proffesion_type": "",
                "url": "http://phpstack-11819-25991-62288.cloudwaysapps.com/user/timeline/e6b34e9a0311a7f829e09d2d7c4b313e",
                "cover": {
                    "id": "403",
                    "active": "1",
                    "album_id": "0",
                    "extension": "jpg",
                    "name": "IMG_3686.JPG",
                    "post_id": "0",
                    "temp": "0",
                    "timeline_id": "0",
                    "type": "photo",
                    "url": "common/marvel/photos/2015/09/ofRL7_403_bbf94b34eb32268ada57a3be5062fe7d",
                    "complete_url": "http://phpstack-11819-25991-62288.cloudwaysapps.com/common/marvel/photos/2015/09/ofRL7_403_bbf94b34eb32268ada57a3be5062fe7d.jpg",
                    "post_url": "/index.php?tab1=story&id=0"
                },
                "actual_cover_url": "http://phpstack-11819-25991-62288.cloudwaysapps.com/common/marvel/photos/2015/09/ofRL7_403_bbf94b34eb32268ada57a3be5062fe7d.jpg",
                "cover_url": "http://phpstack-11819-25991-62288.cloudwaysapps.com/common/marvel/photos/2015/09/ofRL7_403_bbf94b34eb32268ada57a3be5062fe7d_cover.jpg",
                "avatar": {
                    "id": "397",
                    "active": "1",
                    "album_id": "0",
                    "extension": "jpg",
                    "name": "IMG_3734.JPG",
                    "post_id": "0",
                    "temp": "0",
                    "timeline_id": "0",
                    "type": "photo",
                    "url": "common/marvel/photos/2015/09/2jY3t_397_e46de7e1bcaaced9a54f1e9d0d2f800d",
                    "complete_url": "http://phpstack-11819-25991-62288.cloudwaysapps.com/common/marvel/photos/2015/09/2jY3t_397_e46de7e1bcaaced9a54f1e9d0d2f800d.jpg",
                    "post_url": "/index.php?tab1=story&id=0"
                },
                "thumbnail_url": "http://phpstack-11819-25991-62288.cloudwaysapps.com/common/marvel/images/default-male-avatar.png",
                "avatar_url": "http://phpstack-11819-25991-62288.cloudwaysapps.com/common/marvel/images/default-male-avatar.png",
                "online": false
            }
        },
        "totalRecords": "0",
        "timelineJobInfo": false,
        "privacyArray": [{
            "privacy_option_id": "1",
            "privacy_option_name": "Only Me"
        }, {
            "privacy_option_id": "2",
            "privacy_option_name": "Friends"
        }, {
            "privacy_option_id": "3",
            "privacy_option_name": "Mentors"
        }, {
            "privacy_option_id": "4",
            "privacy_option_name": "Mentees"
        }, {
            "privacy_option_id": "5",
            "privacy_option_name": "Everyone"
        }],
        "educationInfo": [{
            "user_qualification_id": "35",
            "user_id": "479",
            "institute_name": "G.S.S.SCHOOL.BHOTA",
            "qualification_type": "high school",
            "from_month": null,
            "to_month": null,
            "from_year": "0",
            "to_year": "2004",
            "other_qualification_type_description": "",
            "field_of_study": "",
            "date_created": "2015-03-14 16:07:34",
            "date_modified": "2015-03-30 12:07:46",
            "currently_studing_here": null,
            "program_id": null,
            "institute_id": "104",
            "logo": ""
        }, {
            "user_qualification_id": "36",
            "user_id": "479",
            "institute_name": "Himachal Pradesh University",
            "qualification_type": "bachelors degree",
            "from_month": null,
            "to_month": null,
            "from_year": null,
            "to_year": "2011",
            "other_qualification_type_description": null,
            "field_of_study": null,
            "date_created": "2015-03-14 16:07:35",
            "date_modified": "2015-03-14 16:07:35",
            "currently_studing_here": null,
            "program_id": null,
            "institute_id": "105",
            "logo": ""
        }, {
            "user_qualification_id": "69",
            "user_id": "479",
            "institute_name": "Bhavan Vidyalaya",
            "qualification_type": "secondary school",
            "from_month": null,
            "to_month": null,
            "from_year": "2011",
            "to_year": "2015",
            "other_qualification_type_description": "",
            "field_of_study": "",
            "date_created": "2015-06-16 14:36:42",
            "date_modified": "2015-06-16 14:34:40",
            "currently_studing_here": null,
            "program_id": null,
            "institute_id": "51",
            "logo": "1432290512HbMWOd18Do.jpg"
        }, {
            "user_qualification_id": "70",
            "user_id": "479",
            "institute_name": "D.A.V College, Chandigarh",
            "qualification_type": "secondary school",
            "from_month": null,
            "to_month": null,
            "from_year": "0",
            "to_year": "2015",
            "other_qualification_type_description": "",
            "field_of_study": "",
            "date_created": "2015-06-25 18:39:21",
            "date_modified": "2015-07-02 11:50:57",
            "currently_studing_here": null,
            "program_id": null,
            "institute_id": "65",
            "logo": "1427433797f9zgcZJE5Y.jpg"
        }],
        "professionalInfo": [{
            "user_profession_id": "100",
            "user_id": "479",
            "industry": null,
            "organisation": "VebificTechnologies",
            "designation": "CTO",
            "city": "Chandigarh, India",
            "month_from": "1",
            "month_to": "3",
            "year_from": "2015",
            "year_to": "2015",
            "profession_type": null,
            "job_profile": "",
            "date_created": "2015-03-23 10:50:35",
            "date_modified": "2016-01-16 10:38:41",
            "org_type": null,
            "currently_working_here": null,
            "functional_area": null,
            "on_level": null,
            "work_meta": null,
            "migrated": "1"
        }, {
            "user_profession_id": "110",
            "user_id": "479",
            "industry": null,
            "organisation": "Microsoft",
            "designation": "Software Developer",
            "city": "Chandigarh, India",
            "month_from": "1",
            "month_to": "3",
            "year_from": "2015",
            "year_to": "2015",
            "profession_type": null,
            "job_profile": "",
            "date_created": "2015-03-30 16:37:28",
            "date_modified": "2016-01-16 10:39:30",
            "org_type": null,
            "currently_working_here": null,
            "functional_area": null,
            "on_level": null,
            "work_meta": null,
            "migrated": "1"
        }, {
            "user_profession_id": "83",
            "user_id": "479",
            "industry": null,
            "organisation": "Intelegain Technology Pvt. Ltd",
            "designation": "Sr. Programmer Analyst",
            "city": "Chandigarh",
            "month_from": "12",
            "month_to": "11",
            "year_from": "2011",
            "year_to": "2014",
            "profession_type": null,
            "job_profile": "Managing team and projects",
            "date_created": "2014-11-29 16:52:27",
            "date_modified": "2014-11-29 16:52:27",
            "org_type": null,
            "currently_working_here": null,
            "functional_area": null,
            "on_level": null,
            "work_meta": null,
            "migrated": "0"
        }, {
            "user_profession_id": "93",
            "user_id": "479",
            "industry": null,
            "organisation": "Freelance",
            "designation": "Web Developer",
            "city": null,
            "month_from": "1",
            "month_to": "3",
            "year_from": "2011",
            "year_to": "2015",
            "profession_type": null,
            "job_profile": null,
            "date_created": "2015-03-14 16:07:35",
            "date_modified": "2016-01-16 10:38:41",
            "org_type": null,
            "currently_working_here": null,
            "functional_area": null,
            "on_level": null,
            "work_meta": null,
            "migrated": "1"
        }, {
            "user_profession_id": "96",
            "user_id": "479",
            "industry": null,
            "organisation": "Freelance",
            "designation": "Web Developer",
            "city": null,
            "month_from": "1",
            "month_to": "3",
            "year_from": "2011",
            "year_to": "2015",
            "profession_type": null,
            "job_profile": null,
            "date_created": "2015-03-19 18:41:46",
            "date_modified": "2016-01-16 10:38:41",
            "org_type": null,
            "currently_working_here": null,
            "functional_area": null,
            "on_level": null,
            "work_meta": null,
            "migrated": "1"
        }],
        "friendWithYou": false,
        "mutualFriends": null,
        "photos": null
    }
},
"statusCode": "200"

}

我已经为响应写了 POJO。

5.Example.java

public class EducationInfo {

@SerializedName("user_qualification_id")
@Expose
private String userQualificationId;
@SerializedName("user_id")
@Expose
private String userId;
@SerializedName("institute_name")
@Expose
private String instituteName;
@SerializedName("qualification_type")
@Expose
private String qualificationType;
@SerializedName("from_month")
@Expose
private Object fromMonth;
@SerializedName("to_month")
@Expose
private Object toMonth;
@SerializedName("from_year")
@Expose
private String fromYear;
@SerializedName("to_year")
@Expose
private String toYear;
@SerializedName("other_qualification_type_description")
@Expose
private String otherQualificationTypeDescription;
@SerializedName("field_of_study")
@Expose
private String fieldOfStudy;
@SerializedName("date_created")
@Expose
private String dateCreated;
@SerializedName("date_modified")
@Expose
private String dateModified;
@SerializedName("currently_studing_here")
@Expose
private Object currentlyStudingHere;
@SerializedName("program_id")
@Expose
private Object programId;
@SerializedName("institute_id")
@Expose
private String instituteId;
@SerializedName("logo")
@Expose
private String logo;

/**
 *
 * @return
 * The userQualificationId
 */
public String getUserQualificationId() {
    return userQualificationId;
}

/**
 *
 * @param userQualificationId
 * The user_qualification_id
 */
public void setUserQualificationId(String userQualificationId) {
    this.userQualificationId = userQualificationId;
}

/**
 *
 * @return
 * The userId
 */
public String getUserId() {
    return userId;
}

/**
 *
 * @param userId
 * The user_id
 */
public void setUserId(String userId) {
    this.userId = userId;
}

/**
 *
 * @return
 * The instituteName
 */
public String getInstituteName() {
    return instituteName;
}

/**
 *
 * @param instituteName
 * The institute_name
 */
public void setInstituteName(String instituteName) {
    this.instituteName = instituteName;
}

/**
 *
 * @return
 * The qualificationType
 */
public String getQualificationType() {
    return qualificationType;
}

/**
 *
 * @param qualificationType
 * The qualification_type
 */
public void setQualificationType(String qualificationType) {
    this.qualificationType = qualificationType;
}

/**
 *
 * @return
 * The fromMonth
 */
public Object getFromMonth() {
    return fromMonth;
}

/**
 *
 * @param fromMonth
 * The from_month
 */
public void setFromMonth(Object fromMonth) {
    this.fromMonth = fromMonth;
}

/**
 *
 * @return
 * The toMonth
 */
public Object getToMonth() {
    return toMonth;
}

/**
 *
 * @param toMonth
 * The to_month
 */
public void setToMonth(Object toMonth) {
    this.toMonth = toMonth;
}

/**
 *
 * @return
 * The fromYear
 */
public String getFromYear() {
    return fromYear;
}

/**
 *
 * @param fromYear
 * The from_year
 */
public void setFromYear(String fromYear) {
    this.fromYear = fromYear;
}

/**
 *
 * @return
 * The toYear
 */
public String getToYear() {
    return toYear;
}

/**
 *
 * @param toYear
 * The to_year
 */
public void setToYear(String toYear) {
    this.toYear = toYear;
}

/**
 *
 * @return
 * The otherQualificationTypeDescription
 */
public String getOtherQualificationTypeDescription() {
    return otherQualificationTypeDescription;
}

/**
 *
 * @param otherQualificationTypeDescription
 * The other_qualification_type_description
 */
public void setOtherQualificationTypeDescription(String otherQualificationTypeDescription) {
    this.otherQualificationTypeDescription = otherQualificationTypeDescription;
}

/**
 *
 * @return
 * The fieldOfStudy
 */
public String getFieldOfStudy() {
    return fieldOfStudy;
}

/**
 *
 * @param fieldOfStudy
 * The field_of_study
 */
public void setFieldOfStudy(String fieldOfStudy) {
    this.fieldOfStudy = fieldOfStudy;
}

/**
 *
 * @return
 * The dateCreated
 */
public String getDateCreated() {
    return dateCreated;
}

/**
 *
 * @param dateCreated
 * The date_created
 */
public void setDateCreated(String dateCreated) {
    this.dateCreated = dateCreated;
}

/**
 *
 * @return
 * The dateModified
 */
public String getDateModified() {
    return dateModified;
}

/**
 *
 * @param dateModified
 * The date_modified
 */
public void setDateModified(String dateModified) {
    this.dateModified = dateModified;
}

/**
 *
 * @return
 * The currentlyStudingHere
 */
public Object getCurrentlyStudingHere() {
    return currentlyStudingHere;
}

/**
 *
 * @param currentlyStudingHere
 * The currently_studing_here
 */
public void setCurrentlyStudingHere(Object currentlyStudingHere) {
    this.currentlyStudingHere = currentlyStudingHere;
}

/**
 *
 * @return
 * The programId
 */
public Object getProgramId() {
    return programId;
}

/**
 *
 * @param programId
 * The program_id
 */
public void setProgramId(Object programId) {
    this.programId = programId;
}

/**
 *
 * @return
 * The instituteId
 */
public String getInstituteId() {
    return instituteId;
}

/**
 *
 * @param instituteId
 * The institute_id
 */
public void setInstituteId(String instituteId) {
    this.instituteId = instituteId;
}

/**
 *
 * @return
 * The logo
 */
public String getLogo() {
    return logo;
}

/**
 *
 * @param logo
 * The logo
 */
public void setLogo(String logo) {
    this.logo = logo;
}

}

我不知道问题出在哪里。我使用正确的方式来发布 JSON 字符串吗?我在这里得到空响应。请帮我解决问题。

4

0 回答 0