implementation 'com.squareup.okhttp3:okhttp:3.12.1'
okhttp是一个第三方类库,用于android中请求网络。
这是一个开源项目,是安卓端最火热的轻量级框架,由移动支付Square公司贡献(该公司还贡献了Picasso和LeakCanary)
。用于替代HttpUrlConnection和Apache HttpClient(android API23
里已移除HttpClient)。
1. public void OKHttp_post(final String urlPath){ new Thread(new
Runnable() {
@Override
public void run() {
FormBody formBody = new FormBody.Builder().add(“phone”, “wuyancu”).add(“passwd”, “wert”).build();
Request request = new Request.Builder().url(urlPath).post(formBody).build();
OkHttpClient client = new OkHttpClient();
try {
String s = client.newCall(request).execute().body().toString();
Log.e("OkHttp_post",s);
} catch (IOException e) {
e.printStackTrace();
}
} }).start(); }
2. public void OkHttp_get(final String urlPath){ new Thread(new
Runnable() {
@Override
public void run() {
Request request = new Request.Builder().url(urlPath).get().build();
OkHttpClient client = new OkHttpClient();
try {
Response response = client.newCall(request).execute();
String string = response.body().string();
Log.e("OKHttp_get",string);
} catch (IOException e) {
e.printStackTrace();
}
} }).start(); }
3. public void OkHttp_Asyn_Post(String urlPath){
FormBody body = new FormBody.Builder().add(“phone”,“222”).add(“password”,“sadnuas”).build();
Request request = new Request.Builder().url(urlPath).build();
OkHttpClient client = new OkHttpClient();
Call call =client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.e("OkHttp_Asyn_Post",response.body().toString());
}
});
}
**4**. public void OkHttp_Asyn_Get(String urlPath){
final Request request = new Request.Builder().url(urlPath).get().build();
OkHttpClient client = new OkHttpClient();
Call call =client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.e("OkHttp_Asyn_Get",response.body().toString());
}
});
}
implementation 'com.android.volley:volley:1.1.0'
所谓Volley,它是2013年Google I/O上发布的一款网络框架,基于Android平台,能使网络通信更快,更简单,更健全
优点:
(1)默认Android2.3及以上基于HttpURLConnection,2.3以下使用基于HttpClient;
(2)符合Http 缓存语义 的缓存机制(提供了默认的磁盘和内存等缓存);
(3)请求队列的优先级排序;
(4)提供多样的取消机制;
(5)提供简便的图片加载工具(其实图片的加载才是我们最为看重的功能);(6)一个优秀的框架。
缺点:
它只适合数据量小,通信频繁的网络操作,如果是数据量大的,像音频,视频等的传输,还是不要使用Volley的为好
---------------------
1. public void VolleyImage(String urlstr, Context context, final
ImageView imageView) {
ImageRequest imageRequest = new ImageRequest(urlstr, new com.android.volley.Response.Listener() {
@Override
public void onResponse(Bitmap response) {
imageView.setImageBitmap(response);
}
}, 100, 100, Bitmap.Config.ARGB_8888, errorListener);
RequestQueue requestQueue = Volley.newRequestQueue(context);//获得volley队列
requestQueue.add(imageRequest);
requestQueue.start();
}
2. public void Volley_post(final String urlPath,Context context){
StringRequest request = new StringRequest(StringRequest.Method.POST, urlPath, listener,
errorListener) {
@Override
protected Map
HashMap
map.put(“phone”, “10091661”);
map.put(“passwd”, “169”);
return map;
}
};
queue.add(request);
queue.start();
}
3. public void Volley_get(final String urlPath, Context context){
queue= Volley.newRequestQueue(context);
StringRequest request = new StringRequest(StringRequest.Method.GET, urlPath, listener,
errorListener);
queue.add(request);
queue.start();
}
com.android.volley.Response.ErrorListener errorListener=new com.android.volley.Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
};
com.android.volley.Response.Listener listener=new com.android.volley.Response.Listener() {
@Override
public void onResponse(String response) {
Log.e("Volley_Get",response);
}
};