代码之家  ›  专栏  ›  技术社区  ›  Thuan Vo

Android改造2获取单列表字符串值

  •  -1
  • Thuan Vo  · 技术社区  · 8 年前

    我正在Android Studio上使用Reformation 2从WORDSAPI同义词中获取列表字符串no name。

    我从

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

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

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

    你能帮我创建 调用、同义词列表和获取同义词列表字符串 在Android代码中?? 这让我很困惑,因为它与数组JSON不同!!

    非常感谢。

    2 回复  |  直到 8 年前
        1
  •  1
  •   Milan Hirpara    8 年前

    像这样做

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

    主要活动->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;
    
    }
    
        2
  •  0
  •   Santanu Sur    8 年前

    您的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