我在Android Studio上使用Retrofit 2来获取列表字符串没有来自WORDSAPI SYNONYMS的名字。
我从
获得了JSONhttps://wordsapiv1.p.mashape.com/words/go/synonyms?mashape-key= {我的密钥}
---- JSON ----------
window.localStorage.setItem("permissions", JSON.stringify(this.permissions));
你能帮我解决一下如何在Android代码中创建调用,同义词列表并获取同义词列表字符串吗? 这让我感到困惑,因为它与Array JSON不同!!
谢谢!
答案 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