简体   繁体   English

如何使用Android的Retrofit获取JSON对象

[英]How to get JSON Object using Retrofit for Android

I'm using Android and DRF, Retrofit now. 我正在使用Android和DRF,现在进行改装。

I'm trying to get Django models using Retrofit, It returns JSON Response. 我正在尝试使用Retrofit获得Django模型,它返回JSON响应。

But in my model, there is multiple JSON Objects. 但是在我的模型中,有多个JSON对象。

For example, 例如,

request url is [object1's url] 请求网址是[object1的网址]

and

response = { object2:[object2's url], some other data(s) } 响应= {object2:[object2的url],其他一些数据}

How can I change object2 to json object using Retrofit? 如何使用翻新将object2更改为json对象?


Edit 编辑

I solved this issue using multiple AsyncTask. 我使用多个AsyncTask解决了这个问题。 get Object1 and object2's url, and in onPostExecute, make some new AsyncTask, get Object2 by Retrofit, check Object2's url and Object2's list parameter 'url' and match. 获取Object1和object2的url,然后在onPostExecute中创建一些新的AsyncTask,通过Retrofit获取Object2,检查Object2的url和Object2的列表参数“ url”并进行匹配。 for example, 例如,

AsyncTask() {
  doInBackground() {
    get object1List
  }
  onPostExecute(object1List) {
    AsyncTask2() {
      doInBackground() {
        get object2List
        for (object1 : object1List)
          object2ListItem & object1.url match
      }
      onPostExecute() {
        something to do
      }
    }
  }
}

i recommend you to receive the raw JSON and then parsing with GSON 我建议您接收原始的JSON,然后使用GSON进行解析

private static Retrofit retrofit = null;
private static Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().setLenient().create();

public static Retrofit getClient(String baseUrl) {
        if (retrofit==null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(baseUrl)
                    .addConverterFactory(GsonConverterFactory.create(gson)) 
                    .client(httpClient.build())
                    .build();
        }
        return retrofit;
    }

and create a class where got the expected information example 并创建一个获得预期信息示例的类

public class People{
    @Expose
    private String Name;
    @Expose
    private Int Age;
    @Expose
    private String Surname;

// setters and getters...

the api interface api接口

public interface APIService {
 @POST("people.php")
    Call<whatyoureceive> response();

}

Also

private static final String BASE_URL = "YOUR URL";

public static APIService getAPIService() {

    return RetrofitClient.getClient(BASE_URL).create(APIService.class);
}

In your activity 在您的活动中

private APIService mAPIService;

mAPIService = ApiUtils.getAPIService();



public void sendRequest() {
        mAPIService.responseo().enqueue(new Callback<PlanDetails>() {
@Override
            public void onResponse(......) {
// what you want to do
}
@Override
            public void onFailure(.....) {
// what you want to do
}

More information https://futurestud.io/tutorials/retrofit-getting-started-and-android-client 更多信息https://futurestud.io/tutorials/retrofit-getting-started-and-android-client

Hope help! 希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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