如何从Retrofit 2.0使用gson 2.0 on - onResponse [英] how to use gson 2.0 on - onResponse from Retrofit 2.0
问题描述
我正在使用新的改进2.0,
和获得回调进行http调用,
i希望使用gson 2.0库来解析该json obj并能够执行
jsonData.sector [2] .sectorOne
这是我的回调函数:
$ b $ $ pre $ retrofitApi.Factory.getInstance()。getCallData()。enqueue新回调< ResponseBody>(){
@Override
public void onResponse(Call< ResponseBody> call,Response< ResponseBody> response){
try {
} catch(IOException e){
e.printStackTrace();
}
}
@Override
public void onFailure(调用< ResponseBody>调用,Throwable t){
Log.d(myLogs,未能恢复数据);
Log.d(myLogs,becouse:+ t );
largeTextVar.setText(fail ed:+ t);
}
});
这就是我的回调看起来像
< $ p
$ data $ {
sector:[
[
{
scanning:false,
sectorOne:,
sectorTwo:,
sectorThree:
},
:false,
sectorOne:,
sectorTwo:,
sectorThree:
}
]
]
},
curserLocation:{
elevation:30,
horizontal:105
}
}
我正在使用:
compile'com.squareup.retrofit2:retrofit:2.0.1'
compile'com.squareup.retrofit2:converter-gson:2.0.1'
我到处寻找如何做到这一点,但我无法找到一个简单的解决方案,
什么是实现这个最简单,最简单的方法?
JSON 响应转换为 POJO
:解决方案
首先,您必须创建一个POJO类: JSON Schema 2 POJO
- 粘贴您的示例
JSON
响应
- 选择来源类型:JSON
- 注释样式:Gson
它会为你生成一个 POJO
类。然后在你的 onResponse
method:
@Override
public void onResponse(Call< ResponseBody>呼叫,响应< ResponseBody>响应){
if(response.code()== 200){
Gson gson = new GsonBuilder()。create();
YourPOJOClass yourpojo = new YourPOJOClass();
尝试{
yourpojo = gson.fromJson(response.body()。toString(),YourPOJOClass.class);
} catch(IOException e){
//处理失败读取错误
Log.v(gson error,gson process时出错);
不要忘记添加编译'com.google.code.gson:gson:2.4'
另一种方法是:创建一个pojo class like above。
在您的API中:
@POST(端点)
公共呼叫< YourPOJOClass> exampleRequest();
当调用这个时:
OkHttpClient okClient = new OkHttpClient.Builder()。connectTimeout(10,TimeUnit.SECONDS).build();
Gson gson = new GsonBuilder()
.setDateFormat(yyyy-MM-dd'T'HH:mm:ssZ)
.create();
Retrofit client = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create(gson))
.client(okClient)
.build();
YourApiClass service = client.create(YourApiClass.class);
致电< YourPOJOClass>调用= service.exampleRequest();
call.enqueue(new Callback< YourPOJoclass>(){
@Override
public void onResponse(Call< Your POJoclass> call,Response< Your POJoclass> response){
// already Gson convertor factory将您的响应主体转换为pojo
response.body()。getCurserLocation();
}
@Override
public void onFailure(Call< YourPOJoclass>电话,Throwable t){
});
i'm doing an http call using the new retrofit 2.0,
and getting a callback,
i want to use gson 2.0 library to parse that json obj and to be able to do
jsonData.sector[2].sectorOne
this is my callback:
retrofitApi.Factory.getInstance().getCallData().enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
try {
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.d("myLogs", "failed to Retrive Data");
Log.d("myLogs", "becouse: "+t);
largeTextVar.setText("failed: " + t);
}
});
this is how my callback will look like
{
"data": {
"sector": [
[
{
"scanned": false,
"sectorOne": "",
"sectorTwo": "",
"sectorThree": ""
},
{
"scanned": false,
"sectorOne": "",
"sectorTwo": "",
"sectorThree": ""
}
]
]
},
"curserLocation": {
"elevation": 30,
"horizontal": 105
}
}
i'm using :
compile 'com.squareup.retrofit2:retrofit:2.0.1'
compile 'com.squareup.retrofit2:converter-gson:2.0.1'
i looked everywhere on how to do this, but i couldn't find a simple solution for this,
what is the simplest, easiest way to achieve this ?
解决方案 Okey i will explain how to convert JSON
response to POJO
:
First of all you must create a POJO class in : JSON Schema 2 POJO
- Paste your example
JSON
response
- Select Source Type : JSON
- annotation style : Gson
It will generate a POJO
class for you.
Then in your onResponse
method :
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if(response.code()==200){
Gson gson = new GsonBuilder().create();
YourPOJOClass yourpojo=new YourPOJOClass ();
try {
yourpojo= gson.fromJson(response.body().toString(),YourPOJOClass.class);
} catch (IOException e) {
// handle failure to read error
Log.v("gson error","error when gson process");
}
}
Dont forget to add compile 'com.google.code.gson:gson:2.4'
Another way to do this : create a pojo class like in above.
In your API :
@POST("endpoint")
public Call<YourPOJOClass> exampleRequest();
When calling this :
OkHttpClient okClient = new OkHttpClient.Builder().connectTimeout(10, TimeUnit.SECONDS).build();
Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
.create();
Retrofit client = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create(gson))
.client(okClient)
.build();
YourApiClass service = client.create(YourApiClass.class);
Call<YourPOJOClass> call=service.exampleRequest();
call.enqueue(new Callback<YourPOJOClass>() {
@Override
public void onResponse(Call<YourPOJOClass> call, Response<YourPOJOClass> response) {
//already Gson convertor factory converted your response body to pojo
response.body().getCurserLocation();
}
@Override
public void onFailure(Call<YourPOJOClass> call, Throwable t) {
}
});
这篇关于如何从Retrofit 2.0使用gson 2.0 on - onResponse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文