我目前使用的是RetroFit,並且是新的工具。當我運行我的應用程序時,我遇到了標題中所述的問題。它不會崩潰,但它不會返回我需要的信息。我附上我的代碼。非常感謝您的幫助。
public class MainActivity extends AppCompatActivity {
@Bind(R.id.textView_userid) TextView textView_userid;
@Bind(R.id.textView_email) TextView textView_email;
@Bind(R.id.textView_password) TextView textView_password;
@Bind(R.id.textView_firstName) TextView textView_firstname;
@Bind(R.id.textView_lastname) TextView textView_lastname;
@Bind(R.id.refresh_button) Button refresh_button;
@Override
protected void onCreate(Bundle savedInstanceState) {
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitAll().build());
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
@OnClick(R.id.refresh_button)
public void onClick_Refresh(){
UserApi.Factory.getInstance().getUsers().enqueue(new Callback<User>() {
@Override
public void onResponse(Response<User> response, Retrofit retrofit) {
textView_userid.setText(response.body().getUserId());
textView_email.setText(response.body().getEmail());
textView_firstname.setText(response.body().getFirstName());
textView_lastname.setText(response.body().getLastName());
Log.e("Info", response.body().toString());
Log.v("Get", response.body().toString());
}
@Override
public void onFailure(Throwable t) {
Log.e("failed", t.getMessage());
}
});
}
@Override
protected void onResume(){
super.onResume();
onClick_Refresh();
}
}
public interface UserApi {
String baseUrl = "http://someUrl/";
@GET("users")
Call<User> getUsers();
class Factory{
private static UserApi service;
public static UserApi getInstance(){
if(service == null){
Retrofit retrofit = new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create()).baseUrl(baseUrl).build();
service = retrofit.create(UserApi.class);
return service;
}else{
return service;
}
}
}
}
@Generated( 「org.jsonschema2pojo」) 公共類用戶{
@SerializedName("userId")
@Expose
private int userId;
@SerializedName("email")
@Expose
private String email;
@SerializedName("firstName")
@Expose
private String firstName;
@SerializedName("lastName")
@Expose
private String lastName;
/**
*
* @return
* The userId
*/
public int getUserId() {
return userId;
}
/**
*
* @param userId
* The userId
*/
public void setUserId(int userId) {
this.userId = userId;
}
/**
*
* @return
* The email
*/
public String getEmail() {
return email;
}
/**
*
* @param email
* The email
*/
public void setEmail(String email) {
this.email = email;
}
/**
*
* @return
* The firstName
*/
public String getFirstName() {
return firstName;
}
/**
*
* @param firstName
* The firstName
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
*
* @return
* The lastName
*/
public String getLastName() {
return lastName;
}
/**
*
* @param lastName
* The lastName
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
[{"userId":1,"email":"[email protected]","password":"abc123","firstName":"bob","lastName":"joe"}]
你的問題還不夠清楚,你能告訴我們什麼方法正好引發異常。無論如何,從標題中的錯誤文本我得出結論,該方法是期待JSON對象,而不是JSON數組。 – stjepano
另外,您可以查看http://stackoverflow.com/a/23670188/2450855 –