无法获取Retrofit 2响应体

时间:2016-07-24 15:06:35

标签: java android client-server retrofit

我有一个带有mongo,express和node的服务器 我试图通过用户名获取用户数据: myip/users/name/example_name

其中example_name是参数。

这是我的代码:

主要活动:

@BindView(R.id.btnTest)
Button btnTest;
@BindView(R.id.tvTest)
TextView tvTest;

@OnClick(R.id.btnTest) void changeText(){
    GitHubService gitHubService = GitHubService.retrofit.create(GitHubService.class);
    final Call<User> call =
            gitHubService.getUser("dolev");

    call.enqueue(new Callback<User>() {
        @Override
        public void onResponse(Call<User> call, Response<User> response) {
            Toast.makeText(MainActivity.this,"This: " +  response.body().getName(), Toast.LENGTH_SHORT).show();
            tvTest.setText("response success," + response.body().toString());
        }
        @Override
        public void onFailure(Call<User> call, Throwable t) {
            tvTest.setText("Something went wrong: " + t.getMessage());
        }
    });
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);

}

@Override
public void onResponse(Call<User> call, Response<User> response) {
    int code = response.code();
    if (code == 200) {
        User user = response.body();
        Log.v("FETCH", response.body().getEmail());
        Toast.makeText(this, "Got the user: " + user.getName(), Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(this, "Did not work: " + String.valueOf(code), Toast.LENGTH_LONG).show();
    }
}

@Override
public void onFailure(Call<User> call, Throwable t) {
    Toast.makeText(this, "Nope", Toast.LENGTH_LONG).show();

}

GitHubService:

public interface GitHubService {



@GET("users/name/{username}")
Call<User> getUser(@Path("username") String username);

@GET("users}")
Call<List<User>> getAllUsers();





public static final Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(Global.API)
        .addConverterFactory(GsonConverterFactory.create())
        .build();

}

用户类:

package com.noy.photobomb.model;

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

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Generated;

@Generated("org.jsonschema2pojo")
public class User {


    @SerializedName("_id")
    @Expose
    private String id;
    @SerializedName("isBanned")
    @Expose
    private Boolean isBanned;
    @SerializedName("users_followers")
    @Expose
    private List<Object> usersFollowers = new ArrayList<Object>();
    @SerializedName("__v")
    @Expose
    private Integer v;
    @SerializedName("followers")
    @Expose
    private List<Object> followers = new ArrayList<Object>();
    @SerializedName("groups")
    @Expose
    private List<Object> groups = new ArrayList<Object>();
    @SerializedName("phoneNumber")
    @Expose
    private String phoneNumber;
    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("email")
    @Expose
    private String email;


    private String login;

    public String getLogin() {
        return login;
    }
    /**
     *
     * @return
     * The id
     */
    public String getId() {
        return id;
    }

    /**
     *
     * @param id
     * The _id
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     *
     * @return
     * The isBanned
     */
    public Boolean getIsBanned() {
        return isBanned;
    }

    /**
     *
     * @param isBanned
     * The isBanned
     */
    public void setIsBanned(Boolean isBanned) {
        this.isBanned = isBanned;
    }

    /**
     *
     * @return
     * The usersFollowers
     */
    public List<Object> getUsersFollowers() {
        return usersFollowers;
    }

    /**
     *
     * @param usersFollowers
     * The users_followers
     */
    public void setUsersFollowers(List<Object> usersFollowers) {
        this.usersFollowers = usersFollowers;
    }

    /**
     *
     * @return
     * The v
     */
    public Integer getV() {
        return v;
    }

    /**
     *
     * @param v
     * The __v
     */
    public void setV(Integer v) {
        this.v = v;
    }

    /**
     *
     * @return
     * The followers
     */
    public List<Object> getFollowers() {
        return followers;
    }

    /**
     *
     * @param followers
     * The followers
     */
    public void setFollowers(List<Object> followers) {
        this.followers = followers;
    }

    /**
     *
     * @return
     * The groups
     */
    public List<Object> getGroups() {
        return groups;
    }

    /**
     *
     * @param groups
     * The groups
     */
    public void setGroups(List<Object> groups) {
        this.groups = groups;
    }

    /**
     *
     * @return
     * The phoneNumber
     */
    public String getPhoneNumber() {
        return phoneNumber;
    }

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

    /**
     *
     * @return
     * The name
     */
    public String getName() {
        return name;
    }

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

    /**
     *
     * @return
     * The email
     */
    public String getEmail() {
        return email;
    }

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

}

当我试图获取JSON数据时 - 响应正文为空 - 例如名称,ID,电子邮件等。 response.body()。toString()给我一个结果: @ 59285 数量变化.. 我的意思是,我得到身体 com.noy.photobomb.model.User@84593 虽然@后面的数字改变了每个按钮的点击次数。 我从早上开始研究它,我搜索了StackOverFlow的许多教程和错误,但我还没有发现这样的错误。 有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

@Sagar Jogadia 通过聊天帮我解决了问题。 问题是数据是在JSON数组中,我试图获取一个对象,因此用户I而不是User用户列表。我通过正文得到了数据,然后是一个带有一个索引的数组 - response.body.get(0).getName() - 例如。 非常感谢你