我的服务器在调用其HTTPS API时会提供一个自签名证书。我在文件asset夹中有证书文件,并在pubspec.yaml
尝试将证书传递到SecurityContext然后使用该上下文创建时引用了它的路径HttpClient。但是我将证书传递给的SecurityContext方式不起作用。这是代码:
Future<ByteData> getFileData(String path) async {
return await rootBundle.load(path);
}
void initializeHttpClient() async {
try {
Future<ByteData> data = getFileData('assets/raw/certificate.crt');
await data.then((value) {
var context = SecurityContext.defaultContext;
context.useCertificateChainBytes(value.buffer.asInt8List());
client = HttpClient(context: context);
});
} on Exception catch (exception) {
print(exception.toString());
}
}
Run Code Online (Sandbox Code Playgroud)
将SecurityContext有两种方法:
1)useCertificateChain()此接受文件路径。但是,当我在资产文件夹中提供文件的路径时(“ assets / raw / certificate.crt”)。它说找不到文件。
2)useCertificateChainBytes()上面的代码就是使用这种方法。但这也给我类似的错误(文件结尾意外)。
截至目前的解决方案
我使用绕过它client.badCertificateCallback = (X509Certificate cert, String host, int port)=> true;。
但我想使其与证书一起使用
我想,我们不能更新任何UI view元素(TextView,EditText在任何工作线程等),但在下面的例子中,我能够更新views从辅助线程,不是所有的时间,但只是偶尔.
请参阅以下示例我在工作线程中更新UI元素的示例 -
public class AndroidBasicThreadActivity extends AppCompatActivity
{
public static TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_basic_thread);
textView = (TextView) findViewById(R.id.textview);
MyAndriodThread myTask = new MyAndriodThread();
Thread t1 = new Thread(myTask, "Bajrang Hudda");
t1.start();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的工作线程 -
class MyAndriodThread implements Runnable
{
@Override
public void run()
{
AndroidBasicThreadActivity.textView.setText("Hello!! Android Team :-) From child thread.");
}
}
Run Code Online (Sandbox Code Playgroud)
相信我,我不会说任何例外 -
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view …Run Code Online (Sandbox Code Playgroud)