javamatlabpostcosm

Post data from Matlab to Pachube (Cosm) using Java Methods


I am using JPachube.jar and Matlab in order to send data to my datastream. This java code works on my machine:

package smartclassroom;
import Pachube.Data;
import Pachube.Feed;
//import Pachube.FeedFactory;
import Pachube.Pachube;
import Pachube.PachubeException;

public class SendFeed {     

    public static void main(String arsg[]) throws InterruptedException{
        SendFeed s = new SendFeed(0.0);
        s.setZainteresovanost(0.3);
        double output = s.getZainteresovanost();      
        System.out.println("zainteresovanost " + output);             

        try {
                Pachube p = new Pachube("MYAPIKEY");
                Feed f = p.getFeed(MYFEED);
                f.updateDatastream(0, output);
            } catch (PachubeException e) {
                System.out.println(e.errorMessage);
            }
    }

    private double zainteresovanost;    
    public SendFeed(double vrednost) {
        zainteresovanost = vrednost;
    }
    public void setZainteresovanost(double vrednost) {
        zainteresovanost = vrednost;
    }
    public double getZainteresovanost() {
        return zainteresovanost;
    }

}

but I need to do this from Matlab. I have tried rewriting example (example from link is working on my machine): I have compile java class with javac and added JPachube.jar and SendFeed.class into path and then utilize this code in Matlab:

javaaddpath('C:\work')
javaMethod('main','SendFeed','');    
pachubeValue = SendFeed(0.42);

I get an error:

??? Error using ==> javaMethod
No class SendFeed can be located on Java class path

Error in ==> post_to_pachube2 at 6
javaMethod('main','SendFeed','');

This is strange because, as I said example from the link is working.

Afterwards, I decided to include JPachube directly in Matlab code and to write equivalent code in Matlab:

 javaaddpath('c:\work\JPachube.jar')

import Pachube.Data.*
import Pachube.Feed.*
import Pachube.Pachube.*
import Pachube.PachubeException.*

pachube = Pachube.Pachube('MYAPIKEY');
feed = pachube.getFeed(MYFEED);
feed.updateDatastream(0, 0.54);

And I get this error:

??? No method 'updateDatastream' with matching signature found for class 'Pachube.Feed'.

Error in ==> post_to_pachube2 at 12
feed.updateDatastream(0, 0.54);

So I have tried almost everything and nothing! Any method making this work will be fine for me. Thanks for help in advance!


Solution

  • This done trick for me (answer from here)

    javaaddpath('c:\work\httpcore-4.2.2.jar');
    javaaddpath('c:\work\httpclient-4.2.3.jar');
    
    
    import org.apache.http.impl.client.DefaultHttpClient
    import org.apache.http.client.methods.HttpPost
    import org.apache.http.entity.StringEntity
    
    
    httpclient = DefaultHttpClient();
    
    httppost = HttpPost('http://api.cosm.com/v2/feeds/FEEDID/datastreams/0.csv?_method=put');
    httppost.addHeader('Content-Type','text/plain');
    httppost.addHeader('X-ApiKey','APIKEY');
    
    params = StringEntity('0.7');
    httppost.setEntity(params);
    
    response = httpclient.execute(httppost);