【江鸟中原】鸿蒙----网络访问

qq6575a8a3674c8
发布于 2023-12-10 20:35
浏览
0收藏

前言
【江鸟中原】期末大作业,对鸿蒙的网络请求进行知识点讲解和对相关项目的复现。
鸿蒙系统网络访问基础配置
1、跟Android类似,要访问网络,我们首先要配置网络访问权限,在config.json的"module"节点最后,添加上网络权限代码
“reqPermissions”: [
{
“reason”: “”,
“name”: “ohos.permission.INTERNET”
}
]
2、配置网络明文访问白名单
“deviceConfig”: {
“default”: {
“network”: {
“usesCleartext”: true,
“securityConfig”: {
“domainSettings”: {
“cleartextPermitted”: true,
“domains”: [
{
“subDomains”: true,
“name”: “www.baidu.com
}
]
}
}
}
}
}
其中的name即为可以直接http访问的域名,如果全是https链接则可以做该不配置,切记域名是不带http://的,切记域名是不带http://的,切记域名是不带http://的,重要的事说三遍。

Java原生访问网络
由于鸿蒙系统支持Java开发,所以我们可以直接使用Java原生的Api来进行网络访问
该方式使用了java的url.openConnection() Api来获取网络数据

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 {
/**
*访问url,获取内容
* @param urlStr
* @return
*/
public static String httpGet(String urlStr){
StringBuilder sb = new StringBuilder();
try{
//添加https信任
SSLContext sslcontext = SSLContext.getInstance(“SSL”);//第一个参数为协议,第二个参数为提供者(可以缺省)
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;
}

}
最后是测试是否能够正确访问的代码,注意网络访问是耗时操作要放线程里面执行

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===”), “网页返回结果:”+result);
}
}).start();
采用Retrofit访问网络
在模块的build.gradle里添加Retrofit库的引用,我这边采用的是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类用来管理获取OkHttpClient,SSLSocketClient用来提供https支持,ApiResponseConverterFactory是Retrofit的转换器,将请求结果转成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对象的方法
    */
    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”),10241024100);
    OkHttpClient.Builder builder = new OkHttpClient.Builder()
    // .followRedirects(false)//关闭重定向
    // .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 {

//获取这个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的转换器
    */
    public class ApiResponseConverterFactory extends Converter.Factory {

    public static Converter.Factory create(){
    return new ApiResponseConverterFactory();
    }

    @Override
    public Converter<ResponseBody, String> responseBodyConverter(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<ResponseBody, String> {
    @Override
    public String convert(ResponseBody value) throws IOException {
    String s = value.string();
    return s;
    }
    }
    }
    开始使用Retrofit书写业务逻辑

BusinessApiManager.java
package com.example.demo.classone;

/**

  • 服务端访问接口管理
    */
    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.*;

/**

  • 服务端访问接口
    /
    public interface BusinessApiService {
    /
    *
    • 获取网页信息
    • @param url
    • @return
      */
      @GET()
      Call<String> getHtmlContent(@Url String url);
      }
      测试Retrofit是否能够正常使用

BusinessApiManager.get().getHtmlContent(“https://www.baidu.com”).enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
if (!response.isSuccessful() || response.body() == null){
onFailure(null,null);
return;
}
String result = response.body();
HiLog.warn(new HiLogLabel(HiLog.LOG_APP, 0, “===demo===”), “网页返回结果:”+result);
}

@Override
public void onFailure(Call<String> call, Throwable throwable) {
    HiLog.warn(new HiLogLabel(HiLog.LOG_APP, 0, "===demo==="), "网页访问异常");
}

});

分类
收藏
回复
举报
回复
    相关推荐