Android Studio OkHttpClient使用教程詳解
本次來(lái)記錄下OkHttpClient的使用,OkHttpClient是用來(lái)完成android 客戶(hù)端對(duì)服務(wù)端請(qǐng)求的工具。
首先記住,使用網(wǎng)絡(luò)的時(shí)候一定要加入權(quán)限,加入到AndroidMainfest.xml中
<uses-permission android:name='android.permission.INTERNET' />
在初次使用的時(shí)候會(huì)出現(xiàn)報(bào)錯(cuò)。cannot resolve symbol OkHttpClient
這里需要引入
implementation ’com.squareup.okhttp3:okhttp:3.0.1’然后刷新下項(xiàng)目就可以了。
代碼:
package com.example.administrator.testclient;import com.squareup.*;import java.io.IOException;import okhttp3.FormBody;import okhttp3.MediaType;import okhttp3.OkHttpClient;import okhttp3.Request;import okhttp3.RequestBody;import okhttp3.Response;public class BaseHttpClient { public static final MediaType MEDIA_TYPE_MARKDOWN = MediaType.parse('text/x-markdown; charset=utf-8'); // 01. 定義okhttp private final OkHttpClient client = new OkHttpClient(); public BaseHttpClient(){ //client.connectTimeoutMillis(); } /** * 發(fā)送一個(gè)表單請(qǐng)求 * @throws Exception */ public void SendForm() throws Exception { RequestBody formBody = new FormBody.Builder() .add('search', 'Jurassic Park') .build(); Request request = new Request.Builder() .url('https://en.wikipedia.org/w/index.php') .post(formBody) .build(); Response response = client.newCall(request).execute(); if (!response.isSuccessful()) throw new IOException('Unexpected code ' + response); System.out.println(response.body().string()); } /**POST 請(qǐng)求 * 發(fā)送一個(gè)string請(qǐng)求 * @throws Exception */ public void SendPostString() throws Exception { String postBody = '' + 'Releasesn' + '--------n' + 'n' + ' * _1.0_ May 6, 2013n' + ' * _1.1_ June 15, 2013n' + ' * _1.2_ August 11, 2013n'; Request request = new Request.Builder() .url('https://api.github.com/markdown/raw') .post(RequestBody.create(MEDIA_TYPE_MARKDOWN, postBody)) .build(); Response response = client.newCall(request).execute(); if (!response.isSuccessful()) throw new IOException('Unexpected code ' + response); System.out.println(response.body().string()); } /**POST 請(qǐng)求 * 發(fā)送一個(gè)From請(qǐng)求 * @throws Exception */ public void SendPostFrom() throws Exception { RequestBody body = new FormBody.Builder() .add('name', 'sy')//添加參數(shù)體 .add('age', '18') .build(); Request request = new Request.Builder() .post(body) //請(qǐng)求參數(shù) .url('http://123.207.70.54:8080/SpringMvc/hello') .build(); Response response = client.newCall(request).execute(); if (!response.isSuccessful()) throw new IOException('Unexpected code ' + response); } /**Get請(qǐng)求 * 發(fā)送一個(gè)From請(qǐng)求 * @throws Exception */ public void SendGetFrom() throws Exception { Request request = new Request.Builder() .get() //請(qǐng)求參數(shù) .url('http://123.207.70.54:8080/SpringMvc/hello') .build(); Response response = client.newCall(request).execute(); if (!response.isSuccessful()) throw new IOException('Unexpected code ' + response); }}
測(cè)試發(fā)現(xiàn),上面的用不了,下面放一個(gè)測(cè)試通過(guò)的方法:
public void getDatasyncFactory(){ new Thread(new Runnable() { @Override public void run() { try { OkHttpClient client = new OkHttpClient();//創(chuàng)建OkHttpClient對(duì)象 Request request = new Request.Builder() .url('http://123.207.70.54:8080/SpringMvc/hello')//請(qǐng)求接口。如果需要傳參拼接到接口后面。 .build();//創(chuàng)建Request 對(duì)象 Response response = null; response = client.newCall(request).execute();//得到Response 對(duì)象 if (response.isSuccessful()) {Log.d('kwwl','response.code()=='+response.code());Log.d('kwwl','response.message()=='+response.message());Log.d('kwwl','res=='+response.body());//此時(shí)的代碼執(zhí)行在子線(xiàn)程,修改UI的操作請(qǐng)使用handler跳轉(zhuǎn)到UI線(xiàn)程。 } } catch (Exception e) { e.printStackTrace(); } } }).start(); }
返回信息:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 詳細(xì)分析css float 屬性以及position:absolute 的區(qū)別2. ASP中格式化時(shí)間短日期補(bǔ)0變兩位長(zhǎng)日期的方法3. ASP基礎(chǔ)知識(shí)Command對(duì)象講解4. PHP設(shè)計(jì)模式中工廠(chǎng)模式深入詳解5. 得到XML文檔大小的方法6. jsp+servlet簡(jiǎn)單實(shí)現(xiàn)上傳文件功能(保存目錄改進(jìn))7. xpath簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理8. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)9. PHP循環(huán)與分支知識(shí)點(diǎn)梳理10. ASP實(shí)現(xiàn)加法驗(yàn)證碼
