簡體   English   中英

如何使用 retrofit 存儲 json 數組響應

[英]how to store json array response using retrofit

我想調用 api 端點並在我的 android 應用程序中顯示相關響應。 api 采用參數 user_name 並返回該用戶的響應。 響應在 json 數組中,包括日期、位置和 img (base64) 的 json。

回復

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

我面臨的問題是我無法存儲響應並對其進行解析。 我為此使用 retrofit。 api 調用中沒有問題,服務器獲得成功的請求,問題出在客戶端(android 應用程序)。 這是我目前擁有的代碼。 ApiInterface.java

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

任務.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. 此外,對於要使用的響應中的每個鍵值對,您的 model class 中必須有一個變量。

檢查此示例以了解 retrofit 的實際工作原理。

MainActivity.java文件中,更新如下代碼,因為您在 toast 消息中打印rs object,它只打印object 地址而不是其中的值。 所以要打印你收到的 object 的值,你必須調用 object 的方法來得到類似的值。

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());  
            }  
        }  
  
        ... 
    });    
}

並將Task model 更新為

任務.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; }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM