Android Network request library volley method details, androidvolley
Using volley for network requests: you must first import the volley package to androidstudio.
Project Structrue under File, click the plus sign to export the package
Volley network request steps:
1. CREATE request queue RequestQueue queue = Volley. newRequestQueue (this );
2. Create request objects (3 types)
StringRequest request = new StringRequest ("request Method", "requested network address", "Successful network callback", "failed network callback ");
ImageRequest request = new ImageRequest ("image path", "successful callback function", "image width", "Image Height", "Image Color Attribute ", "failed network callback ");
Jsonrequest request = new Jsonrequest ();
3. Put the request object in the request queue. add (request );
Logout request: override onstop Method
@ Override
Protected void onStop (){
Super. onStop ();
/* Cancel all requests in the current request queue */
Queue. cancelAll (this );
/* Cancel the request Whose tag is get in the current request queue */
Queue. cancelAll ("get ");
/* Cancel the request Whose tag is post in the current request queue */
Queue. cancelAll ("post ");
}
Set the priority of the current request: override the getPriority Method
@Override
public Priority getPriority() {
return Priority.LOW;
}
Set Request Header: override the GetHeader Method
@ Override
Public Map <String, String> getHeaders () throws AuthFailureError {
Map <String, String> map = new HashMap <String, String> ();
Map. put ("apikey", "fc642e216cd19906f642ee930ce28174 ");
Return map;
}
Passing parameters: override the GetParams Method
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> map = new HashMap<String, String>();
map.put("num","10");
map.put("page","1");
map.put("word","%E6%9E%97%E4%B8%B9");
return map;
}
Code Section:
Xml file:
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: paddingBottom = "@ dimen/activity_vertical_margin" android: paddingLeft = "@ dimen/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/plugin" android: orientation = "vertical" tools: context = "com. jredu. helloworld. activity. volleyActivity "> <WebView android: id =" @ + id/volleyWebView "android: layout_width =" match_parent "android: layout_height =" 300dp "> </WebView> <ImageView android: id = "@ + id/img" android: layout_width = "match_parent" android: layout_height = "wrap_content"/> <Button android: id = "@ + id/volleyButton" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: layout_marginTop = "5dp" android: textAllCaps = "false" android: text = "Volley"/> <Button android: id = "@ + id/imgButton" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: layout_marginTop = "5dp" android: textAllCaps = "false" android: text = "Volley getting images"/> </LinearLayout>
Activity file:
Package com. jredu. helloworld. activity; import android. graphics. bitmap; import android. OS. bundle; import android. support. v7.app. appCompatActivity; import android. view. view; import android. webkit. webView; import android. widget. button; import android. widget. imageView; import com. android. volley. authFailureError; import com. android. volley. networkResponse; import com. android. volley. parseError; import com. android. vo Lley. request; import com. android. volley. requestQueue; import com. android. volley. response; import com. android. volley. volleyError; import com. android. volley. toolbox. httpHeaderParser; import com. android. volley. toolbox. imageRequest; import com. android. volley. toolbox. stringRequest; import com. android. volley. toolbox. volley; import com. jredu. helloworld. r; import java. io. unsupportedEncodingException; import java. ut Il. hashMap; import java. util. map; public class VolleyActivity extends AppCompatActivity {WebView webView; Button button; Button imgButton; ImageView img; RequestQueue queue = null; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_volley); queue = Volley. newRequestQueue (this); webView = (WebView) findViewById (R. id. vol LeyWebView); img = (ImageView) findViewById (R. id. img); button = (Button) findViewById (R. id. volleyButton); button. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {doStringVolley2 (); doStringVolley () ;}}); imgButton = (Button) findViewById (R. id. imgButton); imgButton. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {GetImg () ;}}) ;}/ * Get Method */public void doStringVolley () {/* CREATE request queue * // RequestQueue queue = Volley. newRequestQueue (this);/* Create a request object */StringRequest Request = new StringRequest (request. method. GET, "http://apis.baidu.com/txapi/tiyu/tiyu? Num = 10 & page = 1 & word = % E6 % 9E % 97% E4 % B8 % B9 ",/*" http://www.baidu.com ", */new Response. listener <String> () {@ Override public void onResponse (String response) {String s = response; webView. getSettings (). setdefatextextencodingname ("UTF-8"); webView. getSettings (). setJavaScriptEnabled (true); webView. loadDataWithBaseURL (null, s, "text/html", "UTF-8", null) ;}, new Response. errorListener () {@ Override public vo Id onErrorResponse (VolleyError error) {webView. loadDataWithBaseURL (null, "Network Connection Failed !!! "," Text/html "," UTF-8 ", null) ;}} {/* set the request header */@ Override public Map <String, String> getHeaders () throws AuthFailureError {Map <String, String> map = new HashMap <String, String> (); map. put ("apikey", "fc642e216cd19906f642ee930ce28174"); return map;}/* method for parsing network request results */@ Override protected Response <String> parseNetworkResponse) {try {String jsonObject = new String (res Ponse. data, "UTF-8"); return Response. success (jsonObject, HttpHeaderParser. parseCacheHeaders (response);} catch (UnsupportedEncodingException e) {return Response. error (new ParseError (e);} catch (Exception je) {return Response. error (new ParseError (je);}/* set the Priority of the current request */@ Override public Priority getPriority () {return Priority. LOW ;}}; request. setTag ("get");/* put the request object in the Request queue */queue. add (reque St);}/* post method */public void doStringVolley2 () {/* CREATE request queue * // RequestQueue queue = Volley. newRequestQueue (this);/* Create a request object */StringRequest Request = new StringRequest (request. method. POST, "http://www.baidu.com", new Response. listener <String> () {@ Override public void onResponse (String response) {String s = response; webView. getSettings (). setdefatextextencodingname ("UTF-8"); webView. getSettings (). SetJavaScriptEnabled (true); webView. loadDataWithBaseURL (null, s, "text/html", "UTF-8", null) ;}, new Response. errorListener () {@ Override public void onErrorResponse (VolleyError error) {webView. loadDataWithBaseURL (null, "Network Connection Failed !!! "," Text/html "," UTF-8 ", null) ;}} {/* rewrite the params method to write parameters */@ Override protected Map <String, String> getParams () throws AuthFailureError {Map <String, String> map = new HashMap <String, String> (); map. put ("num", "10"); map. put ("page", "1"); map. put ("word", "% E6 % 9E % 97% E4 % B8 % B9"); return map;}/* set the request object Priority */@ Override public Priority getPriority () {return Priority. HIGH ;}}; request. setTag ("post");/* put the request object in the Request queue */queue. add (request);}/* Get image */public void GetImg () {ImageRequest request = new ImageRequest ("https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png", new Response. listener <Bitmap> () {@ Override public void onResponse (Bitmap response) {img. setImageBitmap (response) ;}, 5000,500 0, Bitmap. config. ARGB_8888, new Response. errorListener () {@ Override public void onErrorResponse (VolleyError error) {img. setImageResource (R. drawable. first5) ;}}); queue. add (request);}/* rewrite the onStop method to cancel the request */@ Override protected void onStop () {super. onStop ();/* cancel all requests in the current request queue */queue. cancelAll (this);/* cancel the request whose current request queue tag is get */queue. cancelAll ("get");/* cancel the request whose current request queue tag is post */queue. cancelAll ("post ");}}