Skip to content

Commit b7765ec

Browse files
yu.zuoyu.zuo
yu.zuo
authored and
yu.zuo
committed
update android demo.
1 parent 4a7b234 commit b7765ec

File tree

5 files changed

+63
-9
lines changed

5 files changed

+63
-9
lines changed

example/android/test_cpphttplib_for_app_on_android/.idea/vcs.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/android/test_cpphttplib_for_app_on_android/app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ android {
1616
'-DANDROID_STL=c++_shared',
1717
'-DANDROID_ARM_NEON=TRUE',
1818
"-DANDROID_CPP_FEATURES=rtti exceptions"
19-
'-DANDROID_ABI=arm64-v8a'
20-
abiFilters "arm64-v8a"
19+
// '-DANDROID_ABI=arm64-v8a'
20+
abiFilters 'arm64-v8a'
2121
}
2222
}
2323
}

example/android/test_cpphttplib_for_app_on_android/app/src/main/AndroidManifest.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="com.citylife.test_cpphttplib_for_app_on_android">
4+
<uses-permission android:name="android.permission.INTERNET" />
5+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
6+
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
7+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
8+
49

510
<application
611
android:allowBackup="true"

example/android/test_cpphttplib_for_app_on_android/app/src/main/cpp/http/test_http.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313

1414
//#define CA_CERT_FILE "./ca-bundle.crt"
15-
#define CA_CERT_FILE "ca-bundle.crt"
15+
#define CA_CERT_FILE "/storage/emulated/0/test_cpphttplib_for_app_on_android/tmp/ca_bundle.crt"
1616
using namespace std;
1717

1818
void test_https() {

example/android/test_cpphttplib_for_app_on_android/app/src/main/java/com/citylife/test_cpphttplib_for_app_on_android/MainActivity.java

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
import android.os.Bundle;
55
import android.util.Log;
66
import android.widget.TextView;
7+
import android.os.Environment;
78

9+
import java.io.FileOutputStream;
810
import java.io.InputStream;
911
import java.util.Vector;
1012
import java.io.File;
13+
import java.lang.String;
1114

1215
public class MainActivity extends AppCompatActivity {
1316
private static final String TAG = "demo";
@@ -47,13 +50,36 @@ protected void onCreate(Bundle savedInstanceState) {
4750
}
4851

4952
try {
50-
InputStream in = getResources().openRawResource(R.raw.ca_bundle);
51-
int length = in.available();
52-
if (0 == length) {
53-
return;
53+
54+
55+
56+
/**
57+
* ref: https://blog.csdn.net/hacker_Lees/article/details/78593799
58+
*/
59+
String tmpDir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/test_cpphttplib_for_app_on_android/tmp";
60+
String cerFileName = tmpDir + "/ca_bundle.crt";
61+
File outFile = new File(cerFileName);
62+
if (!outFile.getParentFile().exists()) {
63+
createDir(tmpDir);
64+
}
65+
if (!outFile.exists()) {
66+
boolean ret = outFile.createNewFile();
67+
Log.i(TAG, String.valueOf(ret));
68+
InputStream in = getResources().openRawResource(R.raw.ca_bundle);
69+
int length = in.available();
70+
if (0 == length) {
71+
Log.i(TAG, String.valueOf(length));
72+
return;
73+
}
74+
byte[] buffer = new byte[length];
75+
in.read(buffer);
76+
77+
FileOutputStream outputStream = new FileOutputStream(outFile);
78+
outputStream.write(buffer);
79+
Log.i(TAG, cerFileName);
5480
}
55-
byte[] buffer = new byte[length];
56-
in.read(buffer);
81+
82+
5783
} catch (Exception e) {
5884
e.printStackTrace();
5985
}
@@ -65,6 +91,28 @@ protected void onCreate(Bundle savedInstanceState) {
6591
tv.setText(stringFromJNI());
6692
}
6793

94+
public static String createDir(String dirPath){
95+
//因为文件夹可能有多层,比如: a/b/c/ff.txt 需要先创建a文件夹,然后b文件夹然后...
96+
try{
97+
File file=new File(dirPath);
98+
if(file.getParentFile().exists()){
99+
Log.i(TAG, "----- 创建文件夹" + file.getAbsolutePath());
100+
file.mkdir();
101+
return file.getAbsolutePath();
102+
}
103+
else {
104+
createDir(file.getParentFile().getAbsolutePath());
105+
Log.i(TAG,"----- 创建文件夹" + file.getAbsolutePath());
106+
file.mkdir();
107+
}
108+
109+
}catch (Exception e){
110+
e.printStackTrace();
111+
}
112+
return dirPath;
113+
}
114+
115+
68116
/**
69117
* A native method that is implemented by the 'native-lib' native library,
70118
* which is packaged with this application.

0 commit comments

Comments
 (0)