Android Question How create a binary string

Discussion in 'Android Questions' started by Badesse, Feb 1, 2015.

  1. Badesse

    Badesse New Member Licensed User

    Hello, (Sorry for my Engish, I'm french and don't speak english very well)

    Let me come ask you for a little help because I can not coding in b4a a Java function that I found on the Net.
    This function is intended to return the Latitude and Longitude of the "celltower" on which the GSM is connected:
    http://android-coding.blogspot.fr/2011/06/convert-celllocation-to-real-location.html

    I can without problems recovering the lac and cid:
    Code:
    Dim r As Reflector
    Dim cid, lac As Int

    r.Target = r.GetContext
    r.Target = r.RunMethod2(
    "getSystemService""phone""java.lang.String")
    r.Target = r.RunMethod(
    "getCellLocation")
    cid = r.RunMethod(
    "getCid")
    lac = r.RunMethod(
    "getLac")

    I transferred my request through httpUtils2:
    Code:
    Dim Http As HttpJob
    Dim MyData as Byte

    Http.PostBytes(
    "http://www.google.com/glm/mmap",MyData)
    My problem is that I can not build my binary string :/

    I watched a lot of posts and tutorials, and tested libraries "ByteConverter" and "RandomAccessFile" but without success.

    How can I get the following string in my MyData variable:
    Code:
    DataOutputStream dataOutputStream = new DataOutputStream(out);
    dataOutputStream.writeShort(
    21);
    dataOutputStream.writeLong(
    0);
    dataOutputStream.writeUTF(
    "en");
    dataOutputStream.writeUTF(
    "Android");
    dataOutputStream.writeUTF(
    "1.0");
    dataOutputStream.writeUTF(
    "Web");
    dataOutputStream.writeByte(
    27);
    dataOutputStream.writeInt(
    0);
    dataOutputStream.writeInt(
    0);
    dataOutputStream.writeInt(
    3);
    dataOutputStream.writeUTF(
    "");

    dataOutputStream.writeInt(cid);
    dataOutputStream.writeInt(lac);

    dataOutputStream.writeInt(
    0);
    dataOutputStream.writeInt(
    0);
    dataOutputStream.writeInt(
    0);
    dataOutputStream.writeInt(
    0);
    dataOutputStream.flush();
    Thanking you in advance for your help.
     
  2. eurojam

    eurojam Well-Known Member Licensed User

  3. Erel

    Erel Administrator Staff Member Licensed User

    Use RandomAccessFile and create a temporary file. It should be simple to convert this code.

    To convert writeUTF:
    Code:
    Sub WriteUTF(raf As RandomAccessFile, s As String)
     
    Dim b() As Byte = s.GetBytes("UTF8")
     raf.WriteShort(b.Length, raf.CurrentPosition)
     raf.WriteBytes(b, 
    0, b.Length, raf.CurrentPosition)
    End Sub
    Then use Bit.InputStreamToBytes to read the data.
     
  4. Badesse

    Badesse New Member Licensed User

    @eurojam :

    Thank you for your reply but I've already tested this library but it doesn't work for me.

    Return is "Provider Disabled"

    I think it based on "opencellid.org" who doesn't know all CellTower (For Exemple the Tower which i'm connected is not known). I've tested it on the website without result.
    I have a php page who test on Google API and Google know it.

    @Erel :
    Ok, thanks, i test this solution
     
  5. Badesse

    Badesse New Member Licensed User

    Erel say : It should be simple to convert this code.

    And me and me, i can not do this .... :oops:

    Yet I think I understand :

    Code:
    Sub Activity_Resume
       
        
    ' Get cid & lac
        r.Target = r.GetContext
        r.Target = r.RunMethod2(
    "getSystemService""phone""java.lang.String")
        r.Target = r.RunMethod(
    "getCellLocation")
        cid = r.RunMethod(
    "getCid")
        lac = r.RunMethod(
    "getLac")

        
    ' Write Binary String
        If File.Exists(File.DirRootExternal, "tmp_out.dat"Then
            
    File.Delete(File.DirRootExternal, "tmp_out.dat")
        
    End If
       
        raf1.Initialize( 
    File.DirRootExternal, "tmp_out.dat"False)
        raf1.WriteShort(
    21,raf1.CurrentPosition)
        raf1.WriteLong(
    0,raf1.CurrentPosition)
        WriteUTF(raf1,
    "en")
        WriteUTF(raf1,
    "Android")
        WriteUTF(raf1,
    "1.0")
        WriteUTF(raf1,
    "Web")
        raf1.WriteByte(
    27,raf1.CurrentPosition)
        raf1.WriteInt(
    0,raf1.CurrentPosition)
        raf1.WriteInt(
    0,raf1.CurrentPosition)
        raf1.WriteInt(
    3,raf1.CurrentPosition)
        WriteUTF(raf1,
    "")
        raf1.WriteInt(cid,raf1.CurrentPosition)
        raf1.WriteInt(lac,raf1.CurrentPosition)
        raf1.WriteInt(
    0,raf1.CurrentPosition)
        raf1.WriteInt(
    0,raf1.CurrentPosition)
        raf1.WriteInt(
    0,raf1.CurrentPosition)
        raf1.WriteInt(
    0,raf1.CurrentPosition)
        raf1.Flush
        raf1.Close

        
    ' Get Binary String
        MyData = Bit.InputStreamToBytes( File.OpenInput(File.DirRootExternal, "tmp_out.dat") )
       
        
    ' Test Binary String sent
        'ToastMessageShow( BC.StringFromBytes(MyData, "UTF8"),True)
       
        
    ' Send Request
        Http.Initialize("http",Me)
        Http.PostBytes(
    "http://www.google.com/glm/mmap", MyData )

    End Sub

    Sub JobDone (Job As HttpJob)
        
    If Job.Success = True Then
           
            
    ' Write InputStream
            Dim out As OutputStream = File.OpenOutput(File.DirRootExternal, "tmp_in.dat"False)
            
    File.Copy2(Job.GetInputStream, out)
            out.Close
           
            
    ' Test Binary String return
            MyData = Bit.InputStreamToBytes( File.OpenInput(File.DirRootExternal, "tmp_in.dat") )
            
    ToastMessageShow( BC.StringFromBytes(MyData, "UTF8"),True)

        
    Else
            
    ToastMessageShow("Error: " & Job.ErrorMessage, True)
        
    End If
        Job.Release
    End Sub

    Sub WriteUTF(raf As RandomAccessFile, s As String)
        
    Dim b() As Byte = s.GetBytes("UTF8")
        raf.WriteShort(b.Length, raf.CurrentPosition)
        raf.WriteBytes(b, 
    0, b.Length, raf.CurrentPosition)
    End Sub
    Do you have an idea of my stupidity ?
     

    Attached Files:

    • Test.zip
      File size:
      462.6 KB
      Views:
      56
  6. Erel

    Erel Administrator Staff Member Licensed User

    Your code looks correct. Do you get any error message?
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice