B4A Library GoogleUrlShortener

An Android/Java Library to create short URL's using Google's URL Shortener.
It wraps his project. https://github.com/PDDStudio/GoogleUrlShortener
GoogleURLShortener
Version:
0.09
  • GoogleURLShortener
    Events:
    • finishedLoading (shortUrl As String)
    • startedLoading
    Methods:
    • Initialize (EventName As String)
    • ShortURL (longUrl As String)
    Permissions:
    • android.permission.ACCESS_NETWORK_STATE
    • android.permission.INTERNET
preview.png
 

Attachments

  • GoogleUrlShortener.zip
    6.8 KB · Views: 215
  • URLShortenerEx.zip
    7.3 KB · Views: 186
Last edited:

imgsimonebiliato

Well-Known Member
Licensed User
Longtime User
Where's "okio-1.6.0" library?
 

imgsimonebiliato

Well-Known Member
Licensed User
Longtime User

so27

Active Member
Licensed User
Longtime User
Where can I find okhttp-3.0.0-rc1.jar ?
 

imgsimonebiliato

Well-Known Member
Licensed User
Longtime User
I founded the *.jar missing file with google.
I started the app, but I've got this error log:


B4X:
LogCat connected to: 0123456789ABCDEFG

Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1), thread 30565 (config)

** Activity (main) Create, isFirst = true **

** Activity (main) Resume **
java.lang.RuntimeException: An error occured while executing doInBackground()


    at android.os.AsyncTask$3.done(AsyncTask.java:299)
    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
    at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
    at java.util.concurrent.FutureTask.run(FutureTask.java:239)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)


    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
    at java.lang.Thread.run(Thread.java:838)
Caused by: java.lang.NoClassDefFoundError: com.google.gson.Gson
    at com.pddstudio.urlshortener.async.AsyncLoader.doInBackground(AsyncLoader.java:42)
    at com.pddstudio.urlshortener.async.AsyncLoader.doInBackground(AsyncLoader.java:23)
    at android.os.AsyncTask$2.call(AsyncTask.java:287)
    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
    ... 4 more
** Activity (main) Pause, UserClosed = true **
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You don't need a library for such simple web service:

Depends on the following libraries: OkHttpUtils2, OkHttp and JSON.
B4X:
Sub Process_Globals
   Private apikey As String = "AIzaSyCmF..." 'API key from Google developer console
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
   ShortenUrl("www.b4x.com")
End Sub

Sub ShortenUrl(url As String)
   Dim j As HttpJob
   j.Initialize("shortenurl", Me)
   Dim jg As JSONGenerator
   jg.Initialize(CreateMap("longUrl": url))
   j.PostBytes($"https://www.googleapis.com/urlshortener/v1/url?key=${apikey}"$, jg.ToString.GetBytes("UTF8"))
   j.GetRequest.SetContentType("application/json")
End Sub

Sub JobDone(job As HttpJob)
   If job.JobName = "shortenurl" Then
     If job.Success Then
       Dim jp As JSONParser
       jp.Initialize(job.GetString)
       Dim shortUrl As String = jp.NextObject.Get("id")
       Log($"Short url: ${shortUrl}"$)
     Else
       Log("Error: " & job.ErrorMessage)
     End If
   End If
   job.Release
End Sub

The API is described here: https://developers.google.com/url-shortener/v1/getting_started#

Note that you can create an Android credentials key that is not restricted to any specific package or signing key.
 

imgsimonebiliato

Well-Known Member
Licensed User
Longtime User
Hi Erel,
please, can you explain me this line?
B4X:
j.PostBytes($"https://www.googleapis.com/urlshortener/v1/url?key=${apikey}"$, jg.ToString.GetBytes("UTF8"))
Maybe with a concrete example please
 

imgsimonebiliato

Well-Known Member
Licensed User
Longtime User
What do I do with apis? Only create one "Android key" new?
 
Top