Skip to content

Instantly share code, notes, and snippets.

@Khanashima
Last active February 3, 2016 16:05
Show Gist options
  • Save Khanashima/155646a07d2da3666f61 to your computer and use it in GitHub Desktop.
Save Khanashima/155646a07d2da3666f61 to your computer and use it in GitHub Desktop.
Retrofit ver2.0.0の使い方 ref: http://qiita.com/kiimiiis/items/9e1078c66646c02cc103
compile 'com.squareup.retrofit2:retrofit:2.0.0-beta3'
compile 'com.squareup.retrofit2:converter-gson:latest.release'
compile 'com.google.code.gson:gson:2.3.1'
compile 'org.glassfish:javax.annotation:10.0-b28'
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//QiitaApiの使用
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://qiita.com")
.addConverterFactory(GsonConverterFactory.create())
.build();
QiitaApiService qiitaApiservice = retrofit.create(QiitaApiService.class);
Call<List<QiitaGsonResponse>> call = qiitaApiservice.getData("1", "20", "android");
try {
call.enqueue(new Callback<List<QiitaGsonResponse>>() {
@Override
public void onResponse(Response<List<QiitaGsonResponse>> response) {
String title = response.body().get(0).getTitle();
Log.d("Qiita",title);
}
@Override
public void onFailure(Throwable t) {
Log.d("Qiita", "error");
}
});
} catch (Exception e) {
Log.d("Qiita", "レスポンスエラー");
}
}
/**
* Created by hanasjima on 2016/01/28.
*/
public interface QiitaApiService {
@GET("/api/v2/items")
Call<List<QiitaGsonResponse>> getData(@Query("page") String page, @Query ("per_page") String per_page, @Query("query") String query);
}
@Generated("org.jsonschema2pojo")
public class QiitaGsonResponse {
@SerializedName("rendered_body")
@Expose
private String renderedBody;
@SerializedName("body")
@Expose
private String body;
@SerializedName("coediting")
@Expose
private Boolean coediting;
@SerializedName("created_at")
@Expose
private String createdAt;
@SerializedName("id")
@Expose
private String id;
@SerializedName("private")
@Expose
private Boolean _private;
@SerializedName("tags")
@Expose
private List<Tag> tags = new ArrayList<Tag>();
@SerializedName("title")
@Expose
private String title;
@SerializedName("updated_at")
@Expose
private String updatedAt;
@SerializedName("url")
@Expose
private String url;
@SerializedName("user")
@Expose
private User user;
/**
*
* @return
* The renderedBody
*/
public String getRenderedBody() {
return renderedBody;
}
/**
*
* @param renderedBody
* The rendered_body
*/
public void setRenderedBody(String renderedBody) {
this.renderedBody = renderedBody;
}
/**
*
* @return
* The body
*/
public String getBody() {
return body;
}
/**
*
* @param body
* The body
*/
public void setBody(String body) {
this.body = body;
}
/**
*
* @return
* The coediting
*/
public Boolean getCoediting() {
return coediting;
}
/**
*
* @param coediting
* The coediting
*/
public void setCoediting(Boolean coediting) {
this.coediting = coediting;
}
/**
*
* @return
* The createdAt
*/
public String getCreatedAt() {
return createdAt;
}
/**
*
* @param createdAt
* The created_at
*/
public void setCreatedAt(String createdAt) {
this.createdAt = createdAt;
}
/**
*
* @return
* The id
*/
public String getId() {
return id;
}
/**
*
* @param id
* The id
*/
public void setId(String id) {
this.id = id;
}
/**
*
* @return
* The _private
*/
public Boolean getPrivate() {
return _private;
}
/**
*
* @param _private
* The private
*/
public void setPrivate(Boolean _private) {
this._private = _private;
}
/**
*
* @return
* The tags
*/
public List<Tag> getTags() {
return tags;
}
/**
*
* @param tags
* The tags
*/
public void setTags(List<Tag> tags) {
this.tags = tags;
}
/**
*
* @return
* The title
*/
public String getTitle() {
return title;
}
/**
*
* @param title
* The title
*/
public void setTitle(String title) {
this.title = title;
}
/**
*
* @return
* The updatedAt
*/
public String getUpdatedAt() {
return updatedAt;
}
/**
*
* @param updatedAt
* The updated_at
*/
public void setUpdatedAt(String updatedAt) {
this.updatedAt = updatedAt;
}
/**
*
* @return
* The url
*/
public String getUrl() {
return url;
}
/**
*
* @param url
* The url
*/
public void setUrl(String url) {
this.url = url;
}
/**
*
* @return
* The user
*/
public User getUser() {
return user;
}
/**
*
* @param user
* The user
*/
public void setUser(User user) {
this.user = user;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment