2016-03-29 15 views
0

こんにちは私は改造を使用しようとしているすべての問題はありません。しかし、私はこのような出力を得ています。 [[email protected][email protected]]奇妙な出力を取得する

ここでは、これは私のモデルである私のMainActivity

public class MainActivity extends AppCompatActivity { 

@Bind(R.id.activity_main_tv_display) 
TextView textData; 

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

@OnClick(R.id.activity_main_btn_show) 

void press() { 
    RemoteApi.Factory.getInstance().getModel().enqueue(new Callback<List<Model>>() { 
     @Override 
     public void onResponse(Call<List<Model>> call, Response<List<Model>> response) { 
      textData.setText(String.valueOf(response.body())); 
      Log.e("--success--", String.valueOf(response.body())); 
     } 

     @Override 
     public void onFailure(Call<List<Model>> call, Throwable t) { 
      Log.e("--fail--", t.getMessage()); 
     } 
    }); 
} 
} 

ある

public class Model { 

@SerializedName("Title") 
@Expose 
private String Title; 
@SerializedName("Message") 
@Expose 
private String Message; 
@SerializedName("id") 
@Expose 
private int id; 
// getters and setters declare 
} 

ここでは私のインターフェイスは

public interface RemoteApi { 

String BASE_URL = "xyz/"; 
@GET("api/Cards") 
Call<List<Model>> getModel(); 
class Factory { 
    public static RemoteApi remoteApi; 
    public static RemoteApi getInstance() { 
      Retrofit retrofit = new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create()) 
        .baseUrl(BASE_URL) 
        .build(); 
      remoteApi = retrofit.create(RemoteApi.class); 
      return remoteApi; 
     } 
    } 
} 

され、私のAPIは、このようになります

[{ 
    "Title": "xyz", 
    "Message": "hello", 
    "id": 1 
}, { 
    "Title": "abc", 
    "Message": "hello", 
    "id": 2 
}] 

答えて

1

私が正しいんだ場合、あなたは応答して、ここで読んでいる

public void onResponse(Call<List<Model>> call, Response<List<Model>> response) { 
     textData.setText(String.valueOf(response.body())); 
     Log.e("--success--", String.valueOf(response.body())); 
    } 

ボディはモデルオブジェクトのリストです。 タイトルとメッセージの値を取得する場合は、おそらくこのリストを繰り返す必要があります。

編集 あなたはこのように繰り返すことができます。コンソールに

public void onResponse(Call<List<Model>> call, Response<List<Model>> response) { 
    for (Model eachModel : response.body()) 
    { 
     Log.e("--success--", eachModel.title); 
    } 
} 

この意志出力の各要素のタイトルを。すべてのタイトルをテキストビューに表示するには、リストを作成するか、テキストビューコードをリファクタリングする必要があります。

+0

完了。なぜ-1? – TooManyEduardos

+0

申し訳ありません。これはresponse.body()です。 – TooManyEduardos

関連する問題