You can follow the following steps
1- create pojo classes
2- add lib retrofit to your project
3- make requests using the rest concept (so step 2)
1 create pojo classes
The class "pojo" serves to model the structure of your json in Java, basically it represents on the android side the data that will be received for the json. This class is used by retrofit to convert the received json to the structure of the passed class (pojo). You can generate these classes (pojo) using the following website: link you just copy a valid json and it will return you the pojo classes. For your case we have
Posts.java
public class Postagens{
private String POST;
private String URLIMG;
private String ID;
private String SOBRENOME;
private String IDUSER;
private String NOME;
public String getPOST ()
{
return POST;
}
public void setPOST (String POST)
{
this.POST = POST;
}
public String getURLIMG ()
{
return URLIMG;
}
public void setURLIMG (String URLIMG)
{
this.URLIMG = URLIMG;
}
public String getID ()
{
return ID;
}
public void setID (String ID)
{
this.ID = ID;
}
public String getSOBRENOME ()
{
return SOBRENOME;
}
public void setSOBRENOME (String SOBRENOME)
{
this.SOBRENOME = SOBRENOME;
}
public String getIDUSER ()
{
return IDUSER;
}
public void setIDUSER (String IDUSER)
{
this.IDUSER = IDUSER;
}
public String getNOME ()
{
return NOME;
}
public void setNOME (String NOME)
{
this.NOME = NOME;
}
@Override
public String toString()
{
return "ClassPojo [POST = "+POST+", URLIMG = "+URLIMG+", ID = "+ID+", SOBRENOME = "+SOBRENOME+", IDUSER = "+IDUSER+", NOME = "+NOME+"]";
}}
MyPojo.java
public class MyPojo{
private Postagens[] postagens;
private String[][] ups;
public Postagens[] getPostagens ()
{
return postagens;
}
public void setPostagens (Postagens[] postagens)
{
this.postagens = postagens;
}
public String[][] getUps ()
{
return ups;
}
public void setUps (String[][] ups)
{
this.ups = ups;
}
@Override
public String toString()
{
return "ClassPojo [postagens = "+postagens+", ups = "+ups+"]";
}}
2 add retrofit
This lib is responsible for making requests and "abstracting" all required coding, you can find it here: link
concept rest: link
3 Requests using rest
For this step it is necessary that you have read and understood step 2.
Here's an example of how to use retrofit.
Retrofit Interface
public interface RetrofitService {
@GET("/postagens/{user_id})
Call<MyPojo> getPostagens(@Path ("user_id")String user);}
In this interface will be added all your requests in will be made use of the retrofit. The next step is where the request will be made for your problem so it is necessary to add the url base of the server to which the request will be made, it is important to remove the "/" from the end of the url. Here is the code:
public class Requisicao {
private static Retrofit retrofit;
private static RetrofitService service;
public static MyPojo getPostagens(String user_id){
// adding base url
retrofit = new Retrofit.Builder()
.baseUrl("URL_BASE")//exemplo: www.facebook.com
//GsonConverterFactory é utilizada para transformar o //json na classe MyPojo
.addConverterFactory(GsonConverterFactory.create())
.build();
service = retrofit.create(AvososService.class);//utilizando metodo definido na interface
// with this call we have the following url www.baseurl.com/posts/user_id
Call<MyPojo> postagens= service.getPostagens(user_id);
// checking for return
postagens.enqueue(new Callback<MyPojo>() {
MyPojo mPojo;
@Override
public void onResponse(Response<MyPojo> response, Retrofit retrofit) {
if (response.message().equals("OK"))//retorno da requisicao ok
{
mPojo = response.getBody();
}
}
@Override
public void onFailure(Throwable t) {
t.printStackTrace();
}
});
return mPojo;
}}
It is necessary to add in the build.gradle of the project the following dependency that will import the class GsonConverterFactory
compile 'com.squareup.retrofit: convert-gson: 2.0.0-beta2'
This is done to make the request:
Requisicao.getPostagens(user_id);