In the daily process of writing Android software, can not avoid the use of Web requests, and the use of single-machine Android, so this is to send a Web request via Android to request pictures of the article.
Let me summarize some of the steps of the network request:
1. Convert the image address you want to request into a URL class
2, through the OpenConnection to establish the connection
3, in the programming time I avoid let the user death, set the network connection timeout time, reads the time
4. Set the type of request network (GET or POST)
5. Submit Network Request
6, accept the return code, through the return code to determine whether the network request success
Note: It is important here that any network request must be coupled with network permissions <uses-permission android:name= "Android.permission.INTERNET"/>
These are the most basic steps in the network request, and the rest of the code needs to be supplemented in the code we write to request the Web image.
Our code is step-by-step:
Convert the image address you want to request into a URL class The address of this picture is a bit long
URL url = new URL ("Https://timgsa.baidu.com/timg?") image&quality=80&size=b9999_10000&sec=1524759026299&di=47e39d74fe559c7ab60318fb308ab8fa& Imgtype=0&src=http%3a%2f%2fimgstore.cdn.sogou.com%2fapp%2fa%2f100540002%2f691448.jpg ");
To establish a connection through openconnection here HttpURLConnection is the return type of OpenConnection
HttpURLConnection connection = (httpurlconnection) url.openconnection ();
When programming I avoid letting the user death, set the network connection timeout time, read the time
Setting the connection timeout to 5 seconds
Connection.setconnecttimeout (the);
//Set read timeout to 5 seconds
Connection.setreadtimeout (the);
//Set the type of request network (GET or post)
Connection.setrequestmethod ("GET") ;
Submit a network request
Connection.connect ();
Accept return code The return code received here is of type int
int code = Connection.getresponsecode ();
Determine if the network is successfully requested by a return code
if (code = = 200) {
//Gets the data stream returned by the connection
InputStream is = Connection.getinputstream ();
//Because a picture is returned, it is obtained by means of a bitmap
Bitmap BM = Bitmapfactory.decodestream (is);
//Set message
Message message = new Message ();
message. what = 0;
message. obj = BM;
//Send messages via handler
handler.sendmessage (message);
}
The time-consuming operation in Android must be performed in a child thread, so here we need a handler to set ImageView's picture in the main thread
Handler Handler =new Handler () {
Public void Handlemessage (Message msg) {
switch (msg. What) {
If Msg.what is 0 then save the picture to display in ImageView
Case 0:
We put the received image in the message when we sent the request, so we need to determine if it is the corresponding value.
To determine whether or not to accept this image
Bitmap BM = (Bitmap) msg.obj;
Set up ImageView pictures
Imageview.setimagebitmap (BM);
Break ;
}
};
};
If our button executes the request picture, it must be executed in the sub-thread, otherwise the error is reported;
Btnbutton, this is a button.
Btnbutton.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View arg0) {
Creating Child Threads
New Thread () {
@Override
public void Run () {
TODO auto-generated Method Stub
Get Pictures
Getpic ();
}
}.start ();
}
});
Android acquires image resources by requesting a network