【问题标题】:Android Retrofit 2 get single list string valueAndroid Retrofit 2 获取单个列表字符串值
【发布时间】:2018-03-17 06:07:35
【问题描述】:

我在 Android Studio 上使用 Retrofit 2 从 WORDSAPI SYNONYMS 获取列表字符串无名称。

我从

得到 JSON

https://wordsapiv1.p.mashape.com/words/go/synonyms?mashape-key={my-key}

----JSON----------

{
    "word": "go",
    "synonyms": [
        "proceed",
        "run",
        "depart",
        "go away",
        "function",
        ...,
        "get",
        "plump",
        "extend",
        "fit"
    ]
}

你能帮我如何在 Android 代码中创建 Call、SynonymsList 和获取 Synonyms 列表字符串吗? 这让我感到困惑,因为它与 Array JSON 不同!

谢谢!

【问题讨论】:

  • 显示您现有的代码

标签: android json string retrofit2


【解决方案1】:

这样做

ApiClient:

public class ApiClient {

    private static Retrofit retrofit = null;
    private static final String SERVER_BASE_URL = "https://wordsapiv1.p.mashape.com";

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

API接口:

public interface ApiInterface {

    @GET("/words/go/synonyms")
    Call<ResponseBody> getDataFromServer(@Query("mashape-key") String mashape_key);

}

MainActivity -> onCreate() :

ServerData serverData;

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

        Call<ResponseBody> call = apiService.getDataFromServer(mashape_key);
        call.enqueue(new Callback<ResponseBody>() {

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

                try {
                    Log.e("~~~~~ response", response.body().string());
                    String response = responsebody.body().string();
                    serverData = gson.fromJson(response, ServerData.class);

                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

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


            }
        });

服务器数据:

public class ServerData {

    @SerializedName("word")
    private String word;

    @SerializedName("synonyms")
    private ArrayList<String> synonyms;

}

【讨论】:

  • 我使用 response.body().string(); --- 它显示了 JSON 的所有内容,例如 -- {"word":"go","synonyms":["proceed","run","depart","go away"} --- 如何获取“同义词”中的所有值并将其设置为 String[].. 谢谢!!
  • 你可以使用 serverData.getsynonyms() ,首先将 getter -setter 添加到 ServerData 类。
  • 我做了,但它返回 null。
  • 我明白了。谢谢!
【解决方案2】:

你的 Pojo 课程:-

public class WordsObj {    
  String word;
  List<String> synonyms;
}

改造界面:-

@Get("yOURuRL")
Call<WordsObj> getWords();

您的电话:-

Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("base_url")
            .build();

Call<WordsObj> call_for_words = retrofit.create(RetrofitInterface.class).getWords();

然后打电话:-

call_for_words.enqueue(...) // for asynchronous

或者,

call_for_words.execute()   // for synchornous 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-02
    • 2016-08-05
    相关资源
    最近更新 更多