gpt4 book ai didi

java - Android JSON解析Retrofit

转载 作者:行者123 更新时间:2023-11-30 10:34:07 25 4
gpt4 key购买 nike

我是 Retrofit 和 JSON 的新手,我真的不知道如何解析下一个 json 字符串:

{
"weight":[
{ "bmi":21,
"date":"2016-12-09",
"fat":14.059000015258789,
"logId":1222222222222,
"source":"Aria",
"time":"11:58:24",
"weight":68
},
{ "bmi":21.83,
"date":"2016-12-14",
"logId":1222222222223,
"source":"Aria",
"time":"14:31:39",
"weight":70.7
}
]

我只想要权重数组中的“权重”和“日期”。我已经按照一些示例创建了一个 pojo 类,但它不起作用。此外,在我的 pojo 类中尝试使用 .string() 时,我无法将“重量”作为字符串(然后我将其用作 double )。

(我知道使用 .toString() 会显示类似“com.myPackage.MyPojo@xxxx”的内容)。

目前,我只能通过 ResponseBody 获取整个 json:

Call<ResponseBody>call = repository.getFitbitApi().getData();
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
try {
System.out.println(response.body().string());
} catch (IOException e) {
e.printStackTrace();
}
}

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

}
});

我做错了什么?这是我的 pojo 类(class),只是一次尝试......:

public class WeightList {

@SerializedName("weight")
@Expose

private ArrayList<WeightLogFitbit> weight = new ArrayList<>();

public WeightList(){

}

public ArrayList<WeightLogFitbit> getWeight() {
return weight;
}


public void setWeight(ArrayList<WeightLogFitbit> weight) {
this.weight = weight;
}

}并且:

public class WeightLogFitbit {

//Variables in my JSON
@SerializedName("bmi")
@Expose
private String bmi;

@SerializedName("date")
@Expose
private String date;

@SerializedName("logId")
@Expose
private String logId;

@SerializedName("source")
@Expose
private String source;

@SerializedName("time")
@Expose
private String time;

@SerializedName("weight")
@Expose
private double weight;

@SerializedName("fat")
@Expose
private String fat;

public WeightLogFitbit(){}

//Getters and setters
public double getWeight() {
return weight;
}

public void setWeight(double weight) {
this.weight = weight;
}

public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}

public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}

public String getBmi(){
return bmi;
}
public void setBmi(String bmi) {
this.bmi = bmi;
}

//
public String getFat(){
return fat;
}
public void setFat(String fat) {
this.fat = fat;
}


public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getLogId() {
return logId;
}
public void setLogId(String logId) {
this.logId = logId;
}

注意:我正在使用 RxSocialConnect库,它实现了 RxJava、Retrofit 2、OkHttp3 和 gson,以防万一。我在 this 之后做了这个示例。

我正在使用的其他类(class):

public class FitbitBtnActivity extends AppCompatActivity {

private FitbitRepository repository;

@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fitbit_btn);

repository = new FitbitRepository();

setUpFitbit();

}


private void setUpFitbit() {
findViewById(R.id.fitbitbtn).setOnClickListener(v ->
RxSocialConnect.with(this, repository.fitbitService())
.subscribe(response -> response.targetUI().showToken(response.token()),
error -> showError(error))
);

findViewById(R.id.retrievebtn).setOnClickListener(v -> {
Call<ResponseBody>call = repository.getFitbitApi().getData();
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
try {
System.out.println(response.body().string());
} catch (IOException e) {
e.printStackTrace();
}
}

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

}
});

//Original code from example in RxSocialConnect

/*.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<Object>() {
@Override
public void call(Object user) {
FitbitBtnActivity.this.showUserProfile(user.toString());
}
},
error -> FitbitBtnActivity.this.showError(error));*/

}
);
}

和:

public class FitbitRepository {


private final FitbitApiRest fitbitApi;

public FitbitRepository() {
fitbitApi = initFitbitApiRest();
}

private FitbitApiRest initFitbitApiRest() {
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(new OAuth2Interceptor(FitbitApi20.class))
.build();

return new Retrofit.Builder()
.baseUrl(FitbitApiRest.URL_BASE)
.client(client)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build().create(FitbitApiRest.class);
}



FitbitApiRest getFitbitApi() {
return fitbitApi;
}



interface FitbitApiRest {
String URL_BASE = "https://api.fitbit.com";

@GET("myrequest.json")
Call<ResponseBody> getData();
}

OAuth20Service fitbitService() {


final String client_id = "xxxxx";
final String client_secret = "1xxxxxxxxxxxxxxxxxx";
final String redirect_uri = "http://example.com";
final String permissions = "weight";

return new ServiceBuilder()
.apiKey(client_id)
.apiSecret(client_secret)
.callback(redirect_uri)
.scope(permissions)
.build(FitbitApi20.instance());
}

最佳答案

您需要将其添加到您的依赖项中:

compile 'com.squareup.retrofit2:converter-gson:your-version'

然后像这样向您的 Retrofit 实例添加一个 gson 转换器:

Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.example.com")
.addConverterFactory(GsonConverterFactory.create())
.build();

并更改您在 api 中的调用以返回 WeightList:

Call<WeightList> getData();

关于java - Android JSON解析Retrofit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41836625/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com