"Block Chain" Ethernet Square web3j for Java use-create vouchers for users credentials <2>__mysql

Source: Internet
Author: User
Create vouchers for users credentials <2>

Environment: Mac OS & Android Studio

Reference: Here First, initial work

View previous article: Here Two, create credentials

String FilePath = Environment.getexternalstoragedirectory (). toString () + "/pictures";
String fileName = walletutils.generatenewwalletfile ("Password", new File (FilePath), false);
Credentials Credentials = walletutils.loadcredentials ("password", filepath+ "/" +filename);

"Password"-> enter your password
//So you can create a voucher

But you have a problem:

/storage/emulated/0/pictures/utc--2017--11-28....json (Permission denied) "

Solution:

Plus permissions:
 <!--sdcard Create and delete file permissions-->  
  <uses-permission android:name= "android.permission.MOUNT_UNMOUNT_ Filesystems "/>  
 <!--write data permissions to SDcard-->  
 <uses-permission android:name=" Android.permission.WRITE_EXTERNAL_STORAGE "/>  

But it may still fail, don't panic, add a paragraph before creating the voucher:

int request_external_storage = 1;
String[] Permissions_storage = {
       Manifest.permission.READ_EXTERNAL_STORAGE,
       Manifest.permission.WRITE_ External_storage
};
int permission = activitycompat.checkselfpermission (Mainactivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) ;

F (Permission!= packagemanager.permission_granted) {
      //We don ' t have permission so prompt the user
      Activitycompa T.requestpermissions (
          mainactivity.this,
          permissions_storage,
          request_external_storage
      );
}

You can succeed:

Third, the main code

Package com.example.pro.blockchain1; Import Android.
Manifest;
Import Android.content.pm.PackageManager;
Import Android.os.Bundle;
Import android.os.Environment;
Import Android.support.v4.app.ActivityCompat;
Import android.support.v7.app.AppCompatActivity;
Import Android.view.View;
Import Android.widget.Button;

Import Android.widget.TextView;
Import org.web3j.crypto.CipherException;
Import Org.web3j.crypto.Credentials;
Import Org.web3j.crypto.WalletUtils;
Import org.web3j.protocol.Web3j;
Import Org.web3j.protocol.Web3jFactory;
Import org.web3j.protocol.core.methods.response.Web3ClientVersion;

Import Org.web3j.protocol.http.HttpService;
Import Java.io.File;
Import java.io.IOException;
Import java.security.InvalidAlgorithmParameterException;
Import java.security.NoSuchAlgorithmException;
Import java.security.NoSuchProviderException;


Import java.util.concurrent.ExecutionException; public class Mainactivity extends appcompatactivity implements view.onclicklistener{private ButtonShowbutton;

    Private TextView TextView;
    public static final int show = 0;
        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
        Setcontentview (R.layout.activity_main);
        textview= (TextView) Findviewbyid (R.id.textview);
        Showbutton = (Button) Findviewbyid (R.id.showview);
    Showbutton.setonclicklistener (this);
        @Override public void OnClick (View v) {if (V.getid () = = R.id.showview) {show (); } private void Show () {New Thread (new Runnable () {@Override public void run ()
                    {try {String url = ' http://10.0.2.2:8545 ';
                    web3j web3 = web3jfactory.build (new Httpservice (URL));
                    Web3clientversion web3clientversion = Web3.web3clientversion (). SendAsync (). get ();

     String clientversion = Web3clientversion.getweb3clientversion ();               Get voucher int request_external_storage = 1;
                            String[] Permissions_storage = {Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE}; int permission = activitycompat.checkselfpermission (Mainactivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)

                    ; if (permission!= packagemanager.permission_granted) {//We don ' t have permission so prompt the U
                                Ser activitycompat.requestpermissions (Mainactivity.this,
                        Permissions_storage, Request_external_storage
                    );
                    String FilePath = Environment.getexternalstoragedirectory (). toString () + "/pictures"; String FileName = WalletutiLs.generatenewwalletfile ("123456", New File (FilePath), false);

                    Credentials Credentials = walletutils.loadcredentials ("123456", filepath+ "/" +filename);
                    String myAddress = credentials.getaddress ();

                Textview.settext (myAddress);
                catch (Executionexception e) {e.printstacktrace ();
                catch (Interruptedexception e) {e.printstacktrace ();
                }catch (IOException e) {e.printstacktrace ();
                }catch (cipherexception e) {e.printstacktrace ();
                catch (NoSuchAlgorithmException e) {e.printstacktrace ();
                catch (Nosuchproviderexception e) {e.printstacktrace ();
                catch (Invalidalgorithmparameterexception e) {e.printstacktrace ();
    }}). Start (); }
}
Iv. credentials created using a private chain

Here I do not have a good solution for the time being, I hope to have big Brother can answer. Here's My method:

1. Open local private chain folder find KeyStore file

Pick a random one, like: Utc--2017-11-16t08-22-03.297518621z--ea7cb6058aaf6369c3a97c4a5203364c8c4ff7fa

2. Turn on Monitor

Toolbars: Tools-> Android-> android Device Monitor

3. Put the KeyStore file into the sdcard/directory Andro code

/* Use existing voucher *
/String FilePath = "sdcard/";
String fileName = "Utc--2017-11-16t08-22-03.297518621z--ea7cb6058aaf6369c3a97c4a5203364c8c4ff7fa";
Credentials Credentials = walletutils.loadcredentials ("123456", FilePath + "/" + fileName);
String myAddress = credentials.getaddress ();
Textview.settext (myAddress);

You can view the address of the EA7CB6058AAF6369C3A97C4A5203364C8C4FF7FA

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.