Android Retrofit 2获取单个列表字符串值

时间:2018-03-17 06:07:36

标签: android json string retrofit2

我在Android Studio上使用Retrofit 2来获取列表字符串没有来自WORDSAPI SYNONYMS的名字。

我从

获得了JSON
  

https://wordsapiv1.p.mashape.com/words/go/synonyms?mashape-key= {我的密钥}

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

window.localStorage.setItem("permissions", JSON.stringify(this.permissions));

你能帮我解决一下如何在Android代码中创建调用,同义词列表并获取同义词列表字符串吗? 这让我感到困惑,因为它与Array JSON不同!!

谢谢!

2 个答案:

答案 0 :(得分:1)

这样做

  

ApiClient:

public interface ApiInterface {

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

}
  

ApiInterface:

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


            }
        });
  

MainActivity - &gt; onCreate():

public class ServerData {

    @SerializedName("word")
    private String word;

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

}
  

ServerData:

{{1}}

答案 1 :(得分:0)

你的Pojo课程: -

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

RetrofitInterface: -

@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