OkHttp
异步:可以直接运行,底层封装了线程
同步:必须放在子线程中执行,不需要重写接口的方法
get:同步
OkHttpClient:客户端
OkHttpclient client = new OkHttpClient();
Request:请求对象
Request r = new Request.Builder().url(网址).get/post(body).build
Response re = client.newCall®.execute();
获得结果
respone.body().String();
post:同步
FormBody:放参数
FormBody body = new FormBody.Builder().add(key,value),build();
吧body给请求对象
异步
获得Call;
Call call = clent.newCall(request)
重写接口方法:
Call.enquequ(new CallBack(){
OnFailure():失败
OnRespone():成功
})
Volley
volley适用于频率高的小数据的下载器
用法:
StringRequest r= new StringRequest(StringRequest.Method.GET/POST,网址,结果监听,错误监听)
获得队列:RequeQuequ que = Volley.newRequeQueue(上下文)
添加队列:que.add®
启动队列:que.start();
post:
重写 getParms(){创建HashMap,put参数}
下载图片:
ImageRequeue img = new ImageRequeue(网址,结果监听,宽,高,Bitmap.Config.GRB656,错误监听);
volley :post
// An highlighted block
public void volleyPost(String url,Context context){
RequestQueue requestQueue = Volley.newRequestQueue(context);
StringRequest request = new StringRequest(StringRequest.Method.POST,url,listener,errorListener){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String,String> hashMap = new HashMap<>();
hashMap.put("phone","14785236995");
hashMap.put("passwd","854721");
return hashMap;
}
};
requestQueue.add(request);
requestQueue.start();
}
volley:get
// An highlighted block
public void volleyGet(String url, Context context){
RequestQueue requestQueue = Volley.newRequestQueue(context);
StringRequest request = new StringRequest(StringRequest.Method.GET,url,listener,errorListener);
requestQueue.add(request);//将请求添加到队列
requestQueue.start();//启动队列
}
volley:get/post 的监听
// An highlighted block
com.android.volley.Response.Listener<String> listener = new com.android.volley.Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e("##########str",response);
}
};
com.android.volley.Response.ErrorListener errorListener = new com.android.volley.Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
};
同步get
// An highlighted block
public String Okhttp_get(String url){
// OkHttpClient.Builder builder = new OkHttpClient.Builder();
// builder.connectTimeout(20000, TimeUnit.MILLISECONDS);//设置连接超时
// builder.readTimeout(4,TimeUnit.MINUTES);//分钟
//
//
// OkHttpClient client = builder.build();//客户端
// Request.Builder builder1 = new Request.Builder();
// builder1.url(url);//访问的网址
// Request request = builder1.build();
//
// Call call = client.newCall(request);
// try {
// Response response = call.execute();
// String string = response.body().string();
// return string;
// } catch (IOException e) {
// e.printStackTrace();
// }
Request request = new Request.Builder().url(url).get().build();
OkHttpClient client = new OkHttpClient();
try {
Response response = client.newCall(request).execute();
if(response.isSuccessful()){
String string = response.body().string();
return string;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
// An highlighted block
var foo = 'bar';
同步post
// An highlighted block
//OkHttp post同步方法,需要在线程中执行
public String OkHttp_post(String url){
// OkHttpClient.Builder obuilder = new OkHttpClient.Builder();
// OkHttpClient client = obuilder.build();
//
// Request.Builder rbuilder = new Request.Builder();
// rbuilder.url(url);
//
// FormBody.Builder builder = new FormBody.Builder();
// builder.add("phone","19968458750");
// builder.add("passwd","145235");
// FormBody body = builder.build();
// rbuilder.post(body);
//
// Request request = rbuilder.build();
// Call call = client.newCall(request);
//
// try {
// Response response = call.execute();
// String string = response.body().string();
// return string;
// } catch (IOException e) {
// e.printStackTrace();
// }
RequestBody body = new FormBody.Builder()
.add("phone","17845698740")
.add("passwd","856974")
.build();
Request request = new Request.Builder().url(url).post(body).build();
OkHttpClient client = new OkHttpClient();
try {
Response response = client.newCall(request).execute();
if(response.isSuccessful()){
String string = response.body().string();
return string;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
异步方法
// An highlighted block
public class MyAsync extends AsyncTask<String,Void,String> {
@Override
protected String doInBackground(String... strings) {
// return new Myutils().HttpUrlConnection_post(strings[0]);
// return new Myutils().Okhttp_get(strings[0]);
return new Myutils().OkHttp_post(strings[0]);
}
}
异步post
// An highlighted block
public void asyncPost(String url){
OkHttpClient client = new OkHttpClient();
FormBody body = new FormBody.Builder()
.add("phone","14785236998")
.add("passwd","569847")
.build();
Request request = new Request.Builder().url(url).post(body).build();
Call call = client.newCall(request);
call.enqueue(new okhttp3.Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
Log.e("########str",string);
}
});
}
异步get
// An highlighted block
public void asyncGet(String url){
final Request request = new Request.Builder().url(url).get().build();
OkHttpClient client = new OkHttpClient();
Call call = client.newCall(request);
call.enqueue(new okhttp3.Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String str = response.body().string();
Log.e("#######str",str);
}
});
}
volley :img下载
// An highlighted block
public void volleyimg(String url,Context context){
RequestQueue requestQueue = Volley.newRequestQueue(context);
ImageRequest request = new ImageRequest(url,listenerimg,100,100,Bitmap.Config.RGB_565,errorListener);
requestQueue.add(request);
requestQueue.start();
}
com.android.volley.Response.Listener<Bitmap> listenerimg = new com.android.volley.Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap response) {
MainActivity.imageView.setImageBitmap(response);
}
};