Android learning ---- Using okHttp (get method) --- download images,

Source: Internet
Author: User

Android learning ---- Using okHttp (get method) --- download images,
First download the Jar package

Https://github.com/square/okhttp

 

If you use android studio, you only need to add dependencies.

compile 'com.squareup.okhttp3:okhttp:3.2.0'

 

2. Download and display an image

The hanlder method is used.

Package com. liunan. okhttpdemo2; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. OS. handler; import android. OS. message; import android. support. v7.app. appCompatActivity; import android. OS. bundle; import android. view. view; import android. widget. imageView; import android. widget. toast; import java. io. IOException; import java. io. inputStream; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; import okhttp3.ResponseBody; public class MainActivity extends AppCompatActivity {private static final int ERROR = 1; private static final int SUCCESS = 2; private String url = "http: // 192.168.1.102: 8080/img/a.jpg"; private ImageView mIv; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); initView ();} private Handler handler = new Handler () {@ Override public void handleMessage (Message msg) {switch (msg. what) {case SUCCESS: mIv. setImageBitmap (Bitmap) msg. obj); break; case ERROR: Toast. makeText (MainActivity. this, "request timeout", Toast. LENGTH_SHORT ). show (); break ;}};/***** initialization component */private void initView () {mIv = (ImageView) findViewById (R. id. main_iv);}/*** click to get the image */public void getPic (View v) {new Thread () {@ Override public void run () {// get the okHttp object get Request, try {OkHttpClient client = new OkHttpClient (); // get the request Object Request = new Request. builder (). url (url ). build (); // obtain the response body ResponseBody body = client.newcall(request0000.exe cute (). body (); // get the stream InputStream in = body. byteStream (); // convert to bitmap Bitmap bitmap = BitmapFactory. decodeStream (in); // use Hanlder to send the Message msg = Message. obtain (); msg. what = SUCCESS; msg. obj = bitmap; handler. sendMessage (msg);} catch (IOException e) {e. printStackTrace (); // failed Message msg = Message. obtain (); msg. what = ERROR; handler. sendMessage (msg );}}}. start ();}}

You can also write network requests as a tool class,

Package com. liunan. okhttpdemo2; import java. io. IOException; import java. io. inputStream; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response;/*** Created by Liu Nan on 2016-03-27. */public class OkHttpUtils {OkHttpClient client = new OkHttpClient ();/*** get stream * @ param url request address * @ return input stream */public InputStream getInpuStream (String url) throws IOException {// Set Request request = new Request. builder (). url (url ). build (); // get the row response InputStream in = client.newcall(requestcmd.exe cute (). body (). byteStream (); return in;}/*** returns the String * @ param url * @ return returns the String * @ throws IOException */public String getString (String url) throws IOException {// Set Request request = new Request. builder (). url (url ). build (); // get the row Response response = client.newcall(requestcmd.exe cute (); return Response. body (). string ();}}

 

Get Method

 

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.