2016-02-19 127 views
0

我目前使用的是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"}] 
+0

你的問題還不夠清楚,你能告訴我們什麼方法正好引發異常。無論如何,從標題中的錯誤文本我得出結論,該方法是期待JSON對象,而不是JSON數組。 – stjepano

+0

另外,您可以查看http://stackoverflow.com/a/23670188/2450855 –

回答

0

的JSON剪斷您提供節目您收到的陣列,而不是的對象請嘗試更改以下代碼:

UserApi.Factory.getInstance().getUsers().enqueue(new Callback<User>() { 
      @Override 
      public void onResponse(Response<User> response, Retrofit retrofit) { 

      } 
      @Override 
      public void onFailure(Throwable t) { 
       Log.e("failed", t.getMessage()); 

      } 
     }); 

爲了這樣的事情:

UserApi.Factory.getInstance().getUsers().enqueue(new Callback<List<User>>() { 
      @Override 
      public void onResponse(Response<List<User>> response, Retrofit retrofit) { 
        List<User> users = response.body; 
        // Do something with the rest 
       } 
      @Override 
      public void onFailure(Throwable t) { 
        Log.e("failed", t.getMessage()); 

      } 
     }); 
0

首先,改變身體的onResponse

@Override 
     public void onResponse(Response<List<User>> response, Retrofit retrofit) { 
       List<User> users = response.body; 

      } 

其次,對於同時測定哪裏想到的JSONObject或jsonArray,你可以使用這個網站,you can get equivalent POJO for any Json response.

0

謝謝你們的幫助。我對界面和主要活動進行了一些更改。下面的結果工作。

UserApi.Factory.getInstance().getUsers().enqueue(new Callback<List<User>>() { 
     @Override 
     public void onResponse(Response<List<User>> response, Retrofit retrofit) { 
      for(User user: response.body()){ 
      Log.v("Endpoint Response: ", user.toString()); 
       textView_userid.setText(String.valueOf(user.getUserId())); 
       textView_email.setText(user.getEmail()); 
       textView_firstname.setText(user.getFirstName()); 
       textView_lastname.setText(user.getLastName()); 
      } 

     } 

     @Override 
     public void onFailure(Throwable t) { 
      Log.e("failed", t.getMessage()); 

     } 
    }); 


@GET("users") 
Call<List<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; 
     } 
    } 
} 
相關問題