简体   繁体   中英

how to store json array response using retrofit

I want to call an api endpoint and display the associated response in my android app. The api takes a parameter user_name and return response of that user. Response is in json array consisting json of date, location and img (base64).

RESPONSE

[
   {
        "id": "602a1901a54781517ecb717c",
        "date": "2021-02-15T06:47:29.191000",
        "location": "mall",
        "img": "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAIBAQEBAQIBAQECAgICAgQDAgICAgUEBAMEBgUGBgYFBgYGBwkIBgcJBwYGCAsICQoKCgoKBggLDAsKDAkKCgr/2wBDAQICAgICAgUDAwUKBwYHCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgr/wAARCASvBLADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/
}
]

the issue I am facing is I am not able to store the response and parse it. I am using retrofit for this. There is no issue in the api call the server gets a successfull request the issue is in the client side (android app). Here is the code that i currently have. ApiInterface.java

interface ApiInterface {  
    @FormUrlEncoded  
    @POST("end_point")  
    Call<List<Task>>getstatus(@Field("user_name") String user_name);  
  }

Task.java

public class Task {  
  @SerializedName("user_name")  
  public String user_name;  
  public Task(String user_name) {  
      user_name = user_name.substring(1, user_name.length()-1);  
      this.user_name= user_name;  
  }  
  public String getUser() {  
      return user_name;  
  }
  }

MainActivity.java

private void LoginRetrofit(String user_name) {  
    Retrofit retrofit = new Retrofit.Builder()  
            .baseUrl("url")  
            .addConverterFactory(GsonConverterFactory.create())  
            .build();  
  
    final ApiInterface request = retrofit.create(ApiInterface.class);  
    Call<List<Task>> call = request.getstatus(user_name);  
    Toast.makeText(getApplicationContext(), "user_name" + " " + call.request(), Toast.LENGTH_LONG).show();  
    call.enqueue(new Callback<List<Task>>() {  
        @Override  
  public void onResponse(Call<List<Task>> call, Response<List<Task>> response) {  
            try { 
                List<Task> rs=response.body();  
                Toast.makeText(getApplicationContext(), "Response" +  " "+rs, Toast.LENGTH_LONG).show();  
            }  
            catch (Exception e)  
            {  
                Log.d("REsponse error",e.getMessage());  
            }  
        }  
  
        @Override  
  public void onFailure(Call<List<Task>> call, Throwable t) {  
            Log.d("Error",t.getMessage());  
        }  
    });    
}

This is the response which is being returned.

Your data model class variables should be serialized or to have names as same as in the response, so that retrofit can be able to bind response values to your model variables. Also you must have a variable in your model class for each key-value pair in your response that you want to use.

check this example to get how retrofit really work.

In MainActivity.java file, update the code as below as you are printing rs object in your toast message, it prints only the object address not the value in it. So to print the value of the object you received, you must call the methods of the object to get value like as.

MainActivity.java

private void LoginRetrofit(String user_name) {  
    Retrofit retrofit = new Retrofit.Builder()  
            .baseUrl("url")  
            .addConverterFactory(GsonConverterFactory.create())  
            .build();  
  
    ...  
    call.enqueue(new Callback<List<Task>>() {  
        @Override  
        public void onResponse(Call<List<Task>> call, Response<List<Task>> response) {  
            try { 
                List<Task> rs=response.body();
                if(rs.size() > 0){
                    Task user = rs.get(0);
                    String id = user.getId();
                    String location = user.getLocation();
                    Toast.makeText(getApplicationContext(),"Response : Id="+id+" and location="+location, Toast.LENGTH_LONG).show();
                }
                
            }  
            catch (Exception e)  
            {  
                Log.d("REsponse error",e.getMessage());  
            }  
        }  
  
        ... 
    });    
}

and update the Task model as

Task.java

public class Task { 
    @SerializedName("id") public String id; 
    @SerializedName("date") public String date; 
    @SerializedName("location") public String location; 
    @SerializedName("img") public String img; 

    public Task(String id, String date, String location, String img) { 
        this.id=id; 
        this.date=date; 
        this.location=location; 
        this.img=img; 
    }

    public String getId(){ return this.id; }
    public String getDate(){ return this.date; }
    public String getLocation(){ return this.location; }
    public String getImg(){ return this.img; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM