我有蒙戈,快遞和節點 我想通過這裏的用戶名來獲取用戶數據的服務器: myip/users/name/example_name
無法獲取改造2響應主體
凡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 和數字變化.. 我的意思是,我得到的身體 類[email protected] 而@後的數字更改每個按鈕單擊。 我從早上開始工作,並且已經搜索了很多教程和StackOverFlow的錯誤,但是我沒有發現這樣的錯誤。 有人可以幫我嗎?
什麼是狀態碼? –
同時調試,如果你的意思是在身體的代碼,然後它的200 – Noy
你可以發佈你的JSON響應? –