前言
過(guò)了一個(gè)漫長(zhǎng)的中秋+國(guó)慶假期,大家伙的鴻蒙內(nèi)功修煉的怎么樣了?難道像小蒙一樣,都在吃吃喝喝中度過(guò)么,哎,罪過(guò)罪過(guò),對(duì)不起那些雞鴨魚(yú)肉啊,趕緊回來(lái)寫(xiě)篇文章收收心,讓我們一起看看,在鴻蒙中如何發(fā)送網(wǎng)絡(luò)請(qǐng)求吧。
本文會(huì)從Java原生訪問(wèn)入手,進(jìn)而再使用Retrofit訪問(wèn)網(wǎng)絡(luò),可以滿足絕大部分開(kāi)發(fā)者對(duì)于鴻蒙網(wǎng)絡(luò)訪問(wèn)方面的代碼需求,開(kāi)始之前需要先做一下基礎(chǔ)配置。
鴻蒙系統(tǒng)網(wǎng)絡(luò)訪問(wèn)基礎(chǔ)配置
1、跟Android類(lèi)似,要訪問(wèn)網(wǎng)絡(luò),我們首先要配置網(wǎng)絡(luò)訪問(wèn)權(quán)限,在config.json的"module"節(jié)點(diǎn)最后,添加上網(wǎng)絡(luò)權(quán)限代碼
"reqPermissions": [ { "reason": "", "name": "ohos.permission.INTERNET" } ]
2、配置網(wǎng)絡(luò)明文訪問(wèn)白名單
"deviceConfig": { "default": { "network": { "usesCleartext": true, "securityConfig": { "domainSettings": { "cleartextPermitted": true, "domains": [ { "subDomains": true, "name": "www.baidu.com" } ] } } } } }
其中的name即為可以直接http訪問(wèn)的域名,如果全是https鏈接則可以做該不配置,切記域名是不帶http://的,切記域名是不帶http://的,切記域名是不帶http://的,重要的事說(shuō)三遍。
Java原生訪問(wèn)網(wǎng)絡(luò)
由于鴻蒙系統(tǒng)支持Java開(kāi)發(fā),所以我們可以直接使用Java原生的Api來(lái)進(jìn)行網(wǎng)絡(luò)訪問(wèn) 該方式使用了java的url.openConnection() Api來(lái)獲取網(wǎng)絡(luò)數(shù)據(jù)
HttpDemo.java
package com.example.demo.classone; import javax.net.ssl.*; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.security.SecureRandom; public class HttpDemo { /** *訪問(wèn)url,獲取內(nèi)容 * @param urlStr * @return */ public static String httpGet(String urlStr){ StringBuilder sb = new StringBuilder(); try{ //添加https信任 SSLContext sslcontext = SSLContext.getInstance("SSL");//第一個(gè)參數(shù)為協(xié)議,第二個(gè)參數(shù)為提供者(可以缺省) TrustManager[] tm = {new HttpX509TrustManager()}; sslcontext.init(null, tm, new SecureRandom()); HostnameVerifier ignoreHostnameVerifier = new HostnameVerifier() { public boolean verify(String s, SSLSession sslsession) { System.out.println("WARNING: Hostname is not matched for cert."); return true; } }; HttpsURLConnection.setDefaultHostnameVerifier(ignoreHostnameVerifier); HttpsURLConnection.setDefaultSSLSocketFactory(sslcontext.getSocketFactory()); URL url = new URL(urlStr); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setReadTimeout(10000); connection.setConnectTimeout(10000); connection.connect(); int code = connection.getResponseCode(); if (code == HttpURLConnection.HTTP_OK) { BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String temp; while ((temp = reader.readLine()) != null) { sb.append(temp); } reader.close(); } connection.disconnect(); } catch (Exception e) { e.printStackTrace(); return e.getMessage(); } return sb.toString(); } }
HttpX509TrustManager.java
package com.example.demo.classone; import javax.net.ssl.X509TrustManager; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; public class HttpX509TrustManager implements X509TrustManager { @Override public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public X509Certificate[] getAcceptedIssuers() { return null; } }
最后是測(cè)試是否能夠正確訪問(wèn)的代碼,注意網(wǎng)絡(luò)訪問(wèn)是耗時(shí)操作要放線程里面執(zhí)行
new Thread(new Runnable() { @Override public void run() { String result = HttpDemo.httpGet("http://www.baidu.com"); HiLog.warn(new HiLogLabel(HiLog.LOG_APP, 0, "===demo==="), "網(wǎng)頁(yè)返回結(jié)果:"+result); } }).start();
采用Retrofit訪問(wèn)網(wǎng)絡(luò)
在模塊的build.gradle里添加Retrofit庫(kù)的引用,我這邊采用的是retrofit2的2.5.0版本做示例
implementation 'com.squareup.retrofit2:retrofit:2.5.0' implementation 'com.squareup.retrofit2:converter-gson:2.5.0' implementation 'io.reactivex.rxjava3:rxjava:3.0.4'
新建ApiManager類(lèi)用來(lái)管理獲取OkHttpClient,SSLSocketClient用來(lái)提供https支持,ApiResponseConverterFactory是Retrofit的轉(zhuǎn)換器,將請(qǐng)求結(jié)果轉(zhuǎn)成String輸出
ApiManager.java
package com.example.demo.classone; import com.example.demo.DemoAbilityPackage; import ohos.app.Environment; import okhttp3.*; import retrofit2.Retrofit; import java.io.File; import java.util.concurrent.TimeUnit; /** * 提供獲取Retrofit對(duì)象的方法 */ public class ApiManager { private static final String BUSINESS_BASE_HTTP_URL = "http://www.baidu.com"; private static Retrofit instance; private static OkHttpClient mOkHttpClient; private ApiManager(){} public static Retrofit get(){ if (instance == null){ synchronized (ApiManager.class){ if (instance == null){ setClient(); instance = new Retrofit.Builder().baseUrl(BUSINESS_BASE_HTTP_URL). addConverterFactory(ApiResponseConverterFactory.create()).client(mOkHttpClient).build(); } } } return instance; } private static void setClient(){ if (mOkHttpClient != null){ return; } Cache cache = new Cache(new File(getRootPath(Environment.DIRECTORY_DOCUMENTS),"HttpCache"),1024*1024*100); OkHttpClient.Builder builder = new OkHttpClient.Builder() // .followRedirects(false)//關(guān)閉重定向 // .addInterceptor(new AppendUrlParamIntercepter()) .cache(cache) .retryOnConnectionFailure(false) .sslSocketFactory(SSLSocketClient.getSSLSocketFactory()) .hostnameVerifier(SSLSocketClient.getHostnameVerifier()) .readTimeout(8,TimeUnit.SECONDS) .writeTimeout(8,TimeUnit.SECONDS) .connectTimeout(8, TimeUnit.SECONDS); // .protocols(Collections.singletonList(Protocol.HTTP_1_1)); mOkHttpClient = builder.build(); mOkHttpClient.dispatcher().setMaxRequests(100); } private static String getRootPath(String dirs) { String path = DemoAbilityPackage.getInstance().getCacheDir() + "/" + dirs; File file = new File(path); if (!file.exists()) { file.mkdirs(); } return path; } }
SSLSocketClient.java
package com.example.demo.classone; import javax.net.ssl.*; import java.security.SecureRandom; import java.security.cert.X509Certificate; public class SSLSocketClient { //獲取這個(gè)SSLSocketFactory public static SSLSocketFactory getSSLSocketFactory() { try { SSLContext sslContext = SSLContext.getInstance("SSL"); sslContext.init(null, getTrustManager(), new SecureRandom()); return sslContext.getSocketFactory(); } catch (Exception e) { throw new RuntimeException(e); } } //獲取TrustManager private static TrustManager[] getTrustManager() { TrustManager[] trustAllCerts = new TrustManager[]{ new X509TrustManager() { @Override public void checkClientTrusted(X509Certificate[] chain, String authType) { } @Override public void checkServerTrusted(X509Certificate[] chain, String authType) { } @Override public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[]{}; } } }; return trustAllCerts; } //獲取HostnameVerifier public static HostnameVerifier getHostnameVerifier() { HostnameVerifier hostnameVerifier = new HostnameVerifier() { @Override public boolean verify(String s, SSLSession sslSession) { return true; } }; return hostnameVerifier; } }
ApiResponseConverterFactory.java
package com.example.demo.classone; import okhttp3.RequestBody; import okhttp3.ResponseBody; import retrofit2.Converter; import retrofit2.Retrofit; import java.io.IOException; import java.lang.annotation.Annotation; import java.lang.reflect.Type; /** * BaseResponse的轉(zhuǎn)換器 */ public class ApiResponseConverterFactory extends Converter.Factory { public static Converter.Factory create(){ return new ApiResponseConverterFactory(); } @Override public ConverterresponseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) { return new StringResponseBodyConverter(); } @Override public Converter, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) { return null; } class StringResponseBodyConverter implements Converter { @Override public String convert(ResponseBody value) throws IOException { String s = value.string(); return s; } } }
開(kāi)始使用Retrofit書(shū)寫(xiě)業(yè)務(wù)邏輯
BusinessApiManager.java
package com.example.demo.classone; /** * 服務(wù)端訪問(wèn)接口管理 */ public class BusinessApiManager { private static BusinessApiService instance; public static BusinessApiService get(){ if (instance == null){ synchronized (BusinessApiManager.class){ if (instance == null){ instance = ApiManager.get().create(BusinessApiService.class); } } } return instance; } }
BusinessApiService.java
package com.example.demo.classone; import retrofit2.Call; import retrofit2.http.*; /** * 服務(wù)端訪問(wèn)接口 */ public interface BusinessApiService { /** * 獲取網(wǎng)頁(yè)信息 * @param url * @return */ @GET() CallgetHtmlContent(@Url String url); }
測(cè)試Retrofit是否能夠正常使用
BusinessApiManager.get().getHtmlContent("https://www.baidu.com").enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { if (!response.isSuccessful() || response.body() == null){ onFailure(null,null); return; } String result = response.body(); HiLog.warn(new HiLogLabel(HiLog.LOG_APP, 0, "===demo==="), "網(wǎng)頁(yè)返回結(jié)果:"+result); } @Override public void onFailure(Call call, Throwable throwable) { HiLog.warn(new HiLogLabel(HiLog.LOG_APP, 0, "===demo==="), "網(wǎng)頁(yè)訪問(wèn)異常"); } });
總結(jié)
鴻蒙是基于Java開(kāi)發(fā)的,所有Java原生api都是可以直接在鴻蒙系統(tǒng)上使用的,另外只要和java相關(guān)的庫(kù)都是可以直接引用的,例如在引用retrofit的時(shí)候也帶入了RxJava。 更多retrofit的使用方式,可以參考retrofit在android系統(tǒng)中的實(shí)現(xiàn),鴻蒙系統(tǒng)基本兼容。
編輯:hfy
-
JAVA
+關(guān)注
關(guān)注
19文章
2952瀏覽量
104489 -
鴻蒙系統(tǒng)
+關(guān)注
關(guān)注
183文章
2634瀏覽量
66157
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論