当我在改造中使用JsonObject作为响应时,我的输出是:response: {}
这是我的代码,带有JsonObject响应:
mCall=apiService.Check_App_Version("api/check-app-version/1/"+Utility.Get_App_Version(context));
mCall.enqueue(new Callback<JSONObject>() {
@Override
public void onResponse(Call<JSONObject> call, Response<JSONObject> response) {
Log.e("response",""+response.body().toString()+" "+response.code());
}
@Override
public void onFailure(Call<JSONObject> call, Throwable t) {
Log.e("ERROR",t.toString());
}
});
但是当我在改造中使用Object作为响应时,我的输出是:
response: {data=[{result=1.0, is_necessary=0.0}]}
以下是此代码:
mCall=apiService.Check_App_Version("api/check-app-version/1/"+Utility.Get_App_Version(context));
mCall.enqueue(new Callback<Object>() {
@Override
public void onResponse(Call<Object> call, Response<Object> response) {
Log.e("response",""+response.body().toString()+" "+response.code());
}
@Override
public void onFailure(Call<Object> call, Throwable t) {
Log.e("ERROR",t.toString());
}
});
我想将它用作JsonObject。我的错在哪里?
答案 0 :(得分:0)
请使用GsonConverterFactory在您想要的对象中获取已解析的响应,如果您想要使用Retrofit进行String响应,请使用ScalarsConverterFactory。您可以同时添加两个工厂。
OkHttpClient client = new OkHttpClient.Builder().connectTimeout(3, TimeUnit.MINUTES)
.writeTimeout(3, TimeUnit.MINUTES)
.readTimeout(3, TimeUnit.MINUTES).addInterceptor(interceptor).build();
if (retrofit == null) {
String baseUrl = "http://example.com/";
retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.addConverterFactory(ScalarsConverterFactory.create())
.client(client)
.build();
}