【问题标题】:Getting the API, Android Studio static获取 API,Android Studio 静态
【发布时间】:2021-05-16 11:21:34
【问题描述】:

我遇到了PlaceholderAPI.getPosts(); 显示的问题 Non-static method 'getPosts()' cannot be referenced from a static context 我才刚刚开始,所以我正在学习一个教程(我在编写 android 应用程序方面有一些经验,但并不多) 代码是:

protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://jsonplaceholder.typicode.com/")
                .build();
        Call<List> call = PlaceholderAPI.getPosts();
        call.enqueue(new Callback<List>()
        {
            @Override
            public void onResponse(Call<List> call, Response<List> response)
            {
                Log.d("Yo", "Success!");                
            }
            @Override
            public void onFailure(Call<List> call, Throwable t)
            {
                Log.d("Yo", "Error!");
            }
        });
        Log.d("Yo","Hello World!");
    }

PlaceholderAPI.java 的代码是一个带有代码的接口:

@GET("posts")
    Call<List> getPosts();

【问题讨论】:

标签: java android android-studio


【解决方案1】:

对于连接创建一个ApiClient 类。

public class ApiClient {
 
    private static Retrofit retrofit = null;
    public static final String base_URL ="https://jsonplaceholder.typicode.com/";
    static final OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .connectTimeout(20, TimeUnit.SECONDS)
            .writeTimeout(20, TimeUnit.SECONDS)
            .readTimeout(30, TimeUnit.SECONDS)
            .build();

    public static Retrofit getApiClient(){

        if(retrofit==null){

            Gson gson = new GsonBuilder()
                            .setLenient()
                            .create();

            retrofit = new Retrofit.Builder()
                    .baseUrl(base_URL)
                    .addConverterFactory(GsonConverterFactory.create(gson))
                    .client(okHttpClient)
                    .build();
        }
        return retrofit;
    }
}

您可以像这样创建与您的接口的连接,然后您可以通过接口函数将您的调用加入队列。

   PlaceholderAPI placeholder= ApiClient.getApiClient().create(PlaceholderAPI.class);
  Call<List> call = PlaceholderAPI.getPosts();
        call.enqueue(new Callback<List>()
        {
            @Override
            public void onResponse(Call<List> call, Response<List> response)
            {
                Log.d("Yo", "Success!");                
            }
            @Override
            public void onFailure(Call<List> call, Throwable t)
            {
                Log.d("Yo", "Error!");
            }
        });
        Log.d("Yo","Hello World!");
    }

【讨论】:

  • 我试图理解代码,因为它没有解决我的问题。 ".getPosts()" 仍然从静态上下文中引用...
【解决方案2】:

PlaceHolderAPI.getPosts() 不是静态方法。对类调用方法要求该方法是静态的。

您需要做以下两件事之一:

改变接口的方法:

static Call<List> getPosts

访问实现PlaceHolderAPI 接口的对象 - 让我们调用对象placeHolderObj 并调用该对象的方法:

placeHolderObj.getPosts()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-17
    • 1970-01-01
    • 2016-04-14
    • 2016-10-13
    • 2015-09-02
    • 1970-01-01
    相关资源
    最近更新 更多