gpt4 book ai didi

android - OkHttp:无法从静态内容引用非静态方法

转载 作者:行者123 更新时间:2023-11-29 15:46:24 26 4
gpt4 key购买 nike

我正在跟进这个 link 的例子我收到错误:

Non-static method 'newCall(com.squareup.okhttp.Request) cannot be referenced from a static content

在这一行 Call call = OkHttpClient.newCall(request);

这是代码

public class MainActivity extends Activity {

public OkHttpClient client = new OkHttpClient();
String requestUrl = " http://iheartquotes.com/api/v1/random?format=json";
Request request = new Request.Builder().url(requestUrl).build();
TextView text1;
public static final MediaType JSON =
MediaType.parse("application/json; charset=utf-8");

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text1 = (TextView) findViewById(R.id.messageText);
}

call = OkHttpClient.newCall(request);

}

这是什么原因?

最佳答案

您已经创建了一个客户端。而不是

call = OkHttpClient.newCall(request);

您的代码应如下所示:

call = client.newCall(request);

您需要创建的客户端引用。

关于android - OkHttp:无法从静态内容引用非静态方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32779729/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com