B4A Library Translate

somed3v3loper

Well-Known Member
Licensed User
I tried MultiPartpost and now I am getting
B4X:
{"error_code":"54001","error_msg":"Invalid Sign"}
It seems I need an appId but the strange thing is that it worked earlier with github author's appid so it might be a different issue

B4X:
private Sub Class_Globals
    Private  salt As String
    Private  Tol As String= "zh"
    Private from As String  = "en"
    Private appId As String = "20160424000019521"
    Private  securityKey As String= "Hv8XkkCeQTn5xGBPDYgj"
    Private call_back As Object
    Private event_Name As String
    Private context As String
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(callback As Object,eventName As String)
    
    call_back=callback
    event_Name=eventName
End Sub

Public Sub SetAppId(App_Id As String) As BaiduTrans
    appId=App_Id
    Return Me
End Sub


Public Sub SetSecurityKey( security_Key As String) As BaiduTrans
    securityKey=security_Key
    Return Me
End Sub


Public Sub ToLanguage( toLang As String) As BaiduTrans
    Tol=toLang
    Return Me
End Sub

Public Sub fromLanguage( fromLang As String) As BaiduTrans
    Return Me
End Sub

Public Sub  with(text As String)As BaiduTrans
    context=text
    
    Return Me
End Sub

Public Sub doTranslate
    salt = DateTime.Now
    Dim url As String  = "http://api.fanyi.baidu.com/api/trans/vip/translate"
    Dim transJob As HttpJob
    transJob.Initialize("transJob",Me)
    
'    Dim bodyformbuilder As JavaObject
'    bodyformbuilder.InitializeNewInstance("okhttp3.FormBody.Builder",Null)
'    bodyformbuilder.RunMethodjo("add",Array("q",trtext))
'    bodyformbuilder.RunMethodjo("add",Array("from",from))
'    bodyformbuilder.RunMethodjo("add",Array("to",Tol))
'    bodyformbuilder.RunMethodjo("add",Array("appid",appId))
'    bodyformbuilder.RunMethodjo("add",Array("salt",salt))
'    bodyformbuilder.RunMethodjo("add",Array("sign",MD5(appId&trtext&salt&securityKey)) )
    
'    Log(bodyformbuilder)
'    Dim s As String=bodyformbuilder.RunMethod("build",Null )
'    Log(s)

    Dim bodymap As Map
    bodymap.Initialize
    bodymap.Put("q",context)
    bodymap.Put("from",from)
    bodymap.Put("to",Tol)
    bodymap.Put("appid",appId)
    bodymap.Put("salt",salt)
    bodymap.Put("sign",MD5(appId&context&salt&securityKey))
    Dim jg As JSONGenerator
    jg.Initialize(bodymap)
    Dim s As String=jg.ToString
    Log(s)
    transJob.PostMultipart(url, bodymap,Null)   
'    Public void into( final OnTransSuccess onTransSuccess) {
'    OkHttpClient client = new OkHttpClient();
'    RequestBody body = new FormBody.Builder()
'    .add("q", context)
'    .add("from", from)
'    .add("to", To)
'    .add("appid", appId)
'    .add("salt", salt)
'    .add("sign", MD5.md5(appId + context + salt + securityKey))
'    .build();
'    String url = "http://api.fanyi.baidu.com/api/trans/vip/translate";
'    Request request = new Request.Builder()
'    .url(url)
'    .post(body)
'    .build();
'   
End Sub
Private Sub jobdone(okjob As HttpJob)
    If okjob.Success Then
        
            
            
        Log(okjob.GetString)
        
        
        
    Else
        Log("error with "&okjob.JobName& " " &okjob.ErrorMessage)
    End If
    
    okjob.Release
End Sub
Private Sub MD5(obj As String) As String
    Dim md As MessageDigest
    Dim ByteCon As ByteConverter
    Dim passwordhash() As Byte

    passwordhash = md.GetMessageDigest( obj.GetBytes("UTF8"),"MD5")


    Return ByteCon.HexFromBytes( passwordhash )

    
End Sub
 
Top