HTTP Post with Matlab - java

I followed the example given in this post: How to send data from Matlab to Rails, but am receiving an error message that I cannot find any information on. My script looks like this:
javaaddpath('./httpcomponents/httpclient-4.2.2.jar')
javaaddpath('./httpcomponents/httpcore-4.2.2.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://localhost:3000/signin');
httppost.addHeader('Content-Type','application/json');
httppost.addHeader('Accept','application/json');
tokenRequest = StringEntity('{"session", "{email_address:""email#aol.com,""password:""password""}"}');
httppost.setEntity(tokenRequest);
response = httpclient.execute(httppost);
On the last line, I get the error:
Java exception occurred: java.lang.VerifyError: Cannot inherit from final class
From searches online, I gather that this is a software version issue. I tried using the 4.2 builds of those files (the same ones used in the other post), but I received the same error. Does anyone have an idea what could be wrong? Or know a way to do what I am trying to do without using these external libraries?
EDIT:
originally I tried using this code:
tokenRequest = {'session', '{''email_address'':''email#aol.com'',''password'':''password''}'};
token = urlread('http://localhost:3000/signin','POST',tokenRequest);
but I received the NoMethodError that led me to that other post:
NoMethodError (undefined method `each' for "{'email_address':'email#aol.com','password':'password'}":String):
app/models/session.rb:14:in `initialize'
I think the reason it throws this error is because the server thinks it is receiving a String object, which doesn't have an each method. I assume I would fix this by using the 'Content-Type' argument to specify that its json. Is there a way to do this using urlread?
EDIT: full stack trace for java libs issue
Java exception occurred:
java.lang.VerifyError: Cannot inherit from final class
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at com.mathworks.jmi.CustomURLClassLoader.findClass(ClassLoaderManager.java:760)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at org.apache.http.impl.client.DefaultHttpClient.createHttpParams(DefaultHttpClient.java:157)
at org.apache.http.impl.client.AbstractHttpClient.getParams(AbstractHttpClient.java:448)
at org.apache.http.impl.client.AbstractHttpClient.createClientConnectionManager(AbstractHttpClient.java:309)
at org.apache.http.impl.client.AbstractHttpClient.getConnectionManager(AbstractHttpClient.java:466)
at org.apache.http.impl.client.AbstractHttpClient.createHttpContext(AbstractHttpClient.java:286)
at
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:851)
at
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
at
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:784)

Looks like the data being sent is not properly quoted. End result should be
{'email_address':'email#aol.com','password':'password'}
Try changing the code to
tokenRequest = StringEntity('{"session", {"email_address":"email#aol.com","password":"password"}}');

urlread2 turned out to be a much better solution. This code does the job:
tokenRequest = '{ "session" : { "email_address": "email#aol.com", "password": "password" } }';
header = http_createHeader('Content-Type','application/json');
token = urlread2('http://localhost:3000/signin.json','POST',tokenRequest,header);

Related

Microsoft Translator API error retrieving translation

I'm simply trying to run this sample code below:
import com.memetix.mst.language.Language;
import com.memetix.mst.translate.Translate;
public class Translator {
public static void main(String[] args) throws Exception {
Translate.setClientId("ID GOES HERE");
Translate.setClientSecret("SECRET GOES HERE");
String translatedText = Translate.execute("Bonjour le monde",
Language.FRENCH, Language.ENGLISH);
System.out.println(translatedText);
}
}
and I'm getting the following Exception:
Exception in thread "main" java.lang.Exception: [microsoft-translator-api] Error retrieving translation : datamarket.accesscontrol.windows.net
at com.memetix.mst.MicrosoftTranslatorAPI.retrieveString(MicrosoftTranslatorAPI.java:202)
at com.memetix.mst.translate.Translate.execute(Translate.java:61)
at Translator.main(Translator.java:10)
Caused by: java.net.UnknownHostException: datamarket.accesscontrol.windows.net
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.security.ssl.SSLSocketImpl.connect(Unknown Source)
at sun.security.ssl.BaseSSLSocketImpl.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.<init>(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.New(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(Unknown Source)
at com.memetix.mst.MicrosoftTranslatorAPI.getToken(MicrosoftTranslatorAPI.java:133)
at com.memetix.mst.MicrosoftTranslatorAPI.retrieveResponse(MicrosoftTranslatorAPI.java:160)
at com.memetix.mst.MicrosoftTranslatorAPI.retrieveString(MicrosoftTranslatorAPI.java:199)
... 2 more
I know it seems like I'm not even trying to figure this out on my own but I'm a complete beginner and can't really understand the Exception trace at all by myself. I'm pretty sure I got the right client Secret. In my azure account I only see an application ID and an Object ID. I'm using the application ID as the client ID.
Does anyone have any ideas on what might be causing this? Any help is greatly appreciated.
Thank you!
The third party Java wrapper boatmeme/microsoft-translator-java-api for MS Azure Translator API is too old & unavailable, because it wrappered the old Microsoft Translator - Text Translation which is old & unavailable now.
There is a notice at the page top of the site Azure datamarket.
DataMarket and Data Services are being retired and will stop accepting new orders after 12/31/2016. Existing subscriptions will be retired and cancelled starting 3/31/2017. Please reach out to your service provider for options if you want to continue service.
For using the new Azure Translator API on Azure portal, you need to refer to the document Announcements: Microsoft Translator Moves to the Azure portal to know how to create the new one on Azure portal and use it via the new REST APIs. Meanwhile, just as reference, you can see my answer in Java for the other SO thread Microsoft Translator API Java, How to get client new ID with Azure.
Hope it helps.

UMLPackage.Literals.MODEL throws java.lang.NoClassDefFoundError error

I would like to get the state machine from .uml file created by papyrus. I am parsing the model.uml file using the code below.
ResourceSet set = new ResourceSetImpl();
set.getResourceFactoryRegistry().getExtensionToFactoryMap()
.put(UMLResource.FILE_EXTENSION, UMLResource.Factory.INSTANCE);
set.getResourceFactoryRegistry().getExtensionToFactoryMap()
.put(UMLResource.FILE_EXTENSION, UMLResource.Factory.INSTANCE);
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap()
.put(UMLResource.FILE_EXTENSION, UMLResource.Factory.INSTANCE);
Resource res = set.getResource(URI.createFileURI("resources/model.uml"), true);
EcoreUtil.resolveAll(res);
System.out.println(res.getContents().get(0));
This code works fine and prints the content. But when I try to cast it to Model class using the code below
Model model = (Model) EcoreUtil.getObjectByType(res.getContents(),UMLPackage.Literals.MODEL);
It throws this exception,
Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/uml2/types/TypesPackage
at org.eclipse.uml2.uml.internal.impl.UMLPackageImpl.init(UMLPackageImpl.java:1907)
at org.eclipse.uml2.uml.UMLPackage.<clinit>(UMLPackage.java:83)
at org.eclipse.uml2.uml.UMLPackage$Literals.<clinit>(UMLPackage.java:28779)
at mp.m2bt.BTGenerator.main(BTGenerator.java:49)
Caused by: java.lang.ClassNotFoundException: org.eclipse.uml2.types.TypesPackage
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
I am not sure if I am missing anything. I think the problem is in UMLPackage.Literals.MODEL. So, Can anybody help me to fix this issue and get the state machines from the model.
You need the jar org.eclipse.uml2.types in your classpath. This is why the type org.eclipse.uml2.types.TypesPackage cannot be found.

Simplest horrorss code doesn't run

I am using the Java RSS parser horrorss
I added horrorss-2.2.0.jar to my Build path in Eclipse.
I can't even get the Simplest bit of Code to run.
import org.horrabin.horrorss.*;
public class Test {
public static void main(String[] args) throws Exception {
RssParser rss = new RssParser();
try{
RssFeed feed = rss.load("http://rss.slashdot.org/Slashdot/slashdot");
// Gets the channel information of the feed and
// display its title
RssChannelBean channel = feed.getChannel();
System.out.println("Feed Title: " + channel.getTitle());
}catch(Exception e){
System.err.println(e);
}
}
}
The Console Output is always the following
Exception in thread "main" java.lang.NoClassDefFoundError: com/hp/hpl/sparta/Parser
at org.horrabin.horrorss.RssParser.parseFromReader(RssParser.java:431)
at org.horrabin.horrorss.RssParser.parseFromURL(RssParser.java:455)
at org.horrabin.horrorss.RssParser.load(RssParser.java:115)
at org.horrabin.horrorss.RssParser.load(RssParser.java:147)
at Test.main(Test.java:10)
Caused by: java.lang.ClassNotFoundException: com.hp.hpl.sparta.Parser
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 5 more
I hope you guys are able to help me out (or show me another nice method for Handling RSS Feeds in Java)
Did you read this on the homepage?
It requires Sparta-XML, a light-weight XML parser based on XPath.
You need to get the Sparta-XML jar and add that to the eclipse build path too.

Skype error? Or Eclipse error?

I tried making a simple project for my skype-bot, and it looks like this:
import com.skype.Skype;
import com.skype.SkypeException;
public class SkypeDemo {
public static void main(String[] args) throws SkypeException {
System.out.println(Skype.getVersion());
}
}
But when i started it i get this error:
Exception in thread "main" com.skype.SkypeException: Loading libskype.jnilib failed.
at com.skype.Utils.convertToSkypeException(Unknown Source)
at com.skype.Utils.getProperty(Unknown Source)
at com.skype.Skype.getVersion(Unknown Source)
at SkypeDemo.main(SkypeDemo.java:6)
Caused by: com.skype.connector.LoadLibraryException: Loading libskype.jnilib failed.
at com.skype.connector.ConnectorUtils.loadLibrary(Unknown Source)
at com.skype.connector.osx.SkypeFramework.init(Unknown Source)
at com.skype.connector.osx.OSXConnector.initializeImpl(Unknown Source)
at com.skype.connector.Connector.initialize(Unknown Source)
at com.skype.connector.Connector.connect(Unknown Source)
at com.skype.connector.Connector.assureAttached(Unknown Source)
at com.skype.connector.Connector.execute(Unknown Source)
at com.skype.connector.Connector.execute(Unknown Source)
at com.skype.connector.Connector.execute(Unknown Source)
at com.skype.connector.Connector.execute(Unknown Source)
... 3 more
So how do I fix this error? Keep in mind I have both Skype jars in the build path.
The error is telling you that it cannot load a native library ("libskype.jnilib"). This is (probably) neither Eclipse or Skype's fault.
It is most likely your fault because either the native library is not where it needs to be (or you haven't told Eclipse where to look), or because you have the wrong flavour of native code (DLL, .so or whatever)
(If there was another chained "cause" in the stack trace, it might tell you more ...)
Reference:
This blog post explains how to add a native library to an Eclipse project - http://www.eclipsezone.com/eclipse/forums/t49342.html (In fact, it describes two ways ...)

Apache XML-RPC seems unable to parse long type passed to it by android-xmlrpc

I've got a little problem developing an Android app. I've got a client (running Android), using android-xmlrpc, that calls some methods on the server (standard Java app), using Apache XML-RPC. Everything runs fine and smoothly, with one exception. When I try to call a method that has a Long type parameter, the server throws out this exception :
21.12.2010 18:54:35 org.apache.xmlrpc.server.XmlRpcErrorLogger log
SEVERE: Failed to parse XML-RPC request: Unknown type: i8
org.apache.xmlrpc.XmlRpcException: Failed to parse XML-RPC request: Unknown type: i8
at org.apache.xmlrpc.server.XmlRpcStreamServer.getRequest(XmlRpcStreamServer.java:71)
at org.apache.xmlrpc.server.XmlRpcStreamServer.execute(XmlRpcStreamServer.java:199)
at org.apache.xmlrpc.webserver.Connection.run(Connection.java:208)
at org.apache.xmlrpc.util.ThreadPool$Poolable$1.run(ThreadPool.java:68)
Caused by: org.xml.sax.SAXParseException: Unknown type: i8
at org.apache.xmlrpc.parser.RecursiveTypeParserImpl.startElement(RecursiveTypeParserImpl.java:122)
at org.apache.xmlrpc.parser.XmlRpcRequestParser.startElement(XmlRpcRequestParser.java:122)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at org.apache.xmlrpc.server.XmlRpcStreamServer.getRequest(XmlRpcStreamServer.java:65)
... 3 more
Everything should be in order, the EnabledForExtensions flag on the Apache side, as mentioned here , is set like this :
serverConfig.setEnabledForExtensions(true);
What am I doing wrong?
It seems there are two dialects for Long parameters, and the client and server have to agree which to use. Eg on a c++ server you need to call:
myRegistry.setDialect(xmlrpc_dialect_apache)
Or
myRegistry.setDialect(xmlrpc_dialect_i8)
Or a corresonding method on your client/server.
I believe the default is i8.
I found the solution of this problem here: https://ws.apache.org/xmlrpc/advanced.html
I must create own implementation of the TypeFactory, something like this:
public class ExtendedTypeFactoryImpl extends TypeFactoryImpl {
private static final String LONG_XML_TAG_NAME = "i8";
public ExtendedTypeFactoryImpl(XmlRpcController pController) {
super(pController);
}
#Override
public TypeParser getParser(XmlRpcStreamConfig pConfig, NamespaceContextImpl pContext, String pURI, String pLocalName) {
if (LONG_XML_TAG_NAME.equals(pLocalName)) {
return new LongParser();
} else {
return super.getParser(pConfig, pContext, pURI, pLocalName);
}
}
}
Then I must set my type factory of my XMPRPC client:
XmlRpcClient client = new XmlRpcClient();
XmlRpcClientConfigImpl conf = new XmlRpcClientConfigImpl();
conf.setServerURL(url.toURL());
conf.setEncoding(Charsets.UTF_8.name());
conf.setEnabledForExtensions(true);
client.setTypeFactory(new ExtendedTypeFactoryImpl(client));
client.setConfig(conf);

Categories

Resources