1

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!

Community
  • 1
  • 1
supermus
  • 576
  • 1
  • 9
  • 21

2 Answers2

1

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);
Community
  • 1
  • 1
supermus
  • 576
  • 1
  • 9
  • 21
  • this is certainly a good solution, although you need to fetch the jars, while `urlread2` uses already available `sun.net.www.protocol.http` and `sun.net.www.protocol.https`. I guess if it's just a single API end-point that your are going to use, then you can take some bits of code from `urlread2` and make put it in a function, therefore making your code pretty much dependency-free. I am not too sure whether Apache HTTP client is better then the Sun's one, any ideas? – errordeveloper Jan 23 '13 at 07:56
  • 1
    This solution is straightforward. I have tried numerous time to use urlread2 for POST-ing data with different headers without success. Thanks anyway :) – supermus Jan 24 '13 at 10:23
0

I would rather use built-in methods. Matlab hasurlread/urlwrite, which could work if all you wish to do is request some CSV data from Cosm API. If you do need to use JSON, it can be handled in Matlab via a plugin.

Passissing the Cosm API key, that can be done via key parameter like so:

cosm_feed_url = "https://api.cosm.com/v2/feeds/61916.csv?key=<API_KEY>"
cosm_feed_csv = urlread(cosm_feed_url)

However, the standard library methods urlread/urlwrite are rather limited. In fact, the urlwrite function is only designed for file input, and I cannot even see any official example of how one could use a formatted string instead. Creating a temporary file would reasonable, unless it's only a few lines of CSV. You will probably need to use urlread2 for anything more serious.

UPDATE: it appears that urlread2 can be problematic.

Community
  • 1
  • 1
errordeveloper
  • 6,716
  • 6
  • 41
  • 54
  • I wish I could extend the example, but unfortunately I don't have Matlab at hand. – errordeveloper Jan 22 '13 at 16:21
  • I have found that urlread by default use hardcoded content type. I need to send POST using urlead with content type text/plain; charset=utf-8 and to add api key to header. – supermus Jan 23 '13 at 01:17