Upgrade from Java Legacy to v4.8.0 or Latest Version

    Introduction

    This is a major application update. Plivo recommends you always use the latest or an active version of our SDKs for guaranteed security, stability, and uptime. The active SDK versions are designed to handle intermittent and regional failures of API requests. In addition, they offer a host of security features, such as protection against DoS attacks and bot detection for suspicious user agents.

    Deprecation notice: We’re deprecating Plivo Java SDK legacy versions lower than v4.8.0 on January 31, 2022. If you use a deprecated version of our SDK after that date, your API requests and messaging may fail intermittently. Plivo will no longer provide bug fixes to these versions, and our support team may ask you to upgrade before debugging issues.

    Migrate your applications

    Java version support

    The Plivo Java SDK supports OpenJDK 8 and 11 and OracleJDK 8 and 11.

    Use the command Update-Package Plivo -Version 4.10.0 to upgrade to the active version of the SDK, or upgrade to the latest version.

    After you upgrade to the latest version of the SDK, you should check every program that depends on it and make changes to the syntax for several kinds of operations. Here are examples of how coding differs between the deprecated legacy version of the SDK and the latest active versions.

    Import the SDK

    LegacyLatest
    import com.plivo.helper.api.client.*;
    import com.plivo.helper.xml.elements.Dial;
       
    import com.plivo.api.Plivo;
    import com.plivo.api.xml.Dial;
       

    Initialize

    LegacyLatest
    RestAPI api = new RestAPI("<auth_id>","<auth_token>", "v1");
       
    Plivo.init("<auth_id>","<auth_token>");
       

    Access resources

    LegacyLatest
    Message resp = api.makeCall(parameters);
       
    MessageCreateResponse response = Message.creator(parameters)
        .create();
       

    Send a message

    LegacyLatest
    package com.plivo.test;
    
    import java.util.LinkedHashMap;
    import com.plivo.helper.api.client.*;
    import com.plivo.helper.api.response.message.MessageResponse;
    import com.plivo.helper.exception.PlivoException;
    
    public class SendMessage {
        public static void main(String[] args) {
            String authId = "<auth_id>";
            String authToken = "<auth_token>";
            RestAPI api = new RestAPI(authId, authToken, "v1");
    
            LinkedHashMap<String, String> parameters = new LinkedHashMap<String, String>();
            parameters.put("src", "12025551212"); 
            parameters.put("dst", "12025552323"); 
            parameters.put("text", "Hello, this is a test message"); 
            parameters.put("url", "https://<yourdomain>.com/sms_status/");
            try {
                MessageResponse msgResponse = api.sendMessage(parameters);
                System.out.println(msgResponse);
            } catch (PlivoException e) {
                System.out.println(e.getLocalizedMessage());
            }
        }
    }
       
    import java.io.IOException;
    import java.net.URL;
    import java.util.Collections;
    
    import com.plivo.api.Plivo;
    import com.plivo.api.exceptions.PlivoRestException;
    import com.plivo.api.models.message.Message;
    import com.plivo.api.models.message.MessageCreateResponse;
    
    class MessageCreate
    {
        public static void main(String [] args)
        {
            Plivo.init("<auth_id>","<auth_token>");
            try
            {
                MessageCreateResponse response = Message.creator("+12025551212","+12025552323",
                        "Hello, this is a test message")
                        .url(new URL("https://<yourdomain>.com/sms_status/") )
                        .create();
                System.out.println(response);
            }
            catch (PlivoRestException | IOException e)
            {
                e.printStackTrace();
            }
        }
    }
       

    Retrieve a message

    LegacyLatest
    package com.plivo.test;
    
    import java.util.LinkedHashMap;
    import com.plivo.helper.api.client.*;
    import com.plivo.helper.api.response.message.MessageResponse;
    import com.plivo.helper.exception.PlivoException;
    
    public class GetDetails {
        public static void main(String[] args) {
            String authId = "<auth_id>";
            String authToken = "<auth_token>";
            RestAPI api = new RestAPI(authId, authToken, "v1");
    
            LinkedHashMap<String, String> parameters = new LinkedHashMap<String, String>();
            parameters.put("record_id", "<your_message_uuid>");
            try {
                Message msg = api.getMessage(parameters);
                System.out.println(msg);
            } catch (PlivoException e) {
                System.out.println(e.getLocalizedMessage());
            }
        }
    }
       
    import java.io.IOException;
    import com.plivo.api.Plivo;
    import com.plivo.api.exceptions.PlivoRestException;
    import com.plivo.api.models.message.Message;
    
    class MessageGet
    {
        public static void main(String [] args)
        {
            Plivo.init("<auth_id>", "<auth_token>");
            try
            {
                Message response = Message.getter("<your_message_uuid>")
                        .get();
    
                System.out.println(response);
        
            }
            catch (PlivoRestException | IOException e)
            {
                e.printStackTrace();
            }
        }
    }
       

    List all messages

    LegacyLatest
    package com.plivo.test;
    
    import java.util.LinkedHashMap;
    import com.plivo.helper.api.client.*;
    import com.plivo.helper.api.response.message.MessageResponse;
    import com.plivo.helper.exception.PlivoException;
    
    public class GetAllDetails {
        public static void main(String[] args) {
            String authId = "<auth_id>";
            String authToken = "<auth_token>";
            RestAPI api = new RestAPI(authId, authToken, "v1");
            try {
                MessageFactory msg = api.getMessages();
    
                System.out.println(msg);
            } catch (PlivoException e) {
                System.out.println(e.getLocalizedMessage());
            }
        
            LinkedHashMap<String, String> parameters = new LinkedHashMap<String, String>();
            parameters.put("limit", "5"); 
            parameters.put("offset", "0"); 
            try {
                MessageFactory msg = api.getMessages(parameters);
                System.out.println(msg);
            } catch (PlivoException e) {
                System.out.println(e.getLocalizedMessage());
            }
        }
    }
       
    import java.io.IOException;
    
    import com.plivo.api.Plivo;
    import com.plivo.api.exceptions.PlivoRestException;
    import com.plivo.api.models.message.Message;
    import com.plivo.api.models.base.ListResponse;
    
    
    class GetAllMessageList
    {
        public static void main(String [] args)
        {
            Plivo.init("<auth_id>","<auth_token>");
            try
            {
                ListResponse<Message> response = Message.lister()
                        .limit(5)
                        .offset(0)
                        .list();
                System.out.println(response);
            }
            catch (PlivoRestException | IOException e)
            {
                e.printStackTrace();
            }
        }
    }