Download Framework
okhttp
Asynchronous: can run, the bottom package of thread
synchronization: the child must be placed in the thread run, the interface does not require rewriting
get method
1, okhttp client
OkHttpClient = new new OkHttpClient Client ();
2, the request object Request
Request build Request.Builder new new = () URL (URL) .get () Build ();..
. 3, Response result objects
the Response Response = client.newCall (Build) .execute ();
. 4, the results obtained
String json = response.body () .string ();
POST method
formBody discharge parameters
. formBody builder = new FormBody.Builder () add ( key, value) .build ();
Call Call = client.newCall (Request);
rewriting the Callback ()
volley for high frequency small data downloads
request = new StringRequest (com.android.volley.Request.Method.GET, URL, monitor the results, error monitor) StringRequest;
RequestQueue requestQueue = Volley.newRequestQueue (context); // added to the queue
requestQueue.add (request); // add to the request queue
requestQueue.start (); // start queue
post
override the getParams ()
to create the HashMap
PUT parameter and corresponding value
Download image
ImageRequest imageRequest = new ImageRequest(网址, 结果监听,宽, 高, Bitmap.Config.RGB_565, 错误监听);
rely
implementation ‘com.squareup.okhttp3:okhttp:3.12.1’
implementation ‘eu.the4thfloor.volley:com.android.volley:2015.05.28’
okhttpget synchronization method
public void Okthhp_Get(String url){
Request build = new Request.Builder().url(url).get().build();
OkHttpClient client = new OkHttpClient();
try {
Response response = client.newCall(build).execute();
if (response.isSuccessful()){
String json = response.body().string();
Log.e("####body",json);
}else{
}
} catch (IOException e) {
e.printStackTrace();
}
}
okhttppost synchronization method
public void okHttp_post(String url){
FormBody builder = new FormBody.Builder()
.add("phone","15148818964")
.add("passwd","123")
.build();
Request request = new Request.Builder().url(url).post(builder).build();
OkHttpClient client = new OkHttpClient();
Call call = client.newCall(request);
try {
Response execute = call.execute();
if(execute.isSuccessful()) {
String string = execute.body().string();
Log.e("####post",string);
}
} catch (IOException e) {
e.printStackTrace();
}
}
okhttpget asynchronous method
public void asyncGet(String url){
Request request = new Request.Builder().url(url).build();//获得请求对象
OkHttpClient client = new OkHttpClient();//客户端对象
Call call = client.newCall(request);
call.enqueue(new okhttp3.Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e("###","失败");
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
Log.e("####sy",string);
}
});
}
okhttppost asynchronous method
public void asyncPost(String url){
OkHttpClient client = new OkHttpClient();
FormBody body = new FormBody.Builder()
.add("phone","15148818964")
.add("passwd","123")
.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("###ybpost",string);
}
});
}
volleyget method
public void volleyget(String url,Context context){
RequestQueue requestQueue = Volley.newRequestQueue(context);
StringRequest request = new StringRequest(com.android.volley.Request.Method.GET, url,listener,errorListener);
requestQueue.add(request);//将请求添加到队列
requestQueue.start();
}
com.android.volley.Response.Listener<String> listener = new com.android.volley.Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e("#####rese",response);
}
};
com.android.volley.Response.ErrorListener errorListener =new com.android.volley.Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
};
volleypost method
public void volleyPost(String url,Context context){
RequestQueue requestQueue = Volley.newRequestQueue(context);
StringRequest request = new StringRequest(com.android.volley.Request.Method.POST, url,listener,errorListener){
@Override
protected Map<String, String> getParams() {
HashMap<String,String> map = new HashMap<>();
map.put("phone","15148819630");
map.put("passwd","123");
return map;
}
};
requestQueue.add(request);
requestQueue.start();
}
com.android.volley.Response.Listener<String> listener = new com.android.volley.Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e("#####rese",response);
}
};
com.android.volley.Response.ErrorListener errorListener =new com.android.volley.Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
};
volley pictures
public void volleyImag(String url,Context context){
RequestQueue requestQueue = Volley.newRequestQueue(context);
ImageRequest imageRequest = new ImageRequest(url, listener1, 100, 100, Bitmap.Config.RGB_565, errorListener);
requestQueue.add(imageRequest);
requestQueue.start();
}
com.android.volley.Response.Listener<Bitmap> listener1= new com.android.volley.Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap response) {
MainActivity.imageView.setImageBitmap(response);
}
};