简体   繁体   English

如果请求成功,我如何检索“解析响应”对象,然后在我的viewmodel类中使用该对象?

[英]How do i retrieve the “Parse Response” object if the request is successful and then use the object in my viewmodel class?

public ParseResponse getParseResponseObj(ParseRequest req){
   this.parseResponse =  getParseResponse(req);
    return this.parseResponse;
}

private ParseResponse getParseResponse(ParseRequest request){

    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    logging.setLevel(HttpLoggingInterceptor.Level.BODY);

      final String BASE_URL = "https://api.infermedica.com/v2/";
      OkHttpClient.Builder httpClient =
            new OkHttpClient.Builder();
      httpClient.addInterceptor(logging);

      Retrofit.Builder builder =
            new Retrofit.Builder().baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(httpClient.build());

        Retrofit retrofit = builder.client(httpClient.build()).build();
        InfermedicaClient infermedicaClient = retrofit.create(InfermedicaClient.class);
    //final MutableLiveData<ParseResponse> resp = new MutableLiveData<>();
    //parseResponse = new ParseResponse();
    Call<ParseResponse> call = infermedicaClient.parseUserInput(Constants.APP_ID,Constants.APP_KEY,request);

    call.enqueue(new Callback<ParseResponse>() {
        @Override
        public void onResponse(Call<ParseResponse> call, Response<ParseResponse> response) {

            if (response.isSuccessful()) {
                 parseResponse = response.body();
             }
            Log.v("ParseReq Success" , response.message());

        }

        @Override
        public void onFailure(Call<ParseResponse> call, Throwable t) {

            Log.v("ParseReq Success" , t.getMessage());


        }
    });


    return parseResponse;
}

How do i retrieve the parse response object from a successful request and then use it in my viewmodel class.Also this request happens only once so, i didn't use livedata,also i am only using the response object in the viewmodel;am not displaying it to the user.i'm getting a null value even though the response from the server is ok.the getParseResponseObj is the method i call from the viewmodel!. 我如何从成功的请求中检索解析的响应对象,然后在我的viewmodel类中使用它。该请求仅发生一次,所以我没有使用livedata,也只是在viewmodel中使用了响应对象;不是向用户显示它。即使来自服务器的响应正常,我也得到了一个空值.getParseResponseObj是我从viewmodel调用的方法!

我已经解决了这个问题,我的响应对象与服务器返回的响应不匹配,因此gson无法正确序列化数据。

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

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