1)Gradle
compile 'com.squareup.okhttp:okhttp:2.4.0'
compile 'com.squareup.okio:okio:1.5.0'
2)Get
//创建okHttpClient对象
OkHttpClient mOkHttpClient = new OkHttpClient();
//创建一个Request
final Request request = new Request.Builder()
.url("http://www.weather.com.cn/data/sk/101010100.html")
.build();
//new call
Call call = mOkHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(Response response) throws IOException {
String htmlStr = response.body().string();
}
});
3)Post
FormEncodingBuilder builder = new FormEncodingBuilder();
builder.add("username","baobao");
String url = "";
Request request = new Request.Builder()
.url(url)
.post(builder.build())
.build();
OkHttpClient mOkHttpClient = new OkHttpClient();
mOkHttpClient.newCall(request).enqueue(new Callback(){
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(Response response) throws IOException {
}
});