How Android uses the API

Source: Internet
Author: User

This article provides some experience sharing on how to use API documentation for Android development .

1, why need to master the use of the API.

Perhaps you need to complete a function many times when you are on the Internet Google , because it is likely to find useful code snippets, and even do not care about the implementation of the principle can be directly used. You may have encountered the results of the search is not very satisfactory, especially you need to complete the function is relatively new,demo relatively few.

This time to develop the best and fastest way is to use the good API, the use of good API is also a required program development.

2, the importance of the API.

In fact, many beginners did not check| ApiHabit, the reason is simple:ApiOnly a few of the class libraries and methods are introduced, and fewsample It's hard for developers to master. First you have to be clear that all the sample all from api Even if you refer to someone else's sampleapiapiapi use, not every time googledemo

3, how to use the API.

In fact, how to use the API can be determined according to personal habits. Today I share with you how I use the API. Specifically, a specific point of the example: using http to submit a form, print out the return value.

When you see this example, we should know the general network access steps (if you do not know that you have to cram knowledge), or at least you should know how to access the network in the Android system. Here we are using HttpClient, and below we formally start using the API (if I have not been exposed to this class before).

① found the HttpClient class on Doc .

This is a interface, so you have to find an implementation of it, and we see that it has three subcategories, and when you go back to see what these three subcategories do separately

Obviously, abstracthttpclient cannot be used directly, because it is an abstract class. But he has a sub-class defaulthttpclient, and this subclass has seen it before.

Look again androidhttpclient will find it API Level 8, and defaulthttpclient API level 1, ofcourse you can choose a class to use, Consider the problem of backwards compatibility. So choose defaulthttpclient.

② on doc to find defaultclient this class.

There is no public method for this class, so look at how it inherits.

A simple combination of the method, you can see that it needs to execute the Execute method to get the return value of the server, and then look at the parameters inside we notice httpurirequest this class, the return value is HttpResponse. So first look at Httpurirequest, and then see httpresponse.

③ on doc to find Httpurirequest this class.

It is an interface, but there are many implementations. If you know anything about HTTP, then you should know the general direction of their use from the name (if you don't know it then there's only one thing to know about the function of this class). When I saw each of the classes I found that HttpGet class can meet my needs, then its implementation will choose HttpGet.

④ on doc to find httpget this class.

Its constructor is simple, and there is no other way except for GetMethod. (So this class is very simple.) ) Look at its constructor:

As for which method you choose, look at your personal choice. I choose the constructor of the String parameter.

⑤ on doc to find HttpResponse this class.

⑥ is actually a process of repeatedly looking up parameters and return values in the API. The rest is omitted.

⑦ complete steps.

This concludes the search API and collates the results of the search.

HttpClient client = new Defaulthttpclient ();

Httpurirequest request = new HttpGet ("http://www.baidu.com");

HttpResponse response = Client.execute (request);

InputStream ins = response.getentity (). getcontent ();

Ins is the return value, returned in the form of a stream, and you need to convert it to Bytearrayinputstream to get the content inside (there is no reason for this API ).

4, summary.

In fact, the use of the API is relatively simple, is to keep searching, check parameters, check the return value. This is probably the case, the other general situation is not as I said this example is so complex, basically query one or two times to be done.

How Android uses the API

Related Article

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.