gpt4 book ai didi

Android Retrofit-2 "Unable to Get String Array"

转载 作者:行者123 更新时间:2023-11-29 01:03:15 40 4
gpt4 key购买 nike

我刚开始使用 Retrofit 2,所以我遇到了一些问题...我需要在“noun”中获取“syn”的 String[]。我试过了,但它什么也没返回!

String[] s = response.body().getNoun().getSyn();

谢谢!!

My JSON:

{
"adjective": {
"ant": [
"bad",
"evil"
],
"syn": [
"full",
"estimable",
"honorable",
"respectable",
"salutary",
"unspoiled",
"unspoilt",
"in effect",
"in force",
"well"
]
},
"noun": {
"ant": [
"bad",
"badness",
"evil",
"evilness"
],
"syn": [
"goodness",
"commodity",
"trade good",
"advantage",
"artefact",
"artifact",
"morality",
"quality",
"vantage"
]
},
"adverb": {
"ant": [
"ill"
],
"syn": [
"well",
"thoroughly",
"soundly"
]
}
}

API_Client:

public class ApiClient {

private static Retrofit retrofit = null;
private static final String SERVER_BASE_URL = "http://www.json-generator.com/api/json/get/";

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

APIInterface

public interface ApiInterface {

@GET("bZKyFPxTIi")
Call<Example> getDataFromServer(@Query("indent") int s);

}

Noun

    public class Noun {

@SerializedName("syn")
@Expose
private String[] syn = null;
@SerializedName("ant")
@Expose
private String[] ant = null;

public String[] getSyn() {
return syn;
}

public void setSyn(String[] syn) {
this.syn = syn;
}

public String[] getAnt() {
return ant;
}

public void setAnt(String[] ant) {
this.ant = ant;
}

}

Example

public class Example {

@SerializedName("adjective")
@Expose
private Adjective adjective;
@SerializedName("verb")
@Expose
private Verb verb;
@SerializedName("noun")
@Expose
private Noun noun;

public Adjective getAdjective() {
return adjective;
}

public void setAdjective(Adjective adjective) {
this.adjective = adjective;
}

public Verb getVerb() {
return verb;
}

public void setVerb(Verb verb) {
this.verb = verb;
}

public Noun getNoun() {
return noun;
}

public void setNoun(Noun noun) {
this.noun = noun;
}

}

MainActivity

ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);

Call<Example> call = apiService.getDataFromServer(2);

Log.d("URL Called", call.request().url() + "");

call.enqueue(new Callback<Example>() {

@Override
public void onResponse(Call<Example> call, Response<Example> response) {

String[] s = response.body().getNoun().getSyn();
Log.e("",""+s);

}

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

Log.e("error", ""+t.getLocalizedMessage());

}



});

最佳答案

使用这段代码实现

 ideaInterface.getPdfUrl("application/json",pdfRequestPojo).enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

Log.e("strace ","** "+response.code() + " "+response.message());

if (response.isSuccessful())
{
try {
// String result = response.body().string();

String result = response.body().string();

Log.d("userloginreports","*** "+result);

JSONObject jsonObject = new JSONObject(result);
JSONObject json2 =
jsonObject.getJSONObject("adjective");
test= (String) json2.get("syn");

Log.d("userl","*** "+test);
// Idea_Urban.getInstance().save_Logoutdata(jsonObject.getString("data"));
Toast.makeText(getApplicationContext(),test,Toast.LENGTH_LONG).show();

sentreports();

} catch (Exception e) {
Log.e("s2233e ","11111 "+e.toString());
e.printStackTrace();
}
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {

Log.e("stgghb ","5522222 "+t.toString());
}
});

关于Android Retrofit-2 "Unable to Get String Array",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49420477/

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