OKhttp基本使用

目錄

????1.Get請求(同步和異步)

????2.POST請求表單(key-value)

????3.POST請求提交(JSON/String/文件等)

????4.文件下載

????5.請求超時設(shè)置

說明:基于okhttp3.8.0


1.Get請求(同步)


2、Get請求(異步)


3、post請求表單


4、post提交json數(shù)據(jù)


5、post提交file文件


6、文件下載


7、超時設(shè)置


8、源碼

public class MainActivityextends AppCompatActivity {

????????private static OkHttpClientclient =new OkHttpClient();

????????private Stringurl ="";

????????private Requestrequest;

????????/*

? ? ????* 超時設(shè)置*?

????????*/

? ? ????static {

????????????????client.newBuilder().connectTimeout(10, TimeUnit.SECONDS);

????????????????client.newBuilder().readTimeout(10,TimeUnit.SECONDS);

????????????????client.newBuilder().writeTimeout(10,TimeUnit.SECONDS);

????????}

????????private TextViewtext;

????????@Override

????????protected void onCreate(Bundle savedInstanceState) {

????????????????super.onCreate(savedInstanceState);

????????????????setContentView(R.layout.activity_main);

????????????????text = ((TextView) findViewById(R.id.textView));

????????}

????????/*

? ????? * 同步Get

????????* */

????????public void syncGet(View view) {

????????????????new Thread(new Runnable() {

????????????????????????@Override

? ? ? ? ? ? ????????????public void run() {

????????????????????????????????try {

????????????????????????????????????????request =new Request.Builder().url(url).build();

????????????????????????????????????????Response response =client.newCall(request).execute();

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? final String message = response.body().string();

????????????????????????????????????????runOnUiThread(new Runnable() {

????????????????????????????????????????????????@Override

? ? ? ? ? ? ? ? ? ? ? ? ????????????????????????public void run() {

????????????????????????????????????????????????????????text.setText(message);

????????????????????????????????????????????????}

????????????????????????????????????????});

????????????????????????????????}catch (IOException e) {

????????????????????????????????????????e.printStackTrace();

????????????????????????????????}

????????????????????????}

????????????????}).start();

????????}

????????/*

? ? ????* 異步Get

????????* */

????????public void asyncGet(View view) {

????????????????new Thread(new Runnable() {

????????????????????????@Override

? ? ? ? ? ? ????????????public void run() {

????????????????????????????????request =new Request.Builder().url(url).build();

????????????????????????????????client.newCall(request).enqueue(new Callback() {

????????????????????????????????????????@Override

? ? ? ? ? ? ? ? ? ? ????????????????????public void onFailure(Call call, IOException e) {

????????????????????????????????????????}

????????????????????????????????????????@Override

? ? ? ? ? ? ? ? ? ? ????????????????????public void onResponse(Call call,final Response response)throws IOException {

????????????????????????????????????????????????if (response.isSuccessful()) {

????????????????????????????????????????????????????????runOnUiThread(new Runnable() {

????????????????????????????????????????????????????????????????@Override

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ????????????????????????????????public void run() {

????????????????????????????????????????????????????????????????????????String string =null;

????????????????????????????????????????????????????????????????????????try {

????????????????????????????????????????????????????????????????????????????????string =response.body().string();

????????????????????????????????????????????????????????????????????????????????text.setText(string);

????????????????????????????????????????????????????????????????????????}catch (IOException e) {

????????????????????????????????????????????????????????????????????????????????e.printStackTrace();

????????????????????????????????????????????????????????????????????????}

????????????????????????????????????????????????????????????????????????Log.d("MainActivity", string);

????????????????????????????????????????????????????????????????}

????????????????????????????????????????????????????????});

????????????????????????????????????????????????}

????????????????????????????????????????}

????????????????????????????????});

????????????????????????}

????????????????}).start();

????????}

????????/*

? ? ????* post表單提交* */

????????public void postForm(View view) {

????????????????//表單請求體

? ? ? ? ????????FormBody body =new FormBody.Builder().add("type","1").build();

????????????????request =new Request.Builder().url(url).post(body).build();

????????????????new Thread(new Runnable() {

????????????????????????@Override

? ? ? ? ? ????????????? public void run() {

????????????????????????????????client.newCall(request).enqueue(new Callback() {

????????????????????????????????????????@Override

? ? ? ? ? ? ? ? ? ????????????????????? public void onFailure(Call call, IOException e) {

????????????????????????????????????????}

????????????????????????????????????????@Override

? ? ? ? ? ? ? ? ? ? ????????????????????public void onResponse(Call call, Response response)throws IOException {

????????????????????????????????????????????????if (response.isSuccessful()) {

????????????????????????????????????????????????????????text.setText("提交成功");

????????????????????????????????????????????????}

????????????????????????????????????????}

????????????????????????????????});

????????????????????????}

????????????????}).start();

????????}

????????/*

? ? ????* 下載* */

????????public void download(View view) {

????????????????request =new Request.Builder().url(url).build();

????????????????new Thread(new Runnable() {

????????????????????????@Override

? ? ? ? ? ? ????????????public void run() {

????????????????????????????????client.newCall(request).enqueue(new Callback() {

????????????????????????????????????????@Override

? ? ? ? ? ? ? ? ? ????????????????????? public void onFailure(Call call, IOException e) {

????????????????????????????????????????}

????????????????????????????????????????@Override

? ? ? ? ? ? ? ? ? ? ????????????????????public void onResponse(Call call, Response response)throws IOException {

????????????????????????????????????????????????if (response.isSuccessful()) {

????????????????????????????????????????????????????????//轉(zhuǎn)換字節(jié)流

? ? ? ? ? ? ? ? ? ? ? ? ? ? ????????????????????????????InputStream inputStream = response.body().byteStream();

????????????????????????????????????????????????????????//文件輸出流

? ? ? ? ? ? ? ? ? ? ? ? ? ? ????????????????????????????FileOutputStream outputStream =new FileOutputStream(new File("sdcard/xxx.txt"));

????????????????????????????????????????????????????????//字節(jié)數(shù)組

? ? ? ? ? ? ? ? ? ? ? ? ? ? ????????????????????????????byte[] bytes =new byte[2048];

????????????????????????????????????????????????????????int len =0;

????????????????????????????????????????????????????????//循環(huán)讀

? ? ? ? ? ? ? ? ? ? ? ? ? ? ????????????????????????????while ((len = inputStream.read(bytes)) != -1){

????????????????????????????????????????????????????????????????//寫文件

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ????????????????????????????????outputStream.write(bytes,0,len);

????????????????????????????????????????????????????????}

????????????????????????????????????????????????????????//關(guān)閉流

? ? ? ? ? ? ? ? ? ? ? ? ? ????????????????????????????? outputStream.flush();

????????????????????????????????????????????????}

????????????????????????????????????????}

????????????????????????????????});

????????????????????????}

????????????????}).start();

????????}

????????/*

? ? ????* 提交json

????????* */

????????public void postJson(View view) {

????????????????//設(shè)置類型

? ? ? ? ????????MediaType jsonType = MediaType.parse("application/json; charset=utf-8");

????????????????String json ="{\\\"username\\\":\\\"lisi\\\",\\\"nickname\\\":\\\"李四\\\"}";

????????????????RequestBody body = RequestBody.create(jsonType,json);

????????????????//創(chuàng)建request

? ? ? ? ????????request =new Request.Builder().url(url).post(body).build();

????????????????new Thread(new Runnable() {

????????????????????????@Override

? ? ? ? ? ? ????????????public void run() {

????????????????????????????????client.newCall(request).enqueue(new Callback() {

????????????????????????????????????????@Override

? ? ? ? ? ? ? ? ? ? ????????????????????public void onFailure(Call call, IOException e) {

????????????????????????????????????????}

????????????????????????????????????????@Override

? ? ? ? ? ? ? ? ? ? ????????????????????public void onResponse(Call call, Response response)throws IOException {

????????????????????????????????????????}

????????????????????????????????});

????????????????????????}

????????????????}).start();

????????}

????????/*

? ? ????* 提交文件* */

? ? ????public void postFile(View view) {

????????????????//設(shè)置類型

? ? ? ? ????????MediaType fileType = MediaType.parse("File/*");

????????????????//文件

? ? ? ? ????????File file =new File("path");

????????????????RequestBody body = RequestBody.create(fileType,file);

????????????????//創(chuàng)建request

? ? ? ? ????????request =new Request.Builder().url(url).post(body).build();

????????????????new Thread(new Runnable() {

????????????????????????@Override

? ? ? ? ? ? ????????????public void run() {

????????????????????????????????client.newCall(request).enqueue(new Callback() {

????????????????????????????????????????@Override

? ? ? ? ? ? ? ? ? ? ????????????????????public void onFailure(Call call, IOException e) {

????????????????????????????????????????}

????????????????????????????????????????@Override

? ? ? ? ? ? ? ? ? ? ????????????????????public void onResponse(Call call, Response response)throws IOException {

????????????????????????????????????????}

????????????????????????????????});

????????????????????????}

????????????????}).start();

????????}

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容