如何使用retrofit2接收Json数组(“结果”)

我知道如何接收这种类型的数组:


[

{

    "username": "luis",

    "job": "developer",

    "age": 23

}

]

我的问题是当我必须接收一个具有特定名称的数组时,如下所示:


{"result":[{"userid":"1","username":"Luis","job":"developer","age":"23"}]}

在这种情况下,我必须使用retrofit2 接收上面名为“result”的数组。任何人都可以帮助我我是 Retrofit 的新手。


这是我尝试过的:


主要活动


apiInterface = ApiClient.getApiClient().create(ApiInterface.class);

       Call<List<Workers>> call = apiInterface.getWorkers();


       call.enqueue(new Callback<List<Workers>>() {

           @Override

           public void onResponse(Call<List<Workers>> call, Response<List<Workers>> response) {

               list=response.body();

               adapter = new WorkerAdapter(getApplicationContext(),list);

               recyclerView.setAdapter(adapter);



           }


           @Override

           public void onFailure(Call<List<Workers>> call, Throwable t) {


           }

       });

Api客户端:


public class ApiClient {


    public static final String BASE_URL="http://192.168.31.206/test1_database/";

    public static Retrofit retrofit = null;


    public static Retrofit getApiClient(){


        if (retrofit==null){

            retrofit=new Retrofit.Builder().baseUrl(BASE_URL)

                    .addConverterFactory(GsonConverterFactory.create()).build();

        }

        return retrofit;

    }


}

接口:


public interface ApiInterface {


    @GET("getAllUser.php")

    Call<List<Workers>> getWorkers();



}

我的 POJO 或 Workers 类:


public class Workers {


    @SerializedName("username")

    String Name;

    @SerializedName("job")

    String Job;

    @SerializedName("age")

    int Age;


    public String getName() {

        return Name;

    }


    public String getJob() {

        return Job;

    }


    public int getAge() {

        return Age;

    }

最后是我的 RecyclerAdpter:


public class WorkerAdapter extends RecyclerView.Adapter<WorkerAdapter.ViewHolder>{

    Context context;

    List<Workers> list;


    public WorkerAdapter(Context context,List<Workers> list) {

        this.context = context;

        this.list = list;

    }



我已经被困了两天了,我仍然无法解决它。请帮忙!


哔哔one
浏览 418回答 3
3回答

慕桂英4014372

创建一个名为 Result 的类模型,并在 Interface 类中编写以下代码:&nbsp;@GET("your endpoint")&nbsp;Call<Result>getResult();并在 Result 类中编写以下代码:&nbsp;@SerializedName("result")&nbsp;&nbsp;private List<UserInfo> userInfo;祝你好运。

桃花长相依

如果你必须使用这种 json 格式{"result":[{"userid":"1","username":"Luis","job":"developer","age":"23"}]}你必须像这样创建两个模型:class Result {&nbsp; &nbsp; &nbsp;@SerializedName("result")&nbsp; &nbsp; &nbsp;@Expose&nbsp; &nbsp; &nbsp;List<ResultDetail> result;&nbsp;}&nbsp;class ResultDetail {&nbsp; &nbsp; &nbsp;@SerializedName("userid")&nbsp; &nbsp; &nbsp;@Expose&nbsp; &nbsp; &nbsp;String userId ;// use int instead&nbsp;&nbsp; &nbsp; &nbsp;String username;&nbsp; &nbsp; &nbsp;String job;&nbsp; &nbsp; &nbsp;String age; // use int instead}将 GsonConverterFactory 添加到您的应用程序 build.gradlecom.squareup.retrofit2:converter-gson:2.14现在构建改造实例:&nbsp;Retrofit retrofit = new Retrofit.Builder().baseUrl("some base url like : www.example.com/api/").addConverterFactory(GsonConverterFactory.create()).build();&nbsp;YourSerivce service = retrofit.create(YourService.class);&nbsp;interface YourService {&nbsp; &nbsp;@GET("someThing")&nbsp; // complete url is www.example.com/api/someThing&nbsp; &nbsp;Call<Result> getResult();&nbsp;}最后得到这样的结果:retrofit.getResult().enqueue(.....)

慕少森

package com.example;import java.util.List;public class Example {private List<Result> result = null;public List<Result> getResult(){&nbsp; &nbsp; return result;}public void setResult(List<Result> result){&nbsp; &nbsp; this.result = result;}}package com.example;public class Result {private String userid;private String username;private String job;private String age;public String getUserid(){&nbsp; &nbsp; return userid;}public void setUserid(String userid){&nbsp; &nbsp; this.userid = userid;}public String getUsername(){&nbsp; &nbsp; return username;}public void setUsername(String username){&nbsp; &nbsp; this.username = username;}public String getJob(){&nbsp; &nbsp; return job;}public void setJob(String job){&nbsp; &nbsp; this.job = job;}public String getAge(){&nbsp; &nbsp; return age;}public void setAge(String age){&nbsp; &nbsp; this.age = age;}}使用本网站制作一些模型。我已经使用这个网站完成了上面的代码。现在,这样做@Get<"url">call<Example> get()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java