我正在尝试做的事情::
我正在尝试学习Okhttp 在 android 中进行网络调用的用法
我做了什么::
- 我在这里阅读了他们的文档
- 我已经在项目中下载并添加了 JAR
- 我在这里使用他们的示例代码
我的代码::
MainActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.internal.http.Request;
import com.squareup.okhttp.internal.http.Response;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Request request = new Request.Builder()
.url("https://raw.github.com/square/okhttp/master/README.md")
.build();
Response response = client.execute(request);
Log.d("ANSWER", response.body().string());
}
}
我面临的错误::
在这一行中Response response = client.execute(request);
,我收到错误:
客户端无法解析为变量
如何解决这个问题!
{更新}
public class MainActivity extends Activity {
OkHttpClient client = new OkHttpClient();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Request request = new Request.Builder()
.url("https://raw.github.com/square/okhttp/master/README.md")
.build();
Response response = client.execute(request);
Log.d("ANSWER", response.body().string());
}
}
现在我面临错误:
在该行
Response response = client.execute(request);
中The method execute(Request) is undefined for the type OkHttpClient