Android网络框架OK3,Android网络框架---OkHttp3
1 public class MainActivity extendsAppCompatActivity {2
3 @Override4 protected voidonCreate(Bundle savedInstanceState) {5 super.onCreate(savedInstanceState);6 setContentView(R.layout.activity_main);7 }8
9 //基本請求默認GET請求
10 public voidnet1(View v){11 //可以通過構造方法來使用。使用Builder模式可以設置一些屬性12 //OkHttpClient client = new OkHttpClient();13 //緩存的請求的數據,json字符串,14 //Cache questCache = new Cache(new File("cache"),1024);
15 OkHttpClient client = newOkHttpClient.Builder()16 //讀取超時5s
17 .readTimeout(5000, TimeUnit.MILLISECONDS)18 //自帶緩存,緩存可做可不做,做了就是讓刷新的時候方便些,不會重復去請求同一個url多次19 //.cache(questCache)
20 .build();21
22 //默認GET請求
23 Request request = newRequest.Builder()24 .url("https://www.baidu.com")25 .build();26
27 //call就是我們可以執行的請求類
28 Call call =client.newCall(request);29
30
31 //try {32 // //同步方法,程序運行到這里會等待,所以這里我們不用這個方法33 //Response execute = call.execute();34 //} catch (IOException e) {35 //e.printStackTrace();36 //}37
38 //異步請求
39 call.enqueue(newCallback() {40 @Override41 public voidonFailure(Call call, IOException e) {42 Log.e("TAG","請求失敗");43 }44
45 @Override46 public void onResponse(Call call, Response response) throwsIOException {47 //子線程
48 Log.e("TAG",Thread.currentThread().getName()+"---結果:"+response.body().string());49 //修改UI使用 runOnUiThread();或者使用Handler發送更新UI消息給主線程
50 }51 });52
53 //取消54 //call.cancel();
55 }56
57 //POST請求
58 public voidnet2(View v){59 OkHttpClient client = newOkHttpClient.Builder()60 .readTimeout(5000,TimeUnit.MILLISECONDS)61 .build();62
63 //創建Form表單對象,可以add多個鍵值對
64 RequestBody body = newFormBody.Builder()65 .add("showapi_appid","27306")66 .add("showapi_sign","150e9206e7f542bab4affe49d73cb920")67 .build();68
69 Request request = new Request.Builder().url("http://route.showapi.com/578-6")70 .post(body)//添加參數
71 .build();72
73 Call call =client.newCall(request);74 call.enqueue(newCallback() {75 @Override76 public voidonFailure(Call call, IOException e) {77 Log.e("TAG","請求失敗");78 }79
80 @Override81 public void onResponse(Call call, Response response) throwsIOException {82 Log.e("TAG","---->"+response.toString());83 //子線程
84 }85 });86
87 }88
89 //文件上傳
90 public voidnet3(View v){91 OkHttpClient client = newOkHttpClient.Builder()92 .readTimeout(5000,TimeUnit.MILLISECONDS)93 .build();94
95 //添加多個文件
96 RequestBody fileBody = MultipartBody.create(MediaType.parse("application/octet-stream"),new File("上傳的文件地址"));97
98
99 RequestBody body = newMultipartBody.Builder()100 //設置為表單類型
101 .setType(MultipartBody.FORM)102 //添加表單鍵值
103 .addFormDataPart("image頭像字段名稱","name文件名稱",fileBody)104 .build();105
106 Request request = newRequest.Builder()107 .url("http://192.168.18.250")108 .post(body)109 .build();110
111 Call call =client.newCall(request);112 call.enqueue(newCallback() {113 @Override114 public voidonFailure(Call call, IOException e) {115
116 }117
118 @Override119 public void onResponse(Call call, Response response) throwsIOException {120 //子線程
121 }122 });123 }124 }
總結
以上是生活随笔為你收集整理的Android网络框架OK3,Android网络框架---OkHttp3的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mate40pro什么时候用鸿蒙,mat
- 下一篇: android读取外部图片,Androi