Android Retrofit-2"无法获取字符串数组"

时间:2018-03-22 04:18:20

标签: android json string retrofit2

我刚开始使用Retrofit 2,所以我遇到了一些问题...... 我需要得到" syn "的String [] in" 名词"。 我试过了,但没有回复!

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

谢谢!

  

我的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);

}
  

名词

    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;
    }

}
  

实施例

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());

            }



        });

2 个答案:

答案 0 :(得分:1)

使用此代码的实施

 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());
        }
    });

答案 1 :(得分:0)

试试这个:

 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) {

                Noun s = response.body().getNoun();
                Log.e("aaa",""+s.getSyn().toString());
                Log.e("aaa",""+s.getSyn().size());
                Log.w("response",new GsonBuilder().setPrettyPrinting().create().toJson(response));

            }

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

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

            }



        });

生成模型类:

使用此链接生成pojo类:http://www.jsonschema2pojo.org/

  • 目标语言:Java

  • 来源类型:JSON

  • 注释风格:GSON

  • 包含getter和setter

只需下载zip并将其添加到您的项目中即可。

输出:

enter image description here