简体   繁体   English

无法获取改造2响应正文

[英]Cannot fetch Retrofit 2 response body

I have a server with mongo, express and node I am trying to get user data by user name here: myip/users/name/example_name 我有一台具有mongo,express和node的服务器,我试图在此处通过用户名获取用户数据: myip/users/name/example_name

Where the example_name is a parameter. 其中example_name是参数。

Here's my code: 这是我的代码:

Main Activity: 主要活动:

@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: 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();

} }

User Class: 用户类别:

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;
    }

}

When I'm trying to get the JSON data- the response body is null - eg the name, id, email etc.. The response.body().toString() gives me a result of : @59285 And the number changes.. I mean, I get as the body class com.noy.photobomb.model.User@84593 While the number after the @ changes every button click. 当我尝试获取JSON数据时-响应主体为空-例如名称,ID,电子邮件等。response.body()。toString()给我的结果是:@ 59285并且数字更改。我的意思是,我得到的是主体类com.noy.photobomb.model.User@84593 @之后的数字会更改每个按钮的单击。 I am working on it since the morning and I have searched through many tutorials and errors of StackOverFlow but I haven't found such error. 从早上开始,我就在研究它,我搜索了许多教程和StackOverFlow的错误,但没有发现此类错误。 Can someone please help me? 有人可以帮帮我吗?

@Sagar Jogadia Helped me through the chat to figure it out. @Sagar Jogadia帮助我完成了聊天过程,以解决问题。 The problem was that the data was in JSON Array and I tried to fetch an object, so instead of User I user a list of users. 问题在于数据位于JSON数组中,我试图获取一个对象,因此我使用用户列表代替了User。 I got the data through the body which then was an array with one index - response.body.get(0).getName() - for example. 我通过主体获取数据,该主体然后是具有一个索引的数组-例如,response.body.get(0).getName()。 Thank you very much 非常感谢你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM