Android uses the network framework Volley recommended by Google to make the connection network easier. androidvolley

Source: Internet
Author: User

Android uses the network framework Volley recommended by Google to make the connection network easier. androidvolley
Hello everyone, with the advancement of technology and the development of technology, few applications are stand-alone. Most of them require online access servers. We used them in the past.
Httpclient and httpurlconnection feel very troublesome, but Google has launched Volley for developers, so my mom will never worry about my networking problems any more, volley makes it easier and faster to connect to the Internet, and Volley requests are executed asynchronously. without blocking the online requests of the main thread, it is nothing more than Get or Post. We seldom use the DELETE request method, after talking so much nonsense, let's see what Volley can do. Is it really amazing? Open sesame. Let's explore Volley together!I. functions provided by Volley

1. encapsulated asynchronous RESTful request API;

2. An elegant and robust Request queue;

3. A scalable architecture that enables developers to implement custom request and response processing mechanisms;

4. Use the external HTTP Client library;

5. Cache Policy;

6. Custom network image loading views (NetworkImageView, ImageLoader, etc );

Ii. Why do I use Asynchronous HTTP requests?Android requires asynchronous execution of HTTP requests. If an HTTP request is executed in the main thread, the android. OS. NetworkOnMainThreadException may be thrown. Blocking the main thread has some serious consequences. It hinders UI rendering and the user experience is Not smooth, which may lead to terrible ANR (Application Not Responding ). To avoid these traps, as a developer, we should always ensure that the HTTP request is in a different thread. 3. Get and Post methods of VolleyFirst, we need to use Volley to import Volley. jar to our project,
In this way, we can make it. Let's take a look at the Get and Post code, as shown below:
Package com. zqy. myvolley; import java. util. hashMap; import java. util. map; import com. android. volley. authFailureError; import com. android. volley. request; import com. android. volley. requestQueue; import com. android. volley. response; import com. android. volley. volleyError; import com. android. volley. request. method; import com. android. volley. response. errorListener; import com. android. volley. response. listener; import Com. android. volley. toolbox. stringRequest; import com. android. volley. toolbox. volley; import android. OS. bundle; import android. text. textUtils; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. toast; import android. app. activity; public class MainActivity extends Activity implements OnClickListener {private RequestQueue mQueue; String url = "h Ttp: // www.baidu.com "; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mQueue = Volley. newRequestQueue (this); <span style = "color: rgb (51, 51, 51); font-family:; font-size: 14px; line-height: 28px; "> Create a RequestQueue object. </Span> initView ();} private void initView () {final Button btnGet = (Button) findViewById (R. id. btn_get); final Button btnPost = (Button) findViewById (R. id. btn_post); btnGet. setOnClickListener (this); btnPost. setOnClickListener (this) ;}@ Overridepublic void onClick (View v) {switch (v. getId () {case R. id. btn_get: initGet (); break; case R. id. btn_post: initPost (); break; default: break;} private void initGet () {// Get request Met Hod is GETmQueue. add (new StringRequest (Method. GET, url, new Listener <String> () {@ Overridepublic void onResponse (String arg0) {Toast. makeText (MainActivity. this, "Get request succeeded", 2 ). show (); // succeeded. Write the code to process the content here }}, new ErrorListener () {@ Overridepublic void onErrorResponse (VolleyError arg0) {Toast. makeText (MainActivity. this, "Get request failed", 2 ). show (); // Failed});} private void initPost () {// Post request // This writes your own internal class PostResquest .... Change Method to POSTmQueue. add (new PostResquest (Method. POST, url, new Listener <String> () {@ Overridepublic void onResponse (String arg0) {Toast. makeText (MainActivity. this, "Post request successful", 2 ). show (); // succeeded. Write the code to process the content here }}, new ErrorListener () {@ Overridepublic void onErrorResponse (VolleyError arg0) {Toast. makeText (MainActivity. this, "Post request failed", 2 ). show (); // Failed});} // write an internal class. Put the class PostResquest extends StringRequest {public PostResquest (int method, string url, Listener <String> listener, ErrorListener errorListener) {super (method, url, listener, errorListener);} protected Map <String, String> getParams () throws AuthFailureError {Map <String, String> params = new HashMap <String, String> (); params. put ("Name", "small source"); // The params parameter. put ("Age", 22 + ""); // parameter return params ;}}}
This is the code for networking. Isn't it very simple? It's much simpler than httpclient and httpurlconnection, and it's still asynchronous execution, without Handler at all. It makes development easier, simpler, and more efficient. Let's look at the layout in XML. I put two buttons in it, one for the Get method and the other for the Post method. As follows:
<RelativeLayout 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" tools: context = ". mainActivity "> <Button android: id =" @ + id/btn_get "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: layout_alignParentTop =" true "android: layout_centerHorizontal = "true" android: layout_marginTop = "28dp" android: text = "Get request"/> <Button android: id = "@ + id/btn_post" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignLeft = "@ + id/btn_get" android: layout_below = "@ + id/btn_get" android: layout_marginTop = "47dp" android: text = "Post request"/> </RelativeLayout>
OK. I'm done. Isn't it easy? after reading the Code, are you planning to say hello to Volley with httpclient and httpurlconnection. In the end, we hope that the Android system will be better and better. Let's make sure that we can walk in the technology world, Yeah ~~~~ Let's take a look, with a Volley publicity picture! Go, Go home, it's raining out! By the way, I almost forgot that I never needed the Internet access permission. Yes!
<uses-permission android:name="android.permission.INTERNET" >


Failed, Post request failed, it was successful on the mobile phone, and my parameters were spelled out! It's just a false case. It's really helpful to me! It's really gone! It's raining!

Source code download






Android development framework

Google's gson package can be used for json parsing.
The Network request and cache mechanism (generally referred to as image cache) can be viewed by volley.
The database is relatively simple. The android app is already encapsulated. But you can check FinalDb in Afinal.
The UI is useless. It is written by yourself. You can master the view and viewgroup. Basically, you can master all the controls.

Why can't I synchronize my HTC G3 Telecom Android mobile phone with my google account? Is the error message "cannot be used with the server? Solution?

Delete data/com. android. providers. settings, restart and try again (some settings will be restored to the default value), and try again in the morning (the network is good at this time)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.