[Android] network picture viewer

Interface layout, linerlayout, linear layout, ImageView control, EditText control, hint property prompt information, button control.

Android:layout_ Weight = "attribute, weight. It only represents the weight when the width and height of the control are 0. Otherwise, it represents the priority of rendering. The higher the value, the lower the priority. The default is 0. Render other controls first

Singleline property single line

Business logic, get EditText value and put it in ImageView, which is essentially HTTP get request

Get EditText object through findviewbyid()

Get the value by calling gettext() method of EditText object, tostring() method and trim() method

Call the isempty () method of textutils class to judge whether it is empty. If it is empty, just toast

Send get request

Get the URL object through new URL (path). Parameter: path is the path of string. Here you need to catch exceptions

Get the httpurlconnection object, return the urlconnection object by calling the openconnection () method of the URL object, and then force the type conversion

Call the setrequestmethod (method) method of the httpurlconnection object to set the request method. The method is get

Call setconnecttimeout (millis) of the httpurlconnection object to set the timeout. Millis is milliseconds

Call setrequestproperty (field, newvalue) method of httpurlconnection object to set request parameters, such as request header, user agent, etc

Call the getresponsecode () method of the httpurlconnection object to get the response code of the server, OK

Call getinputstream() method of httpurlconnection object to get input stream and return InputStream object

Call the decodestream (is) method of the bitmapfactory class to get the bitmap object. The parameter is the InputStream object

Call the setimagebitmap (bitmap) method of the ImageView object to set the picture. Parameter: bitmap object

All users accessing the network need to add permission android.permission.internet

When you deploy directly, an error networkonmainthreadexception will be reported. Network operations on the main thread are not allowed. The following code is only valid in lower versions

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>