Android Question what objects for network

Roberto P.

Well-Known Member
Licensed User
Longtime User
Hello to all,
I need help to understand which HTTP objects I must use in B4A to replicate this Java example. I tried with HttpJob, but there is no match.

B4X:
                    HttpPost httppost = new HttpPost(address); // ?
                  
                    HttpParams httpParameters = new BasicHttpParams(); // ?
                    // Set the timeout in milliseconds until a connection is established.
                    // The default value is zero, that means the timeout is not used.
                    int timeoutConnection = 3000;
                    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
                    // Set the default socket timeout (SO_TIMEOUT)
                    // in milliseconds which is the timeout for waiting for data.
                    int timeoutSocket = 5000;
                    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

                    HttpClient httpclient = new DefaultHttpClient(httpParameters); // ?
                  
                    // Create print document(String)
                    String req = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                            "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
                                "<s:Body>" +
                                    // data
                                    "<printerFiscalReceipt>" +
                                        "<beginFiscalReceipt operator=\"10\" />" +
                                        // list
                                        list +
                                        // end list
                                        "<printRecTotal operator=\"10\" description=\"" + "CONTANTE" /*metodo_pag.ToUpper*/ + "\" payment=\"" + updated_total_amount + "\" paymentType=\"2\" index=\"0\" justification=\"1\" />" +
                                        "<printRecMessage  operator=\"10\" messageType=\"3\" index=\"1\" font=\"4\" message=\"Arrivederci e Grazie\" />" +
                                        "<endFiscalReceipt operator=\"10\" />" +
                                    "</printerFiscalReceipt>" +
                                    // end data
                                "</s:Body>" +
                            "</s:Envelope>";
                    StringEntity entity = new StringEntity(req , HTTP.UTF_8);
                    entity.setContentType("text/xml charset=utf-8");
                    httppost.setEntity(entity);
                    HttpResponse response = httpclient.execute(httppost); // ?
                    // Receive response document
                    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                    DocumentBuilder builder = factory.newDocumentBuilder();

                    // Parse response document(DOM)
                    Document doc = builder.parse(response.getEntity().getContent());
                    Element el = (Element)doc.getElementsByTagName("response").item(0);
                    t_status.setText("PRINTER STATUS\n");
                    t_status.append("Success: " + el.getAttribute("success") + "\n");
                    t_status.append("Code: " + el.getAttribute("code") + "\n");
                    t_status.append("Status: " + el.getAttribute("status"));

B4A code:

B4X:
' pulsante
Sub btnTest_Click
  
    Try
      
          
        Dim j As HttpJob
        j.Initialize("send_data", Me)
        j.JobName = "Printer"
                    
    j.PostString(link, GetStringPrinter)
  
    Catch
        Log(LastException)
    End Try
  
End Sub


Private Sub GetStringPrinter As String
  
    Dim aMessage As StringBuilder
    aMessage.Initialize
  
  
    aMessage.Append("String req = '<?xml version=\1.0\encoding=\utf-8\?>'")
    aMessage.Append("<s:Envelope xmlns:s=\http://schemas.xmlsoap.org/soap/envelope/\>")
    aMessage.Append("<s:Body>")
    aMessage.Append("<printerFiscalReceipt>")
    aMessage.Append("<beginFiscalReceipt operator=\10\ />")
  
    ' data
  
    aMessage.Append("</printerFiscalReceipt>")
    aMessage.Append("</s:Body>")
    aMessage.Append("</s:Envelope>")
  
    Return aMessage.ToString
end sub

Thanks in advance for help
 

Sandman

Expert
Licensed User
Longtime User
Well, I don't really know anything about java, but reading that code, this is the flow as far as I can tell:
  1. It sets two different timeouts in http parameters (one for the connection and one for the socket)
  2. Then it creates the http client, using those parameters
  3. Then it creates a string, named req, that will be used as the actual request
  4. Then it sets the content type for the request
  5. Then it posts the request (but nowhere is it specified to use the address parameter for the method so I have no idea how that works)
  6. Then it receives the response using some magic I don't understand
  7. Then it parses the DOM received, locating the first tag called response
  8. Then it sets t_status to a header and three attributes from that response tag

Looking at your code you do this in btnTest_Click:
  1. You create a new HttpJob, inits and name it
  2. You post the value from GetStringPrinter to link
And you do this is GetStringPrinter:
  1. You return a string, aMessage, inspired by req in the java example.

Perhaps you decided to remove things for the forum, where you say data there are lots missing. And the first append even starts with actual java code, so that's wrong. Also, you don't even receive the response from your post anywhere. All in all I'd say you're pretty far from the java example. Hopefully my list above might be of some help.
 
Upvote 0

Roberto P.

Well-Known Member
Licensed User
Longtime User
What is the request result?

Correct way to send a http request: [B4X] OkHttpUtils2 with Wait For

Hi Erel,
I can't understand which B4A objects I should use to replicate the Java sample code that I have attached.

Eg:
- BasicHttpParams
- HttpClienthttpclient = new DefaultHttpClient(httpParameters); // ?
- HttpResponse response = httpclient.execut

thank you
 
Upvote 0
Top