OKhttp初步

时间:2024-01-01 09:36:03

OkHttp

使用方法:
1.分为同步和异步。
同步:
1.get
Request request = new Request.Builder().url(utl).build();
Response respone = client.newCall(request).execute();
if(respone.isSuccessful()){
retrurn respone.body().string();
}
else {
throw new IOExceptrion(""+respone);
}

1.Request 是请求。 Reponse是响应。

Post提交键值对
RequestBody body=new FormEncodeingBuilder().add("name","value").add
("name1","value1").build();

Request request =new Request.Builder().url(url).post(body).build();

同步步骤:
RequestBody(可选)
Request
Response
client.newCall(request).execute();

异步:
client.newCall(request).enqueue(new CallBack(){
@override
public void onFailure(Request request,IOException e){

}
@override
public void onResponse (final Request respone)throws IOException{

}
});