Jump to content

Java API Example Code


sampsonight

Recommended Posts

sampsonight

I'm having some trouble getting started with the Java API. The Github page references looking at the andriod app but the link is broken (404). 

 

The initial snippets for authing to a known server url does not seem to compile.  Is there any reference Java code I can look at that uses the API on github?

Link to comment
Share on other sites

Hi.  Can you show me the errors you are encountering and I'll see if I can get the example fixed up.  Thanks.

Link to comment
Share on other sites

sampsonight

Thanks!

 

Probably should have led with the code... 

 

I have the below class that compiles all but the apiClient.AuthenticateUserAsync() method. See below errors. I grabbed the master branch of the Java API and compiled/imported the jar into my project. The Types are recognized.

public class LoginService {
    
	// Developers should create their own logger implementation
	ILogger logger = new Logger(this.getClass());

    
    // The underlying http stack. Developers can inject their own if desired
    IAsyncHttpClient httpClient = new HttpClient();

    // Android developers should use GsonJsonSerializer
    IJsonSerializer jsonSerializer = new Seralizer();

    // Android developers should use AndroidDevice
    IDevice device = new Device("deviceId", "deviceName");

    ApiClient apiClient = new ApiClient(httpClient, jsonSerializer, logger, "http://localhost:8096", "My app name", "app version 123", device, new ApiEventListener());

    
    apiClient.AuthenticateUserAsync("username", "password", new Response<AuthenticationResult>(){

        @Override
        public void onResponse(AuthenticationResult result) {
            // Authentication succeeded
        }

        @Override
        public void onError() {
            // Authentication failed
        }

    });
    
    
}

These errors occur on OpenJDK 1.858c49b73c5a0a_CompileErrors.png

Edited by sampsonight
Link to comment
Share on other sites

Okay, I think potentially you imported some incorrect references but let's back up first and ask what you are trying to accomplish overall?  What is your app going to do?

 

There are some newer methods to handling the connection now and I can update the docs but I'd like to know what you are really trying to accomplish in order to steer you in the right direction.

 

Thanks.

Link to comment
Share on other sites

sampsonight

Sure. What I would like to do is auth to Emby and pull the music library. Looking to make a music player app. Right now looking at authing to a single emby server.

Link to comment
Share on other sites

For your own personal use, meaning you are just going to hard code the connection information or for other's consumption so that you will need a full connection workflow?

Link to comment
Share on other sites

sampsonight

For others to consume as well as myself. Users will put in their server address, and user/pass. Not looking to implement Emby connect, but may in the future.

Link to comment
Share on other sites

Then definitely use the example further down the page with ConnectionManager.  Please try that and let me know where you run into problems.  Thanks.

Link to comment
Share on other sites

sampsonight

Are you referencing the Multi-Server Usage section? That section  uses a connection manager to handle multiple servers. I only need access to a single server at a time and I don't want to use the Service apps section with a key. 

 

Isn't the andriod app using this Java api? I'm surprised its not open source where I can look at how its implemented...

Link to comment
Share on other sites

Yes the android app is using it. If you just want single server then you can just instantiate an ApiClient instance.

 

We also have an android-specific version that you could use if you want: https://github.com/MediaBrowser/Emby.ApiClient.Java/blob/master/android/src/main/java/mediabrowser/apiinteraction/android/AndroidApiClient.java

 

You'll want to use the second constructor. In order to instantiate you have to supply some dependencies, but there are implementations available in the library that you can use

 

IAsyncHttpClient - VolleyHttpClient

IJsonSerializer - GsonSerializer

IDevice - AndroidDevice

 

There was a time when we wanted this library to work in pure standalone Java, so that is the reason for separating all of these services into modular dependencies.

Link to comment
Share on other sites

Our apps all use the ConnectionManager interface at this point.  You can use a simplified approach as Luke said above, but the ConnectionManager handles a lot of the dirty work for you and then your app is ready for any type of connection you wish to allow.

 

That's why I suggested using it instead.

Link to comment
Share on other sites

sampsonight

I appreciate both of your help. 

@@Luke, thanks for the info. The app will not be an andriod app, but I don't mind faking some andriod specific args in constructors here and there.

 

@@ebr, I'll take a look at using the Connection Manager. I was going for a simple approach to initially get a connection and then start playing with the API, but if most apps are using Connection Manager that is probably the one to go with.

 

@@All,

 

Still not sure why I can instantiate the APIClient but when I try to use a method from the instance the project fails to compile. Even tried the void method: ensureWebSocket and it still fails.

 

I"ll keep digging.  :)

 

Added full code with imports in case I'm missing an import as @@ebr suggested a few posts back.

package com.mini.emby.services.auth;


import org.springframework.stereotype.Service;

import com.mini.emby.services.http.HttpClient;
import com.mini.emby.services.logging.Logger;
import com.mini.emby.services.serializers.Seralizer;

import mediabrowser.apiinteraction.ApiClient;
import mediabrowser.apiinteraction.ApiEventListener;
import mediabrowser.apiinteraction.device.Device;
import mediabrowser.apiinteraction.device.IDevice;
import mediabrowser.apiinteraction.http.IAsyncHttpClient;
import mediabrowser.model.logging.ILogger;
import mediabrowser.model.serialization.IJsonSerializer;
@Service
public class LoginService {
    
	// Developers should create their own logger implementation
	ILogger logger = new Logger(this.getClass());

    
    // The underlying http stack. Developers can inject their own if desired
    IAsyncHttpClient httpClient = new HttpClient();

    // Android developers should use GsonJsonSerializer
    IJsonSerializer jsonSerializer = new Seralizer();

    // Android developers should use AndroidDevice
    IDevice device = new Device("deviceId", "deviceName");

    ApiClient apiClient = new ApiClient(httpClient, jsonSerializer, logger, "http://localhost:8096", "My app name", "app version 123", device, new ApiEventListener());
    
    apiClient.ensureWebSocket();
    
    
    
    
/*	apiClient.AuthenticateUserAsync("username", "password", new Response<AuthenticationResult>(){
	
	    @Override
	    public void onResponse(AuthenticationResult result) {
	        // Authentication succeeded
	    }
	
	    @Override
	    public void onError() {
	        // Authentication failed
	    }
	
	});*/
    
    
}

Error when trying to use a public method from apiClient:

58c7fcfe30493_compileErrorApiclient.png

Link to comment
Share on other sites

I just copied your code into my Android project and it validated fine (minus the Logger, HttpClient and Serializer that are local to your implementation).  The ApiClient construction and method call were fine.

 

So, it must be something to do with your particular Java version or environment.  We have only ever utilized this in an Android environment...

Link to comment
Share on other sites

sampsonight

Ok, so its definitely a dependency issue. I'm assuming you are using the Andriod SDK? I really don't want to bundle all the andriod dependencies with a non andriod app...

 

So I have few options:

* Bundle andriod dependencies

* Determine what andriod dependencies are required for the api to run and try to remove those so the API client can be used in a pure java project

* Write a new rest api client for emby

 

I"m going to download the AndriodSDK and load in the project to verify it compiles as you have indicated.

 

I think this thread can be marked as resolved. I'll post back on the direction I decide to go.. leaning toward looking to see what library(s) I would need to use the current api and seeing if they can be removed from the core Api.

Link to comment
Share on other sites

sampsonight

Even when I import my project into the AndriodSDK I unable to use any of the instance methods. 

 

It must be some configuration on my end. 

 

I'll take a stab at writing an api client for Emby. I'll be able to use the current api client as a reference and strip out all the Andriod specific code.

 

Error showing red on method:

58c8116e629c0_AndriodStudioApiClientNotR

Link to comment
Share on other sites

  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...