B4J Question doGet Error

bjfhs

Active Member
Licensed User
Longtime User
B4X:
public String doGet(String url, String deviceId, String tokena) {
            HttpGet get =null;
            get = new HttpGet(url+"/"+deviceId);
            get.addHeader("app_key", "f8fc0c83-eebd-4926-9ac7-2584831ce303");
            get.addHeader("Authorization","Bearer" + " " + tokena);
            get.setHeader("Content-Type", "Content-Type:application/json; charset=UTF-8");
            try {
               
                HttpResponse response = this.execute(get);
               
                StatusLine statusLine = response.getStatusLine();
             
                int code = statusLine.getStatusCode();

                if(code==HttpURLConnection.HTTP_OK){
                   
                    HttpEntity entity = response.getEntity();
                   
                    InputStream is = entity.getContent();

                    BufferedReader br = new BufferedReader(new InputStreamReader(is));
                    String line = br.readLine();
                    while(line != null){
                        System.out.println(line);
                        line=br.readLine();
                    }
                }
            } catch (ClientProtocolException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            return line;
       
}

Error:
Compiling generated Java code. Error
javac 1.8.0_112
src\b4j\example\main.java:218: Error: 找不到符号
HttpResponse response = this.execute(get);
^
符号: 方法 execute(HttpGet)
1 Error
 

Daestrum

Expert
Licensed User
Longtime User
Without seeing the surrounding code it's difficult to determine the cause.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
you should use okhttputils2 instead of the httpclient directly.
 
Upvote 0

bjfhs

Active Member
Licensed User
Longtime User
You should never use such code for http communication. There are many problems with it.

Instead learn how to use the very simple and powerful jOkHttpUtils2 library.
Thank Erel.
I'm good at vb,not Java,but the demo(Source code provided by the vendor) was wroten by Java,so I think the Java inline is a good idea,at last I failed.
I want to test jOkHttpUtils2 library,but I don't know how to do it with OkHttpUtils2 library for the next code
B4X:
   public  String SELFCERTPATH = "D://work/software/B4j/20180314Test/cert/outgoing.CertwithKey.pkcs12";

    public  String SELFCERTPWD = "IoM@1234";

    public String TRUSTCAPATH = "D://work/software/B4j/20180314Test/cert/ca.jks";

    public  String TRUSTCAPWD = "Huawei@123";

    public void initSSLConfigForTwoWay() throws Exception {
     
     KeyStore selfCert = KeyStore.getInstance("pkcs12");
        selfCert.load(new FileInputStream(SELFCERTPATH), SELFCERTPWD.toCharArray());
        KeyManagerFactory kmf = KeyManagerFactory.getInstance("sunx509");
        kmf.init(selfCert, SELFCERTPWD.toCharArray());

        KeyStore caCert = KeyStore.getInstance("jks");
        caCert.load(new FileInputStream(TRUSTCAPATH), TRUSTCAPWD.toCharArray());
        TrustManagerFactory tmf = TrustManagerFactory.getInstance("sunx509");
        tmf.init(caCert);

        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
       
         SSLSocketFactory ssf = new SSLSocketFactory(sc);
        ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        ClientConnectionManager ccm = this.getConnectionManager();
        SchemeRegistry sr = ccm.getSchemeRegistry();
        sr.register(new Scheme("https", ssf, 8743));
       
       
    }
 
Upvote 0

bjfhs

Active Member
Licensed User
Longtime User
The vendor give me two files ca.jks and outgoing.CertwithKey.pkcs12,I don't konw how to chang it to keystore.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
There are two types of authentications. Server authentication and client authentication. Server authentication is always done and you can initialize OkHttpClient with OkHttpClient.InitializeAcceptAll to accept invalid certificates (your java code also does it).

Client authentication is not required in most cases and it does require special handling.
 
Upvote 0

bjfhs

Active Member
Licensed User
Longtime User
If I use OkHttpClient.InitializeAcceptAll , do I still need to use the ConfigureSSL ?
 
Upvote 0

bjfhs

Active Member
Licensed User
Longtime User
It need initSSLConfigForTwoWay,So I think the server also expects a client certificate.
Today ,I use this code
B4X:
import org.apache.http.client.utils.URIBuilder;

Compiling generated Java code. Error
javac 1.8.0_112
src\b4j\example\main.java:20: 错误: 找不到符号
import org.apache.http.client.utils.URIBuilder;
^
符号: 类 URIBuilder
位置: 程序包 org.apache.http.client.utils

Why I can't use:import org.apache.http.client.utils.URIBuilder?
 
Last edited:
Upvote 0

bjfhs

Active Member
Licensed User
Longtime User
Can you change the Java inline to jOkHttpUtils2?
 

Attachments

  • temp.zip
    28.2 KB · Views: 247
Upvote 0
Top