1.
we wnat to get json from
https://api.stackexchange.com/2.2/search?order=desc&sort=activity&tagged=android&site=stackoverflow
2.
compile 'com.squareup.retrofit2:retrofit:2.0.0-beta3'
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta3'
3.
Add the permission to access the Internet to your manifest file.
<uses-permission android:name="android.permission.INTERNET"/>
4.create a approaite class
can use http://www.jsonschema2pojo.org/ to help
public class Item {
private List<String> tags = new ArrayList<String>();
private Owner owner;
private Boolean isAnswered;
private Integer viewCount;
private Integer answerCount;
private Integer score;
private Integer lastActivityDate;
private Integer creationDate;
private Integer questionId;
private String link;
private String title;
private Integer acceptedAnswerId;
private Integer lastEditDate;
/**
*
* @return
* The tags
*/
public List<String> getTags() {
return tags;
}
/**
*
* @param tags
* The tags
*/
public void setTags(List<String> tags) {
this.tags = tags;
}
/**
*
* @return
* The owner
*/
public Owner getOwner() {
return owner;
}
/**
*
* @param owner
* The owner
*/
public void setOwner(Owner owner) {
this.owner = owner;
}
/**
*
* @return
* The isAnswered
*/
public Boolean getIsAnswered() {
return isAnswered;
}
/**
*
* @param isAnswered
* The is_answered
*/
public void setIsAnswered(Boolean isAnswered) {
this.isAnswered = isAnswered;
}
/**
*
* @return
* The viewCount
*/
public Integer getViewCount() {
return viewCount;
}
/**
*
* @param viewCount
* The view_count
*/
public void setViewCount(Integer viewCount) {
this.viewCount = viewCount;
}
/**
*
* @return
* The answerCount
*/
public Integer getAnswerCount() {
return answerCount;
}
/**
*
* @param answerCount
* The answer_count
*/
public void setAnswerCount(Integer answerCount) {
this.answerCount = answerCount;
}
/**
*
* @return
* The score
*/
public Integer getScore() {
return score;
}
/**
*
* @param score
* The score
*/
public void setScore(Integer score) {
this.score = score;
}
/**
*
* @return
* The lastActivityDate
*/
public Integer getLastActivityDate() {
return lastActivityDate;
}
/**
*
* @param lastActivityDate
* The last_activity_date
*/
public void setLastActivityDate(Integer lastActivityDate) {
this.lastActivityDate = lastActivityDate;
}
/**
*
* @return
* The creationDate
*/
public Integer getCreationDate() {
return creationDate;
}
/**
*
* @param creationDate
* The creation_date
*/
public void setCreationDate(Integer creationDate) {
this.creationDate = creationDate;
}
/**
*
* @return
* The questionId
*/
public Integer getQuestionId() {
return questionId;
}
/**
*
* @param questionId
* The question_id
*/
public void setQuestionId(Integer questionId) {
this.questionId = questionId;
}
/**
*
* @return
* The link
*/
public String getLink() {
return link;
}
/**
*
* @param link
* The link
*/
public void setLink(String link) {
this.link = link;
}
/**
*
* @return
* The title
*/
public String getTitle() {
return title;
}
/**
*
* @param title
* The title
*/
public void setTitle(String title) {
this.title = title;
}
/**
*
* @return
* The acceptedAnswerId
*/
public Integer getAcceptedAnswerId() {
return acceptedAnswerId;
}
/**
*
* @param acceptedAnswerId
* The accepted_answer_id
*/
public void setAcceptedAnswerId(Integer acceptedAnswerId) {
this.acceptedAnswerId = acceptedAnswerId;
}
/**
*
* @return
* The lastEditDate
*/
public Integer getLastEditDate() {
return lastEditDate;
}
/**
*
* @param lastEditDate
* The last_edit_date
*/
public void setLastEditDate(Integer lastEditDate) {
this.lastEditDate = lastEditDate;
}
}
public class Question {
String title;
String link;
@Override
public String toString() {
return(title);
}
}
4.
Define the REST API for Retrofit via the following interface.
public interface StackOverflowAPI {
@GET("/2.2/questions?order=desc&sort=creation&site=stackoverflow")
Call<StackOverflowQuestions> loadQuestions(@Query("tagged") String tags);
}
more detail can see http://square.github.io/retrofit/
5. use Retrofit class generates an implementation of the interface.
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.stackexchange.com")
.addConverterFactory(GsonConverterFactory.create())
.build();
StackOverflowAPI stackOverflowAPI = retrofit.create(StackOverflowAPI.class);
6. make a asynchronous HTTP request to the remote webserver
Call<StackOverflowQuestions> call = stackOverflowAPI.loadQuestions("android");
//asynchronous call
Response<StackOverflowQuestions> qo=null;
qo = call.execute();
7.get the body to use
qo.body();
other method
call.enqueue(new Callback<Bank>() {
@Override
public void onResponse(Response<Bank> response) {
Bank bank = response.body();
}
@Override
public void onFailure(Throwable t) {
}
});