First, download the jar package
Https://github.com/square/okhttp
If you use Android Studio you just need to join the dependency compile ' com.squareup.okhttp3:okhttp:3.2.0 '
Second, download a picture and show
It's a hanlder way to use.
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 = "Yun_qi_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 (msg) {switch (msg.what) {C ASE 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 picture/public void Getpic (View v) {new Thread () {@Override public void run () {//Obtain Okhttp object GET request
, try {okhttpclient client = new Okhttpclient ();
Get Request Object requests = new Request.builder (). URL (URL). build ();
Get response Body Responsebody = Client.newcall (Request). Execute ().
Get stream InputStream in = Body.bytestream ();
Convert to Bitmap Bitmap Bitmap = Bitmapfactory.decodestream (in);
Use Hanlder to send messages message msg = Message.obtain ();
Msg.what = SUCCESS;
Msg.obj = bitmap;
Handler.sendmessage (msg);
catch (IOException e) {e.printstacktrace ();
Failure message msg = Message.obtain ();
Msg.what = ERROR;
Handler.sendmessage (msg);
}}.start ();
}
}
You can also write a network request 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 requests Request
= new Request.builder ()
. URL (URL). build ();
Gets the row response
InputStream in = Client.newcall (Request). Execute (). Body (). ByteStream ();
return in;
}
/**
* Return string *
@param url
* @return return string
* @throws ioexception/public
string getString (String URL) throws IOException {
//Set requests Request
= new Request.builder ()
. URL (URL). build ();
Get row response
Response Response = client.newcall (Request). Execute ();
Return Response.body (). String ();
}
}
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.