B4J Question send and data from B4j app to Java Servlet ?

Roberto P.

Well-Known Member
Licensed User
Longtime User
I'm trying to send data, string or binary format from an application written with B4J to a servlet written in pure Java.

I have tried in various ways, but I can not read the servlet data. Can anyone help me.


B4j Code

B4X:
    Dim j As HttpJob
    j.Initialize("send_data", Me) 

    Dim sData As String    =    "send data to server from client"
    LogError("lenght string = " & sData.Length)

    j.PostString("http://localhost:8080/devagonsit/servlet/ServletSynchro?name=pippo&city=milan", sData)
    
    Wait For (j) JobDone(j As HttpJob)
    
    If j.Success Then
        
        Log("chiamata avvenuta con successo x invio bytes")
        
        j.Release
        
    Else
        LogError("Chiamata servelet fallita")
    End If

Java Code

B4X:
  ServletInputStream is     = request.getInputStream();
          BufferedReader br         = null;
          StringBuilder stringBuilder = new StringBuilder(); 
          
          out.println("len buffer " + request.getContentLength());
          
          if (is != null) { 
              BufferedReader bufferedReader  = new BufferedReader(new InputStreamReader(is)); 

                char[] charBuffer = new char[128]; 
                int bytesRead = -1; 

                while ((bytesRead = bufferedReader.read(charBuffer)) > 0) { 
                    stringBuilder.append(charBuffer, 0, bytesRead); 
                } 
          
                out.println("lettura dati dal client " + stringBuilder.toString());
          }
          
          out.println("fine della lettura dei dati....");

the log show the dimension of data to receive to server, but I'm not able to read the data from server

client B4J app
lenght string = 31

Java
len buffer 31


thank in advance for any support.

roberto
 

Roberto P.

Well-Known Member
Licensed User
Longtime User
Your B4J code is correct. Why aren't you using jServer to build the server? It will be very simple.

bacause I must integrated an existing TomCat application widht my applications writens in B4A and B4j.
 
Upvote 0
Top