Android Question Return value of "http://192.168.2.102/?pin=leer" of the HttpJob.bas

funker

Member
Licensed User
Hi good afternoon.
How can the file be expanded below, this Http works with a return value?
The return value is a string that evaluates to a number.

Thank you.
Greeting

"http://192.168.2.102/?pin=leer"

Danke.
Gruss

HttpJob.bas
B4X:
Type=Class
Version=6
ModulesStructureVersion=1
B4A=true
@EndOfDesignText@
'HttpUtils2 version 2.01
'Class module
Sub Class_Globals
    Public JobName As String
    Public Success As Boolean
    Public Username, Password As String
    Public ErrorMessage As String
    Private target As Object
    Private taskId As String
    Private req As HttpRequest
    Public Tag As Object
End Sub

'Initializes the Job.
'Name - The job's name. Note that the name doesn't need to be unique.
'TargetModule - The activity or service that will handle the JobDone event.
Public Sub Initialize (Name As String, TargetModule As Object)
    JobName = Name
    target = TargetModule
End Sub
'Sends a POST request with the given data as the post data.
Public Sub PostString(Link As String, Text As String)
    PostBytes(Link, Text.GetBytes("UTF8"))
End Sub

'Sends a POST request with the given string as the post data
Public Sub PostBytes(Link As String, Data() As Byte)
    req.InitializePost2(Link, Data)
    CallSubDelayed2(HttpUtils2Service, "SubmitJob", Me)
End Sub

'Sends a POST request with the given file as the post data.
'This method doesn't work with assets files.
Public Sub PostFile(Link As String, Dir As String, FileName As String)
    Dim length As Int
    If Dir = File.DirAssets Then
        Log("Cannot send files from the assets folder.")
        Return
    End If
    length = File.Size(Dir, FileName)
    Dim In As InputStream
    In = File.OpenInput(Dir, FileName)
    If length < 1000000 Then '1mb
        'There are advantages for sending the file as bytes array. It allows the Http library to resend the data
        'if it failed in the first time.
        Dim out As OutputStream
        out.InitializeToBytesArray(length)
        File.Copy2(In, out)
        PostBytes(Link, out.ToBytesArray)
    Else
        req.InitializePost(Link, In, length)
        CallSubDelayed2(HttpUtils2Service, "SubmitJob", Me)
    End If
End Sub
'Submits a HTTP GET request.
'Consider using Download2 if the parameters should be escaped.
Public Sub Download(Link As String)
    req.InitializeGet(Link)
    CallSubDelayed2(HttpUtils2Service, "SubmitJob", Me)
End Sub
'Submits a HTTP GET request.
'Encodes illegal parameter characters.
'<code>Example:
'job.Download2("http://www.example.com", _
'    Array As String("key1", "value1", "key2", "value2"))</code>
Public Sub Download2(Link As String, Parameters() As String)
    Dim sb As StringBuilder
    sb.Initialize
    sb.Append(Link)
    If Parameters.Length > 0 Then sb.Append("?")
    Dim su As StringUtils
    For i = 0 To Parameters.Length - 1 Step 2
        If i > 0 Then sb.Append("&")
        sb.Append(su.EncodeUrl(Parameters(i), "UTF8")).Append("=")
        sb.Append(su.EncodeUrl(Parameters(i + 1), "UTF8"))
    Next
    req.InitializeGet(sb.ToString)
    CallSubDelayed2(HttpUtils2Service, "SubmitJob", Me)    
End Sub

'Called by the service to get the request
Public Sub GetRequest As HttpRequest
    Return req
End Sub

'Called by the service when job completes
Public Sub Complete (id As Int)
    taskId = id
    CallSubDelayed2(target, "JobDone", Me)
End Sub

'Should be called to free resources held by this job.
Public Sub Release
    File.Delete(HttpUtils2Service.TempFolder, taskId)
End Sub

'Returns the response as a string encoded with UTF8.
Public Sub GetString As String
    Return GetString2("UTF8")
End Sub

'Returns the response as a string.
Public Sub GetString2(Encoding As String) As String
    Dim tr As TextReader
    tr.Initialize2(File.OpenInput(HttpUtils2Service.TempFolder, taskId), Encoding)
    Dim res As String
    res = tr.ReadAll
    tr.Close
    Return res
End Sub

'Returns the response as a bitmap
Public Sub GetBitmap As Bitmap
    Dim b As Bitmap
    b = LoadBitmap(HttpUtils2Service.TempFolder, taskId)
    Return b
End Sub

Public Sub GetInputStream As InputStream
    Dim In As InputStream
    In = File.OpenInput(HttpUtils2Service.TempFolder, taskId)
    Return In
End Sub
 
Last edited:

KMatle

Expert
Licensed User
Longtime User
Hi @funker: Please don't do multiple posts about the same issue (here plus German forum). Bitte keine Doppelposts!

You rather need to understand the concept than to change the HttpUtils module (no change is needed).

As you say you need a return value. So what is the code of your ESP? This is the place where you return a value.

In B4A you can get it like this:

B4X:
Sub JobDone(Job As HttpJob)
    If Job.Success Then
        Dim res As String
        res = Job.GetString
        Log("Back from Job:" & Job.JobName )
        Log("Response from server: " & res)
    Else
        Log("Error: " & Job.ErrorMessage)
    End If
    Job.Release
End Sub
 
Upvote 0

funker

Member
Licensed User
Ich dachte ich mache es mal im englischen Forum bekannt.
Ich nehme an das die Engländer hier reingehen und nicht in das andere Forum um eine Frage auf den Grund zu gehen.

Gruss
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
This is the english part of the forum. Please write english here.
Your chances to get an answer here is much higher than in the german forum...
 
Upvote 0

funker

Member
Licensed User
this Code from ESP8266.

returned with "client.write(&my_string[0],8)" from ESP8266

Greeting

B4X:
#include <ESP8266WiFi.h>

char my_string[32];
byte dat_byte, inbyte;

const char* ssid = "----------";
const char* password = "----------";

WiFiServer server(80);

void setup() {
  Serial.begin(9600);

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
  server.begin();
}

void loop() {

  if (Serial.available()){
    inbyte = Serial.read();
    dat_byte=inbyte;
  }
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  while(!client.available()){
    delay(1);
  }
  String req = client.readStringUntil('\r');
  client.flush();

  my_string[0]=dat_byte;

  client.write(&my_string[0],8);

  if (req.indexOf("vor") != -1){
    Serial.println("v");
  }
  else if (req.indexOf("zur") != -1){
    Serial.println("z"); 
  }
   else if (req.indexOf("hlinks") != -1){
    Serial.println("k");
  }   
  else if (req.indexOf("links") != -1){
    Serial.println("l");
  }
  else if (req.indexOf("hrechts") != -1){
    Serial.println("h");
  } 
  else if (req.indexOf("rechts") != -1){
    Serial.println("r");
  } 
  else if (req.indexOf("mitte") != -1){
    Serial.println("m"); 
  } 
  else if (req.indexOf("v0") != -1){
    Serial.println("w"); 
  }
  else if (req.indexOf("v1") != -1){
    Serial.println("s"); 
  } 
  else if (req.indexOf("v2") != -1){
    Serial.println("x"); 
  } 
   else if (req.indexOf("ser0") != -1){
    Serial.println("q"); 
  } 
  else if (req.indexOf("ser1") != -1){
    Serial.println("u"); 
  }
  else if (req.indexOf("sharp") != -1){
    Serial.println("f"); 
  } 
  else if (req.indexOf("ton") != -1){
    Serial.println("t"); 
  }     
  else if (req.indexOf("leer") != -1){
     Serial.println("p");   
  }     
  else {
    Serial.println("invalid request");
    client.stop();
    return;
  }

  client.flush();

  delay(1);
}
 
Upvote 0
Top